jaxonavena / Battleship

1 stars 1 forks source link

Skipping turns while hiding ships #2

Closed jaxonavena closed 2 months ago

jaxonavena commented 2 months ago

We're getting an output like this ATM. It skips hiding ships.

How many ships per team?: 3
['1x3', '1x2', '1x1']
['1x3', '1x2', '1x1']
Player 1 - Hiding their Destroyer...
  A B C D E F G H I J
0 * * * * * * * * * *
1 * * * * * * * * * *
2 * * * * * * * * * *
3 * * * * * * * * * *
4 * * * * * * * * * *
5 * * * * * * * * * *
6 * * * * * * * * * *
7 * * * * * * * * * *
8 * * * * * * * * * *
9 * * * * * * * * * *
Hide the ship: d2
Unhidden ships: ['1x2', '1x1']
Player 1 - Hiding their Submarine...
  A B C D E F G H I J
0 * * * * * * * * * *
1 * * * * * * * * * *
2 * * * @ * * * * * *
3 * * * * * * * * * *
4 * * * * * * * * * *
5 * * * * * * * * * *
6 * * * * * * * * * *
7 * * * * * * * * * *
8 * * * * * * * * * *
9 * * * * * * * * * *
Hide the ship: e4
Unhidden ships: ['1x1']
Player 2 - Hiding their Destroyer...
  A B C D E F G H I J
0 * * * * * * * * * *
1 * * * * * * * * * *
2 * * * * * * * * * *
3 * * * * * * * * * *
4 * * * * * * * * * *
5 * * * * * * * * * *
6 * * * * * * * * * *
7 * * * * * * * * * *
8 * * * * * * * * * *
9 * * * * * * * * * *
Hide the ship: 

I think the bug may be found in Player.hide_ships(), but I'm not sure. I think the fact that we're iterating on the contents of self.ship_list while also popping from self.ship_list in the same loop may be the cause. Chat said that it doesn't reread the list every iteration, but idk if I believe it... Even if it did reread it though, if we're popping the front of the list then it should restart on the proper one, right?

Chat suggested it might be a recursion issue, so I restructured Player.hide_ships() like so to avoid it:

  def hide_ships(self):
    for _ship in self.ship_list: # Hide all ships
      while True:
        print(f"Player {self.id} - Hiding their {self.selected_ship()}...")
        self.print_board(self.board)

        coord = input("Hide the ship: ")

        if coord in self.list_of_synonyms_for_quit_lol: # QUIT GAME?
          exit()

        if self.valid_coord(coord):
          # coord = "a5"
          # coord => GameObject.letter_to_row_index_map => __hide_ship(0,5) => col, row
          # TODO: UPDATE THIS COMMENT (AND FUNCTION) FROM 05 WHEN ROW NUMBERS CHANGE
          self.__hide_ship(int(self.letter_to_row_index_map[coord[0].upper()]), int(coord[1])) # Not to be confused with hide_ships()
          # self.print_board(self.board)
          break

This still didn't fix it though, so idk.

peteraw03 commented 2 months ago

Fixed - Changed for loop in player.hide_ships to a while loop

jaxonavena commented 2 months ago

Push it up? @peteraw03