Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- board = []
- for x in range(6):
- board.append(["O"] * 6)
- def print_board(board):
- for row in board:
- print((" ").join(row))
- 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)
- testMode = input("Do you want to play the game in test mode? Enter answer in format Y/N ")
- if testMode.lower() == ("y"):
- 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
- 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
- print("Enjoy the game in regular mode!")
- else:
- print("Invalid input, starting in normal mode!")
- print("Let's play Battleship!")
- print_board(board)
- for turn in range(9):
- print ("Turn", turn+1) #it automatically starts at 0 so if i just put +1 it will start at 1 instead of 0
- #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
- #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.
- guess = (input("Guess Row,Col"))
- 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
- 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
- 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
- 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")
- turn =+ 1
- print_board(board)
- board[ship_row][ship_col] = "*"
- print_board(board)
- playAgain = input("Would you like to play again? Enter your answer in the format Y/N ")
- if playAgain.lower() == ("y"):
- print("Starting a new game..")
- print_board(board)
- elif playAgain.lower() == ("n"):
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement