Having problems with Battleship!


#1

Heres my code…

from random import randint

board = []

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

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

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
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
  print "Congratulations! You sank my battleship!"   
else:
  if guess_row not in range(5) or \
     guess_col not in range(5):
    print "Oops, that's not even in the ocean."
  else:
    print "You missed my battleship!"
    board[guess_row][guess_col] = "X"
  print_board(board)

when ran it turns out as this…

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
1
3
Guess Row: 4
Guess Col: 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 X

It will not accept my guess_col as a user input and is assigning it to the value put in for guess_row.


#2

i ran your code here:

https://repl.it/@stetim94/DarkgreyPoliticalHeron

and it worked fine, what if you run your code at that site?


#3

Thank you for checking that and so soon… I really like repl.it and am using it for other programs but I want this project to work so I can progress in the lessons… if I have it show the solution it accepts it as correct but still runs incorrectly, I even checked and compared the solution code and mine and they are identical (with the exception of maybe some white space) and I was hoping I could run it on the same site…
Thanks again for checking and I guess I’ll just start running it on another IDE.


#4

define incorrectly? What you uploaded looks wrong:

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
1
3
Guess Row: 4
Guess Col: 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 X

in repl if i enter 1, 3 i get:

O O O O O
O O O X O
O O O O O
O O O O O
O O O O O

which is fine. That lists are zero indexed based and users start counting at one is outside the scope of this project


#5

well as it is it still prints the location of the ship for debugging purposes so the 1,3 is where the ship actually is
the 4 is what is inputted… and it won’t even accept the guess_col variable and is instead assigning both the guess_row and guess_col to the same int that is supposed to be just the guess_row


#6

but i covered this:

but feel free if you can solve this problem


#7

that would be as simple as making

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

into

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

or would there be a better way to do this?


#8

Yes, its that simple. Yet some people struggle with this because they want to manipulate the list indexes.

What you have is indeed the right and best solution


#9

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