예제 #1
0
    def get_next_move(self, board: Board) -> Coordinate:

        # ______________________________
        # Get previously experienced board position

        possible_moves = []

        try:
            current_state = board.get_board()
            possible_moves = self._matrix[current_state]
        except:
            try:
                current_state = board.get_negative_board()
                possible_moves = self._matrix[current_state]
            except:
                pass

        # ______________________________
        # Get previously experienced board positions by rotating

        must_rotate = False

        if len(possible_moves) == 0:
            must_rotate = True
        else:
            for i in range(len(possible_moves)):
                if possible_moves[i].value > 0:
                    must_rotate = False
                    break

        if must_rotate:
            possible_moves.extend(
                self._get_rotated_possible_moves(board.get_board()))
            possible_moves.extend(
                self._get_rotated_possible_moves(board.get_negative_board()))

        # ______________________________
        # Get best moves

        if len(possible_moves) == 0:
            return None

        best_value = 1
        best_coordinates = []

        for i in range(len(possible_moves)):
            if possible_moves[i].value > best_value:
                best_value = possible_moves[i].value
                best_coordinates.clear()
                best_coordinates.append(possible_moves[i].coordinate)
            elif possible_moves[i].value == best_value:
                best_coordinates.append(possible_moves[i].coordinate)

        # ______________________________
        # Return random best move

        if len(best_coordinates) == 0:
            return None
        elif len(best_coordinates) == 1:
            return best_coordinates[0]
        else:
            ret_index = random.randint(0, len(best_coordinates) - 1)
            return best_coordinates[ret_index]