18. To Your Battle Stations!


#1


https://www.codecademy.com/en/courses/python-beginner-en-4XuFm/2/4?curriculum_id=4f89dab3d788890003000096

No error messages, just not working 100% correctly. Basically the issue is that the game doesn't always recognize a successful hit and instead says it was a miss. It appears to only work properly when both the row and col are the same number. If they're different and I enter the coordinates it says I missed. Note: I added '- 1' to the end of the raw inputs because I didn't like that hypothetical users would be required to guess 0-4 instead of 1-5. I wanted to make the game user friendly and give them the natural guess options even if this is just an exercise. The actual values of the coordinates are still 0-4 however, but since these are hidden values it doesn't really matter. Although, I suspect the issue could have something to do with this inconsistency so if that is the problem then obviously I need to fix.

The game should render 100% of correct guesses as success rather than a failure. Here are some examples of results, first a win:

Let's play Battleship!
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
4
4
Turn 1
Guess Row: 4
Guess Col: 4
You missed my battleship!
O O O O O
O O O O O
O O O O O
O O O X O
O O O O O
Turn 2
Guess Row: 5
Guess Col: 5
Congratulations! You sunk my battleship!
O O O O O
O O O O O
O O O O O
O O O X O
O O O O O
None

Now a false loss:

Let's play Battleship!
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
4
1
Turn 1
Guess Row: 4
Guess Col: 1
You missed my battleship!
O O O O O
O O O O O
O O O O O
X O O O O
O O O O O
Turn 2
Guess Row: 5
Guess Col: 2
You missed my battleship!
O O O O O
O O O O O
O O O O O
X O O O O
O X O O O
Turn 3
Guess Row:

You'll notice that for the win both coordinates were 4, so I first typed 4,4 to demonstrate that guess is incorrect. Then I typed 5,5 and I won the game. For the false loss I did the same, but when I typed 5,2 which should have generated a win, it still says I missed and to try again.


from random import randint

board = []

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

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

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)
print ship_col
print ship_row

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
for turn in range(5):
    print 'Turn', turn + 1
    
    guess_row = int(raw_input("Guess Row:")) - 1
    guess_col = int(raw_input("Guess Col:")) - 1
    
    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
        print_board(board)
        break
        
    else:
        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."
            print_board(board)
            
        elif(board[guess_row][guess_col] == "X"):
            print "You guessed that one already."
            print_board(board)
        else:
            print "You missed my battleship!"
            board[guess_row][guess_col] = "X"
            print_board(board)
            
        if turn == 4:
            print 'Game Over'


#2

Your code is working just fine. The reason your outputs aren't correct, is that you print the ships column location BEFORE the row location. (This is also why it worked when your location's row and col were the same)
Just do

print ship_row
print ship_col

instead of

print ship_col
print ship_row

You can swap those, or you can just input the values backwards. For example:

Let's play Battleship!
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
4
1
Turn 1
Guess Row: 2
Guess Col: 5


#3

Haha wow so simple like most things! Thanks for the help, I'll get better at noticing these small mistakes as time goes on. lol.


#4

I won't pretend to be an expert, but for me, the thing that stands out is the -1:

guess_row = int(raw_input("Guess Row:")) - 1
guess_col = int(raw_input("Guess Col:")) - 1

What this means is that when you enter 5 and 5, you correctly added the -1 to convert from the player counting "1, 2, 3, 4, 5" to the computer counting "0, 1, 2, 3, 4." However, when the computer gives you 4 and 4, you have to remember that, converting to the way humans count, you have to add 1, since you subtracted 1 when converting to computer counting. If this annoys you you could alter the debug print statement to:

print ship_col + 1
print ship_row + 1

#5

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