Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. from random import randint
  2.  
  3. board = []
  4.  
  5. for x in range(6):
  6. board.append(["O"] * 6)
  7.  
  8. def print_board(board):
  9. for row in board:
  10. print((" ").join(row))
  11.  
  12. def random_row(board):
  13. return randint(0, len(board) - 1)
  14. def random_col(board):
  15. return randint(0, len(board[0]) - 1)
  16.  
  17. ship_row = random_row(board)
  18. ship_col = random_col(board)
  19.  
  20. testMode = input("Do you want to play the game in test mode? Enter answer in format Y/N ")
  21.  
  22. if testMode.lower() == ("y"):
  23. print("The coordinates of the battleship are", ship_row+1, ship_col+1) #I'm adding the +1 to match the range I've changed for user input / output for later in the programme, althought the programme is still reading in 0-5
  24. elif testMode.lower() == ("n"): #I'm outputting for the user 1-6 as it makes sense mathematically for a non programming user to start at 1,1 and end at 6,6 instead of 0,0 and 5,5
  25. print("Enjoy the game in regular mode!")
  26. else:
  27. print("Invalid input, starting in normal mode!")
  28.  
  29. print("Let's play Battleship!")
  30. print_board(board)
  31.  
  32.  
  33. for turn in range(9):
  34. print ("Turn", turn+1) #it automatically starts at 0 so if i just put +1 it will start at 1 instead of 0
  35. #old code guess_row = int(input("Guess Row:"))-1 #I've added the -1 because the user who is a non programmer will recognise grids starting at 1,1 not 0,0. so if the user wanted to enter 1,1 for the top left corner
  36. #old code guess_col = int(input("Guess Col:"))-1 #it will translate to the programme to actually start at 0,0 which is the top left grid for the programme.
  37. guess = (input("Guess Row,Col"))
  38. guesses = (guess.split(",")) # this splits the inputs before and after the comma into two different string values and puts them into a list called guesses
  39. guess_row = int(guesses[0])-1 # this assigns the first number in the list to the row variable and converts it to a int and minuses 1
  40. guess_col = int(guesses[1])-1 # this assigns the second number in the list to the column variable and then after converts it to a int so I can minus 1
  41. if guess_row == ship_row and guess_col == ship_col:
  42. print("Congratulations! You sunk my battleship!")
  43. break
  44. else:
  45. if (guess_row < 0 or guess_row > 5) or (guess_col < 0 or guess_col > 5):
  46. print("Oops, that's not even in the ocean.")
  47. elif(board[guess_row][guess_col] == "X"):
  48. print("You guessed that one already.")
  49. else:
  50. print("You missed my battleship!")
  51. board[guess_row][guess_col] = "X"
  52. if turn == 8:
  53. print("Game Over")
  54. turn =+ 1
  55. print_board(board)
  56.  
  57. board[ship_row][ship_col] = "*"
  58. print_board(board)
  59.  
  60. playAgain = input("Would you like to play again? Enter your answer in the format Y/N ")
  61.  
  62. if playAgain.lower() == ("y"):
  63. print("Starting a new game..")
  64. print_board(board)
  65. elif playAgain.lower() == ("n"):
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement