# Battleship Extra Credit

#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
I have two big issues. For whatever reason I can’t get the turn count to keep increasing after each guess and the moment I miss for the second point it resets and I have to guess on the first point again.

<What do you expect to happen instead?>
The turn counter should go: 1 2 3 4 5 etc.
So long as my guess on the first point is right, it should stay on the second point regardless of if I miss.

``````

from random import randint

board = []

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

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

print "Let's play Battleship!"
print_board(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[0]) - 1)

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

battleship_row1 = random_row1(board)
battleship_col1 = random_col1(board)
battleship_row2 = random_row2(board)
battleship_col2 = random_col2(board)
battleship_aft = (battleship_row1, battleship_col1)
battleship_fore = (battleship_row2, battleship_col2)

print battleship_row1
print battleship_col1
print battleship_row2
print battleship_col2

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!

class battleship(object):
for turn in range(6):
turn = 0
print "Turn", turn + 1

guess_row1 = int(raw_input("Guess Row1:"))
guess_col1 = int(raw_input("Guess Col1:"))

if (guess_row1 == battleship_row1) and (guess_col1 == battleship_col1):
print "That's a hit!"
board[guess_row1][guess_col1] = "X"
print_board(board)
print "Turn", turn + 1

guess_row2 = int(raw_input("Guess Row2:"))
guess_col2 = int(raw_input("Guess Col2:"))

else:
print "Turn", turn + 1
if (guess_row1 < 0 or guess_row1 >4) or (guess_col1 < 0 or guess_col1 > 4):
print "Oops, that's not even in the ocean."
elif(board[guess_row1][guess_col1] == "X") or (board[guess_row1][guess_col1] == "O"):
print "You guessed that one already."
else:
print "You missed my battleship!"
board[guess_row1][guess_col1] = "O"
print_board(board)

if (guess_row2 == battleship_row2) and (guess_col2 == battleship_col2):
print "Congratulations! You sunk my battleship!"
board[guess_row2][guess_col2] = "X"
print "You win!"
print_board(board)
break

else:
print "Turn", turn + 1
if (guess_row2 < 0 or guess_row2 > 4) or (guess_col2 < 0 or guess_col2 > 4):
print "Oops, that's not even in the ocean."
elif (board[guess_row2][guess_col2] == "O") or (board[guess_row2][guess_col2] == "X"):
print "You guessed that one already."
else:
print "You missed my battleship!"
board[guess_row2][guess_col2] = "O"
print_board(board)

guess_row2 = int(raw_input("Guess Row2:"))
guess_col2 = int(raw_input("Guess Col2:"))

if turn == 5:
print "Game Over"
break
``````

#2

because you declare `turn = 0` in the for loop, it gets reset to zero every time

you declare `turn` in the for loop, get rid of `turn = 0`

The second prompt:

``````            guess_row2 = int(raw_input("Guess Row2:"))
guess_col2 = int(raw_input("Guess Col2:"))
``````

Why did you place it there? It looks wrongly placed

furthermore, in the real battle ship board game, you can use one guess to hit any ship. You don’t give a guess for each ship

#3

i put turn = 0 because the error “turn is not defied” popped up if I only removed it. Not sure how to declare turn without setting it equal to zero.

As for guess_row2 = int(raw_input("Guess Row2: ")) and guess col2 I actually put that part in twice to see if I could fix the restarting issue. I placed it once after you guess the first point correctly and once after you missed the second point. I should only have it once though huh…

Finally, you are right about the full game allowing you to guess any point, not just one full ship at a time. But right now I’m just trying to get it to run two points correctly. Then I’ll try and expand it further.

#4

i can safely remove `turn = 0` as i did here:

``````https://repl.it/KEy7
``````

`turn` is defined in the loop:

``````for turn in range(6):
``````

as long as you don’t use `turn` outside the loop, you should be good

i assume this means, you fixed it?

good plan

#5

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