Extra Credit; I can never guess any of the two ships?



I tried to include a second battleship to my board by adding a second random row and line. I print the two random places in my code for debugging. But when I enter these random places in the Guess row and Guess col, the game says it is not the right place.

It should say You sunk my battleship, but it just keeps giving me turns. All of the wrong guesses give the right message, but for some reason I cannot guess the right col and row.

Can someone please take a look at my code below and help me in the right direction?

from random import randint

board = []

# Makes 5 by 5 board
for x in range(5):
    board.append(["O"] * 5)

def print_board(board):
    for row in board:
        print " ".join(row)

# Start of game
print "Let's play Battleship!"

# Two random ships in board
def random_row1(board):
    return randint(0, len(board) - 1)

def random_col1(board):
    return randint(0, len(board[0]) - 1)

def random_row2(board):
    return randint(0, len(board) - 1)

def random_col2(board):
    return randint(0, len(board[0]) - 1)

ship_row = (random_row1(board) or random_row2(board))
ship_col = (random_col1(board) or random_row2(board))

# Prints rows and cols for debugging
print "row1", random_row1(board)
print "col1", random_col1(board)

print "row2", random_row2(board)
print "col2", random_col2(board)

# Game loop start, 4 turns
for turn in range(4):
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))

    # if statement for good guess
    if guess_row == ship_row and guess_col == ship_col:
        board[guess_row][guess_col] = "X"
        print "Congratulations! You sunk my battleship!"
    # else statement for wrong guess
        if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or \
        guess_col > 4):
            print "Oops, that's not even in the ocean."
        elif(board[guess_row][guess_col] == "X"):
            print "You guessed that one already."
            print "You missed my battleship!"
            board[guess_row][guess_col] = "X"
        # Print (turn + 1)
        print "You have 4 turns, this is turn", turn + 1
    if turn == 3:
        print "Game Over"


Try printing out where you 've placed the ship (otherwise you can't say that you've guessed the correct location)


I try to print out two locations, for two ships, when I get these four random places, none of these are the right guess.

What do you mean exactly? I tried some more things regarding your suggestion but can't seem to figure it out yet..


I fixed it!

I changed some of the code. I changed the ship_row and ship_col variables to ship_row1, ship_col1 and ship_row2, ship_col2

And I changed the if statement for the good guess to:

 # if statement for good guess
    if (guess_row == ship_row1 and guess_col == ship_col1) or (guess_row \
    == ship_row2 and guess_col == ship_col2):
        board[guess_row][guess_col] = "X"
        print "Congratulations! You sunk my battleship!"

So now it will decide if the guess for ship_row1 and guess_col1 are both good or that the guess for ship_row2 and ship_col2 are both correct


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.