Battleship board coordination


#1

Below is the my complete Battleship code. I made some change to the way the row and column is regulated. Now the input row and column should match the actual row and column in the grid in instead of off by 1. But when running, it says there’s invalid syntax in elif board[a][b] == "X". Can anyone see why it went wrong? Thanks!

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)

ship_row = randiant(1, 5)
ship_col = randiant(1, 5)
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(input("Guess Row: "))
  guess_col = int(input("Guess Col: "))
  a = guess_row - 1
  b = guess_col - 1

  if guess_row == ship_row and guess_col == ship_col:
      print ("Congratulations! You sunk my battleship!")
      break
  else:
      if turn == 3:
          print ('Game Over')
      elif guess_row not in range(1, 6) or guess_col not in range(1, 6):
          print ('Oops, that is not even in the ocean.')
      elif board[a][b] == "X"
          print ('You guessed that one already.')
      else:
          print ('You missed my battleship!')
      board[a][b] = "X"
      print_board(board)

#2

elif board[a][b] == "X"

you missed a : at the end :slight_smile:

also look at here

ship_row = randiant(1, 5)
ship_col = randiant(1, 5)

should be randint .


#3

Thanks! It finally works:)