示例#1
0
    def get_ship_possible_placing_for_human(self, bottom_position_list,
                                            ship_size):
        square_bottom_position = Position(bottom_position_list[0],
                                          bottom_position_list[1])
        grid = self.__human_player_service.get_my_grid().grid

        ship_possible_position = {}

        right_square = Position(square_bottom_position.row,
                                square_bottom_position.column + ship_size - 1)
        down_square = Position(square_bottom_position.row + ship_size - 1,
                               square_bottom_position.column)

        try:
            Validator.validate_ship_placement(square_bottom_position,
                                              right_square, grid, ship_size)
            ship_possible_position['right'] = right_square
        except ValidatorException:
            pass

        try:
            Validator.validate_ship_placement(square_bottom_position,
                                              down_square, grid, ship_size)
            ship_possible_position['down'] = down_square
        except ValidatorException:
            pass

        return ship_possible_position
示例#2
0
 def changes_after_a_move(self, message, square_position):
     if message == 'HIT':
         # left
         if square_position.column - 1 > 0:
             self._squares_stack.append(
                 Position(square_position.row, square_position.column - 1))
         # down
         if square_position.row + 1 < 9:
             self._squares_stack.append(
                 Position(square_position.row + 1, square_position.column))
         # right
         if square_position.column + 1 < 9:
             self._squares_stack.append(
                 Position(square_position.row, square_position.column + 1))
         # up
         if square_position.row - 1 > 0:
             self._squares_stack.append(
                 Position(square_position.row - 1, square_position.column))
         self._mode = 'track'
     elif message == 'MISS':
         if len(self._squares_stack) == 0:
             self._mode = 'hunt'
         else:
             self._mode = 'track'
             self._squares_stack.pop()
     elif message.find('sank'):
         message = self.__get_ship_name_from_message(message)
         if len(self._squares_stack) == 0:
             self._mode = 'hunt'
         self._available_ships.pop(message)
示例#3
0
    def place_ship(self, name, bottom_row, bottom_column, top_row, top_column,
                   size):
        bottom = Position(bottom_row, bottom_column)
        top = Position(top_row, top_column)

        Validator.validate_ship_placement(bottom, top,
                                          self._player.my_grid.grid, size)

        ship = Ship(name, bottom, top, size)

        self._player.add_ship(name, ship)
        self.__mark_ship_on_grid(bottom, top)
示例#4
0
 def __track_mode(self):
     while len(self._squares_stack) != 0:
         if super().get_enemy_grid().grid[self._squares_stack[-1].row][
                 self._squares_stack[-1].column] == '.':
             return self._squares_stack[-1]
         else:
             self._squares_stack.pop()
     return Position(0, 0)
示例#5
0
    def testGetShipHitName_SquarePosition_RightShipName(self):
        player_grid = Grid()
        player_enemy_grid = Grid()
        player = Player(player_grid, player_enemy_grid)
        player_service = PlayerService(player)

        player_service.place_ship('Cruiser', 1, 1, 1, 3, 3)
        player_service.place_ship('Battleship', 2, 1, 2, 4, 4)

        self.assertEqual(player_service.get_ship_hit_name(Position(2, 2)),
                         'Battleship')
示例#6
0
    def human_turn(self, square_row, square_column):
        square_position = Position(square_row, square_column)
        Validator.validate_square(square_position.row, square_position.column)

        returned_value = self.__game_move(self.__human_player_service,
                                          self.__computer_player_service,
                                          square_position)
        message = self.__check_if_game_over(self.__computer_player_service,
                                            'human',
                                            self.__human_player_service)

        if message == 'notover':
            return returned_value
        else:
            return message
示例#7
0
 def __choose_square_hunt_mode(self):
     higher_probability = 0
     square_row = 0
     square_column = 0
     for row in range(1, 9):
         if row % 2 == 1:
             columns_list = [2, 4, 6, 8]
         else:
             columns_list = [1, 3, 5, 7]
         for column in columns_list:
             if self._probability_grid[row][column] > higher_probability:
                 higher_probability = self._probability_grid[row][column]
                 square_row = row
                 square_column = column
     return Position(square_row, square_column)