16/19 battleship Gameover


#1
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_row
print ship_col

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
for turn in range(4):
    print "Turn", turn + 1
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))

    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
    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."
        elif(board[guess_row][guess_col] == "X"):
            print "You guessed that one already."
        else:
            print "You missed my battleship!"
        if turn==3:
            print "Game Over"
            
    # Print (turn + 1) here!
    
    print_board(board)

This is my code it says congratulations but I feel like the code is not perfect and there are many bugs.
For example, there only supposed to be 3 turns, but my game could have 4 turns.

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
3
4
Turn 1
Guess Row: 2
Guess Col: 1
You missed my 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
Turn 2
Guess Row: 3
Guess Col: 4
Congratulations! You sunk my 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
Turn 3
Guess Row: 2
Guess Col: 3
You missed my 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
Turn 4
Guess Row: 1
Guess Col: 2
You missed my battleship!
Game Over
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
None

Appreciate your help!


#2

I think if you consider which part controls the number of turns you'll find it very obvious how to change that to 3!

You seem to not be adding any X's!

Here's a suggestion for how to handle Game Over:

for turn in range(4):
    if win:
        print "you win!"
        break
else:
    print "game over!"

If the loop finishes without reaching the break, which only happens upon victory, then the else-body is executed. This way, you do not need to have to specify the number of turns in two locations.

And.. if you start counting from 0, then 3 is the fourth number.


#3

Hi Ionatan,

Thank you so much for your prompt response. But I am still not quite sure how to change the code. If I change the range to 3, then the turn=3 game over part doesnt work.

Also regarding the part to handle game over, is it gonna be a new if and else statement?

Thank you so much again!


#4

No, that's a for-else, not an if-else. I described how that works.

You've specified the number of turns in two places, so you would need to change it in two places as well. It would be better to only do so in a single place, which is what my suggestion is about.

You could also have an infinite loop, and break out on win or loss, which would be another way to reduce the number of places where you define the number of turns.

A third is to create a variable that stores the number of turns, and use that for your range and lose condition.


#5

Thank you again for your prompt response and detailed explanation.

But I don't understand where are the 2 places that I specified the # of turns. >_<

is it " for turn in range(4): " and " if turn==3:"?

Also I think I don't have enough knowledge to understand you answers yet, unfortunately.

:frowning: I will try to figure it out.

Thanks again!


#6

hi, why you think there only supposed to be 3 turns?


#7

@webwhiz88928

Take a look at his code again, I do not see two places where there are turns specified. He used a for-else loop to run the turns, when you pass range to a for loop it will iterate over the range and stop on the last number. This will cause it to run the required amount of iterations.

Then once the for loop is done the else statement is execute, so what ever code you have there will always process after the loop is done.


#8

2 places in @webwhiz88928's code, I don't think @webwhiz88928 meant anything else


#9

I got it now! thank you so much :smile:


#10

you are right! there are supposed to be 4 turns, but when the turn = 3 then game over
i was confused :smile: Thanks!


#11

i finally figured it out! thank you so much!! :smiley: