def populate_board(self): """Populate all the cells with Coordinate objects""" for row in range(10): for col in range(10): coord = Coordinate(row, col) coord_attack = Coordinate(row, col) self.player_table.setItem(row, col, coord) self.attack_table.setItem(row, col, coord_attack)
def place_31(self, row, col): """Print the 3x1 ship on the board based on the upright variable.""" coordinates = [] result = True if self.boundaries(row, col, 3): if self.upright == False: for i in range(3): coordinates.append(Coordinate(row, col + i)) else: for i in range(3): coordinates.append(Coordinate(row + i, col)) if self.avalaible_cells(coordinates) == True: self.place_ship(coordinates) else: result = False else: result = False return result
def place_52(self, row, col): """Print the 5x2 ship on the board based on the upright variable.""" coordinates = [] result = True if self.boundaries(row, col, 5, 2): if self.upright == False: for x in range(5): for y in range(2): coordinates.append(Coordinate(row + y, col + x)) else: for x in range(2): for y in range(5): coordinates.append(Coordinate(row + y, col + x)) if self.avalaible_cells(coordinates) == True: self.place_ship(coordinates) else: result = False else: result = False return result
def place_11(self, row, col): """Print the 1x1 ship on the board.""" coordinates = [] result = True coordinates.append(Coordinate(row, col)) if self.avalaible_cells(coordinates) == True: self.place_ship(coordinates) else: result = False print(coordinates[0]) return result
def hitPlayer(self, hitted=Coordinate(-1, -1)): coordinates = [] if hitted.pos_x == -1: while True: coord = self.random_cell() if coord not in self.hitted and self.sonBoundaries( coord.pos_x, coord.pos_y): coordinates.append(coord) break else: while True: coordinates.append(Coordinate(hitted.pos_x + 1, hitted.pos_y)) coordinates.append(Coordinate(hitted.pos_x, hitted.pos_y + 1)) coordinates.append(Coordinate(hitted.pos_x - 1, hitted.pos_y)) coordinates.append(Coordinate(hitted.pos_x, hitted.pos_y - 1)) for i in range(len(coordinates) - 1, -1, -1): if self.sonBoundaries( coordinates[i].pos_x, coordinates[i].pos_y ) == False or coordinates[i] in self.hitted: coordinates.pop(i) break return coordinates
def place_33(self, row, col): """Print the 3x3 ship on the board.""" coordinates = [] result = True if self.boundaries(row, col, 3, 3): for x in range(3): for y in range(3): coordinates.append(Coordinate(row + y, col + x)) if self.avalaible_cells(coordinates) == True: self.place_ship(coordinates) else: result = False else: result = False return result
def populate_board(self): """Populate all the cells with Coordinate objects""" for row in range(10): for col in range(10): coord = Coordinate(row, col) self.board.setItem(row, col, coord)
def random_cell(self): pos_x = random.randint(0, 9) pos_y = random.randint(0, 9) if pos_x == pos_y: self.upright = not self.upright return Coordinate(pos_x, pos_y)