Completed Game Can't Handle Guesses in either R5 or C5 of Game Board


#1

The exercise says I have a working game but if I enter any guesses on the fifth row or column, I get the following response:
Capture%201

Obviously, there five rows and five columns. Why is it providing the response that I am out of the game board with the guess, when it is within the range function provided. And why was I allowed to get past the steps of the conditional statements if they weren’t working correctly? Now I don’t know what’s wrong with it nor how to fix it.


#3

range does not include its stop value, so it will produce: [0, 1, 2, 3, 4]

lists are zero indexed based, thus the bottom right has coordinate 4,4

if you to be able to guess 5,5 for bottom right spot, you will have to compensate for the fact that humans start counting at 1


#4

I think I have in fact accounted for that. When I type a row and column o 4 and 4, it puts the ‘X’ for my guess in the 4th column and 4th row (see picture. But when I do 5 on either value, it’s out of the ocean.

image

Here is the code associated with the guess input and response. I don’t know for certain that it has anything to do with this and not the code laying out the board, but please take a look if you can:

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

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 sank my battleship!" 
    break
  else:
    if (guess_row) not in range(5) \
       or (guess_col) not in range(5):
      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!"
      board[guess_row - 1][guess_col - 1] = "X"
    print_board(board)
if turn == 3:
  print "Game Over"

#5

you do manipulate the user input to compensate for 0 (python) vs 1 (human) starting point of counting, but too late. Manipulating user input here:

board[guess_row - 1][guess_col - 1] = "X"

is after validating shot is in the ocean. As such, this causes problems and bugs


#6

Oh okay! Thank you for the diagnosis. I’ll see if I can figure out the proper place. Your help is much appreciated.


#7

let me know if you need more help.


#8

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