Extra credit: Displate a separate board for each player's guesses


#1

Script of game called battleship for two players.

1. From the Random module, import a function for creating a random set of integers

from random import randint

2. Designing the Board for the Game

board1 = [] # board game initiated as an empty list
board2 = []
boards = [board1,board2]

2a. Creating a list of 5 lists to serve as game platform

for x in range(5): #range starts from 0 to 4

list consisting of 5 "O" is appended into board 5 timescreating a list of 5 lists

board1.append(["O"] * 5) 
board2.append(["O"]*5)

print( board)

2b. A function is created that fuses the lists into a 5 x5 matrix to serve as a gaming platform

def print_board(boards):
board1 = boards[0]
board2 = boards[1]

if players[next_player] == player1:
    for row in board1:
        print(player1)
        print(" ".join(row))# Each row is fused in the board

elif players[next_player] == player2:
for row in board2:
    print(player2)
        print(" ".join(row))
else:
                              
       print(player1)
       print(" ".join(row))
              
       print(player2)

       print(" ".join(row))

print("Let's play Battleship!") #Players are invited to start the game
print_board(boards) # The board is printed for the players to see

3. Creating Functions that Randomly Assign the Position of the Battleship

def random_row(board): # function for random row position of battleship
return randint(0, len(board) - 1)

def random_col(board): # function for random column position of battleship
return randint(0, len(board) - 1)

Using functions to randomly assign position of battleship

ship_row = random_row(board)
ship_col = random_col(board)

print(ship_row)

print(ship_col)

4. Function to Determine Outcome of Game by Players

def guess_ship(playa):

4a. Collects guess values of battleship position from player

guess_row = int(input("%s Please guess a row number:" % (playa)))# receives guessed row number from player. {}.format(name)
guess_col = int(input("Also guess a column number: "))# receives guessed column number from player

4b. Compares guess values with randomly generated values

print(print_board(board))
if guess_row == ship_row and guess_col == ship_col: # Condition where player wins
    print("Congratulations! You sunk the battleship!")

else:
# conditions whereby the player looses
    if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4): # Case whereby player guesses out of range
        print("Oops, %s, that's not even in the ocean." %(playa))
    elif (board[guess_row][guess_col] == "X"): #Case where player repeats previous guess.
        print("%s, You guessed that one already."%(playa))
    else: #case whereby player's guess is wrong and not one of the two cases above
        print( "%s, You missed the battleship!"%(playa))
        board[guess_row][guess_col] = "X"

5. Counting the number of turns to play

turn = 1

max_games = int(input("Enter the maximum number of games you want to play: " ))

player1 = str(input("Enter the name of the first player: "))# requests the name of the player
player2 = str(input("Enter the name of the second player: "))

players = [player1, player2]
player = 0

for turn in range(max_games):
turn = turn +1
print("Turn", turn)
next_player = (player % 2)
player = next_player + 1

    guess_ship(players[next_player])

    if turn == max_games:
       print("Game Over!")

print(print_board(board))
print( "The ship's position is: " "row %s, column %s" % (ship_row, ship_col))

Game was initially designed to be played by 2 people. I am trying to modify it so that 1. Each person has his/her guesses displayed on a separate board. 2. The game ends when 1 person guesses rightly.

Please help


#2

Hi :grin:

I'll do my best to help you out, by giving advises and correcting lines to make your code actually work.

One of the most important rules in programming is, to define your functions at the beginning of the code, and not in the middle, it will save you lots of trouble and misunderstanding of the code :wink:

Another this is, that if you write this code in Python 2 (like you did in this website), then you should use input() to let the user only type numbers (integers) and raw_input() for words only (strings and all). Even if you use int(input()), it won't help you much, because it will give out an error, if you type in a string, so the int() would be kind of unnecessary :frowning:

But if you are using Python 3, I'm really sorry for letting you read that text above :grin:

Try to print the board later on, and not at the start of the code, because you haven't defined the necessary variables yet, and will only cause trouble. :confused:

Remember that you have 2 boards, so you actually have to check the other player's board if you shoot, and not only board :slightly_smiling:

you would've to check like:

if player 1:
     elif (board1[guess_row][guess_col] == "X"):
elif player2:
     elif (board2[guess_row][guess_col] == "X"):

I hope this will help you to make fix your code a bit more and make it actually work :slightly_smiling:

This is the code I fixed so far:

from random import randint
#2. Designing the Board for the Game

board1 = [] # board game initiated as an empty list
board2 = []
boards = [board1,board2]

next_player = 1
#2a. Creating a list of 5 lists to serve as game platform

for x in range(5): #range starts from 0 to 4
    #list consisting of 5 "O" is appended into board 5 timescreating a list of 5 lists

    board1.append(["O"] * 5) 
    board2.append(["O"]*5)

    #print(board)
    #2b. A function is created that fuses the lists into a 5 x5 matrix to serve as a gaming platform

def print_board(boards):
    board1 = boards[0]
    board2 = boards[1]

    if players[next_player] == player1:
        for row in board1:
            print(" ".join(row))# Each row is fused in the board

    elif players[next_player] == player2:
        for row in board2:
            print(" ".join(row))
    else:
                              
       print(player1)
       print(" ".join(row))
              
       print(player2)

       print(" ".join(row))

def random_row(board): # function for random row position of battleship
    return randint(0, len(board) - 1)

def random_col(board): # function for random column position of battleship
    return randint(0, len(board) - 1)
    #Using functions to randomly assign position of battleship

def guess_ship(playa):
    #4a. Collects guess values of battleship position from player

    guess_row = input("%s Please guess a row number:" % (playa))# receives guessed row number from player. {}.format(name)
    guess_col = input("Also guess a column number: ")# receives guessed column number from player

    #4b. Compares guess values with randomly generated values

    #print(print_board(board))
    if guess_row == ship_row and guess_col == ship_col: # Condition where player wins
        print("Congratulations! You sunk the battleship!")

    else:
        # conditions whereby the player looses
        if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4): # Case whereby player guesses out of range
            print("Oops, %s, that's not even in the ocean." %(playa))
        elif (board[guess_row][guess_col] == "X"): #Case where player repeats previous guess.
            print("%s, You guessed that one already."%(playa))
        else: #case whereby player's guess is wrong and not one of the two cases above
            print( "%s, You missed the battleship!"%(playa))
            board[guess_row][guess_col] = "X"

#5. Counting the number of turns to play

print("Let's play Battleship!") #Players are invited to start the game
#3. Creating Functions that Randomly Assign the Position of the Battleship


turn = 1

max_games = input("Enter the maximum number of games you want to play: " )

player1 = raw_input("Enter the name of the first player: ")# requests the name of the player
player2 = raw_input("Enter the name of the second player: ")

players = [player1, player2]
player = 0

print_board(boards) # The board is printed for the players to see

ship_row = random_row(board1)
ship_col = random_col(board1)

ship_row = random_row(board2)
ship_col = random_col(board2)


print(ship_row)
print(ship_col)
#4. Function to Determine Outcome of Game by Players

for turn in range(max_games):
    turn = turn +1
    print("Turn", turn)
    next_player = (player % 2)
    player = next_player + 1

    guess_ship(players[next_player])

    if turn == max_games:
       print("Game Over!")

#print(print_board(board))
print( "The ship's position is: " "row %s, column %s" % (ship_row, ship_col))