My Battleship game works but my ships refuse to exist

Why can’t my ships appear on my map?
I’ve tryed to find a ship by changing the code but the ships refuse to exist.
Pls help.
Repl link:

code snippet

The problem most seems to be inside the create_ships function. After finding a suitable location for a ship (a cell that doesn’t already have a ship), you don’t actually place the ship there.

Try changing the board[ship] to outside the loop:

def create_ships(board):
  for _ in range(5):
    ship_r, ship_cl = randint(0, 7), randint(0, 7)
    while board[ship_r][ship_cl] == 'X':
      ship_r, ship_cl = randint(0, 7), randint(0, 7)
    board[ship_r][ship_cl] = 'X'

Thank you @WindLother
Can you please tell me the number of ships that your code generates?

  1. the loop creates a ship 5 times.
1 Like

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