Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. from random import randint
  2.  
  3. while True:
  4.  
  5. board = [] # board is the variable, the brackets are a list of values and/or variables
  6.  
  7. for x in range(6): # this is a loop
  8. board.append(["O"] * 6)
  9.  
  10. def print_board(board):
  11. for row in board:
  12. print((" ").join(row))
  13. test_mode = input("Do you want to play the test mode? Enter y for yes or n for no:") # here we are setting a variable, we use input to assign letters
  14. print("Let's play Battleship!")
  15. print_board(board)
  16.  
  17. def random_row(board):
  18. return randint(0, len(board) - 1)
  19. def random_col(board):
  20. return randint(0, len(board[0]) - 1)
  21.  
  22. ship_row = random_row(board)
  23. ship_col = random_col(board)
  24.  
  25. if test_mode == "y" or test_mode == "Y": # here we are setting an if statement to see if the user is using test mode or not
  26. print("The battleship is at row:" , ship_row+1) # here i have printed the co-ordinates of the ship row when the user is in test mode
  27. print("The battleship is at col:" , ship_col+1) # here i have printed the co-ordinates of the ship column when the user is in test mode
  28. print("You are now in test mode, goodluck") # here i have printed the message you will recieve when pressing "y" or "Y"
  29. else:
  30. print("You are not in test mode, goodluck") # here i have printed the message you will recieve when pressing "n" or "N"
  31.  
  32. for turn in range(9):
  33. print("Turn", turn + 1) # i've added +1 to the variable because python index starts at 0
  34. print("Guess between 1-6")
  35. ship_row_ship_col = input ("Enter your row and column guess using a comma between both:")
  36. ship_row_ship_col = (ship_row_ship_col.split(","))
  37. guess_row = int(ship_row_ship_col[0]) -1 # here we are setting new coordinates for the board, it's now 1-6 rather than 0-5
  38. guess_col = int(ship_row_ship_col[1]) -1
  39.  
  40. if guess_row == ship_row and guess_col == ship_col:
  41. print("Congratulations! You sunk my battleship!")
  42. break
  43. else:
  44. if (guess_row < 0 or guess_row > 5) or (guess_col < 0 or guess_col > 5):
  45. print("Oops, that's not even in the ocean.")
  46. elif(board[guess_row][guess_col] == "X"):
  47. print("You guessed that one already.")
  48. else:
  49. print("You missed my battleship!")
  50. board[guess_row][guess_col] = "X"
  51. if turn == 8:
  52. print("Game Over")
  53. print("The Battleship was located at this position") # here i have printed where the battleship is at the end of the game if the player has not found the battleship by the end of the game
  54. board[ship_row][ship_col] = "*" # here will dislay "*" on the board where the battleship was for this game
  55.  
  56. turn =+ 1
  57. print_board(board)
  58.  
  59. end_game = input("Do you want to play again?")
  60. if end_game == "y" or end_game == "Y":
  61. print("Goodluck")
  62. break
  63.  
  64. elif end_game == "n" or end_game == "N":
  65. print("Thanks for playing")
  66. continue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement