Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- while True:
- board = [] # board is the variable, the brackets are a list of values and/or variables
- for x in range(6): # this is a loop
- board.append(["O"] * 6)
- def print_board(board):
- for row in board:
- print((" ").join(row))
- 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
- print("Let's play Battleship!")
- print_board(board)
- def random_row(board):
- return randint(0, len(board) - 1)
- def random_col(board):
- return randint(0, len(board[0]) - 1)
- ship_row = random_row(board)
- ship_col = random_col(board)
- 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
- 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
- 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
- print("You are now in test mode, goodluck") # here i have printed the message you will recieve when pressing "y" or "Y"
- else:
- print("You are not in test mode, goodluck") # here i have printed the message you will recieve when pressing "n" or "N"
- for turn in range(9):
- print("Turn", turn + 1) # i've added +1 to the variable because python index starts at 0
- print("Guess between 1-6")
- ship_row_ship_col = input ("Enter your row and column guess using a comma between both:")
- ship_row_ship_col = (ship_row_ship_col.split(","))
- 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
- guess_col = int(ship_row_ship_col[1]) -1
- if guess_row == ship_row and guess_col == ship_col:
- print("Congratulations! You sunk my battleship!")
- break
- else:
- if (guess_row < 0 or guess_row > 5) or (guess_col < 0 or guess_col > 5):
- print("Oops, that's not even in the ocean.")
- elif(board[guess_row][guess_col] == "X"):
- print("You guessed that one already.")
- else:
- print("You missed my battleship!")
- board[guess_row][guess_col] = "X"
- if turn == 8:
- print("Game Over")
- 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
- board[ship_row][ship_col] = "*" # here will dislay "*" on the board where the battleship was for this game
- turn =+ 1
- print_board(board)
- end_game = input("Do you want to play again?")
- if end_game == "y" or end_game == "Y":
- print("Thanks for playing")
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement