Beispiel #1
0
    def generate_successors(
        self,
        state: TwoPlayerGameState,
    ) -> List[TwoPlayerGameState]:
        """Generate the list of successors of a game state."""
        successors = []
        board = state.board
        moves = self._get_valid_moves(board, state.next_player.label)

        for move in moves:
            board_successor = copy.deepcopy(state.board)
            assert isinstance(state.next_player, Player)
            # show the move on the board
            board_successor[move] = state.next_player.label
            # flip enemy
            for enemy in self._enemy_captured_by_move(board, move, state.next_player.label):
                board_successor[enemy] = state.next_player.label
            move_code = self._matrix_to_display_coordinates(move)
            successor = state.generate_successor(
                board_successor,
                move_code,
            )

            successors.append(successor)

        if not successors:
            board_successor = copy.deepcopy(state.board)
            move_code = None
            no_movement = state.generate_successor(
                board_successor,
                move_code,
            )
            successors = [ no_movement ]

        return successors
Beispiel #2
0
def complex_evaluation_function(state: TwoPlayerGameState) -> float:
    """Return zero, except for terminal game states."""
    state_value = 0
    if state.end_of_game:
        scores = state.scores
        # Evaluation of the state from the point of view of MAX

        assert isinstance(scores, (Sequence, np.ndarray))
        score_difference = scores[0] - scores[1]

        if state.is_player_max(state.player1):
            state_value = score_difference
        elif state.is_player_max(state.player2):
            state_value = -score_difference
        else:
            raise ValueError('Player MAX not defined')
    else:
        successors = state.game.generate_successors(state)
        # Minimize the number of your opponent moves (for MAX).
        score_difference = -len(successors)
        if state.is_player_max(state.player1):
            state_value = score_difference
        elif state.is_player_max(state.player2):
            state_value = -score_difference

    return state_value
Beispiel #3
0
    def evaluate(self, state: TwoPlayerGameState) -> float:
        if not isinstance(state.game, Reversi):
            return 0
        if isinstance(state.board, dict):
            board = state.board
        else:
            board = reversi.from_array_to_dictionary_board(state.board)

        game = state.game
        corners = [board.get((1, 1)), board.get((1, game.height)), board.get((game.width, 1)),
                   board.get((game.width, game.height))]
        x_squares = [board.get((2, 2)), board.get((2, game.height - 1)), board.get((game.width - 1, 2)),
                     board.get((game.width - 1, game.height - 1))]
        corner_diff = 0
        for i in range(4):
            if corners[i] == game.player1.label:
                corner_diff += 4
            elif corners[i] == game.player2.label:
                corner_diff -= 4
            else:
                if x_squares[i] == game.player1.label:
                    corner_diff -= 1
                elif x_squares[i] == game.player2.label:
                    corner_diff += 1

        mobility = len(game._get_valid_moves(board, state.player2.label))   # Opponent's mobility

        result = 4 * corner_diff - mobility

        if state.is_player_max(state.player1):
            return result
        elif state.is_player_max(state.player2):
            return - result
        else:
            raise ValueError('Player MAX not defined')
Beispiel #4
0
 def evaluate(self, state: TwoPlayerGameState) -> float:
     """ Minimizes captures to limit opponent's mobility """
     coins_taken = state.scores[1] - state.parent.scores[1]
     if state.is_player_max(state.player1):
         return -coins_taken
     if state.is_player_max(state.player2):
         return coins_taken
     else:
         raise ValueError('Player MAX not defined')
Beispiel #5
0
 def evaluate(self, state: TwoPlayerGameState) -> float:
     game, board = state.game, state.board
     utility = 0.4 * game._coin_diff(board) + 0.3 * game._choice_diff(
         board) + 0.3 * game._corner_diff(board)
     if state.is_player_max(state.player1):
         return -utility / 100
     if state.is_player_max(state.player2):
         return utility / 100
     else:
         raise ValueError('Player MAX not defined')
Beispiel #6
0
def general_evaluation_function(state: TwoPlayerGameState, func: Callable[[TwoPlayerGameState], float]) -> float:
    if state.end_of_game:
        scores = state.scores
        score_difference = scores[0] - scores[1]
        if state.is_player_max(state.player1):
            return score_difference
        if state.is_player_max(state.player2):
            return - score_difference
        else:
            raise ValueError('Player MAX not defined')

    return func(state)
Beispiel #7
0
    def evaluate(self, state: TwoPlayerGameState) -> float:
        score = 0
        for pos in state.board:
            if pos not in state.parent.board:  # i. e. the move played
                if pos in X_POSITIONS:
                    score = -1
                if pos in C_POSITIONS:
                    score = -.5
                break

        if state.is_player_max(state.player1):
            return score / 2
        if state.is_player_max(state.player2):
            return -score / 2
        else:
            raise ValueError('Player MAX not defined')
Beispiel #8
0
 def evaluate(self, state: TwoPlayerGameState) -> float:
     """Evaluate a state."""
     # Prevent modifications of the state.
     # Deep copy everything, except attributes related
     # to graphical display.
     state_copy = state.clone()
     return self.evaluation_function(state_copy)
Beispiel #9
0
def simple_evaluation_function(state: TwoPlayerGameState) -> float:
    """Return a random value, except for terminal game states."""
    state_value = 2 * np.random.rand() - 1
    if state.end_of_game:
        scores = state.scores
        # Evaluation of the state from the point of view of MAX

        assert isinstance(scores, (Sequence, np.ndarray))
        score_difference = scores[0] - scores[1]

        if state.is_player_max(state.player1):
            state_value = score_difference
        elif state.is_player_max(state.player2):
            state_value = -score_difference
        else:
            raise ValueError('Player MAX not defined')

    return state_value
def evaluation_function(state: TwoPlayerGameState) -> float:
    scores = state.scores

    score_difference = scores[0] - scores[1]
    score_sum = scores[0] + scores[1]

    if state.end_of_game:
        if state.is_player_max(state.player1):
            state_value = score_difference
        elif state.is_player_max(state.player2):
            state_value = -score_difference
        else:
            raise ValueError('Player MAX not defined')
    else:
        # Calculamos el momento de la partida
        stage = ((state.game.height * state.game.width) - 4) / 60
        stab = stability(state)
        mob = mobility(state)
        cor = corner(state)
        score = 100 * score_difference / score_sum
        # Early game
        if score_sum <= (stage * 20):
            num = (45 * stab + 30 * mob + 5 * cor + 20 * score)
            if state.is_player_max(state.player1):
                state_value = num
            elif state.is_player_max(state.player2):
                state_value = -num
        # Late game
        elif score_sum >= (stage * 54):
            num = (20 * stab + 5 * mob + 30 * cor + 45 * score)
            if state.is_player_max(state.player1):
                state_value = num
            elif state.is_player_max(state.player2):
                state_value = -num
        # Mid game
        else:
            num = (30 * stab + 20 * mob + 45 * cor + 5 * score)
            if state.is_player_max(state.player1):
                state_value = num
            elif state.is_player_max(state.player2):
                state_value = -num

    return state_value
Beispiel #11
0
def create_match(player1: Player, player2: Player) -> TwoPlayerMatch:

    dim_board = 8

    initial_board = None
    initial_player = player1

    game = Reversi(player1=player1,
                   player2=player2,
                   height=dim_board,
                   width=dim_board)

    game_state = TwoPlayerGameState(
        game=game,
        board=initial_board,
        initial_player=initial_player,
    )

    return TwoPlayerMatch(game_state, max_sec_per_move=5, gui=False)
def create_match(player1: Player, player2: Player) -> TwoPlayerMatch:

    dim_board = 3

    initial_board = np.zeros((dim_board, dim_board))
    initial_player = player1

    game = TicTacToe(
        player1=player1,
        player2=player2,
        dim_board=dim_board,
    )

    game_state = TwoPlayerGameState(
        game=game,
        board=initial_board,
        initial_player=initial_player,
    )

    return TwoPlayerMatch(game_state, max_sec_per_move=1000, gui=False)
Beispiel #13
0
    def generate_successors(
        self,
        state: TwoPlayerGameState,
    ) -> List[TwoPlayerGameState]:
        """Generate the list of successors of a game state."""
        successors = []

        n_rows, n_columns = np.shape(state.board)
        for i in range(n_rows):
            for j in range(n_columns):
                if (state.board[i, j] == 0):
                    # Prevent modification of the board
                    board_successor = copy.deepcopy(state.board)
                    assert isinstance(state.next_player, Player)
                    board_successor[i, j] = state.next_player.label
                    move_code = self._matrix_to_display_coordinates(i, j)
                    successor = state.generate_successor(
                        board_successor,
                        move_code,
                    )

                    successors.append(successor)

        return successors
Beispiel #14
0
    except ValueError:
        raise ValueError('Wrong configuration of the board')
    else:
        print("Successfully initialised board from array")

# Initialize a reversi game.
game = Reversi(
    player1=player_a,
    player2=player_b,
    height=height,
    width=width,
)

# Initialize a game state.
game_state = TwoPlayerGameState(
    game=game,
    board=initial_board,
    initial_player=initial_player,
)

# Initialize a match.
match = TwoPlayerMatch(
    game_state,
    max_sec_per_move=1000,
    gui=True,
)

# Play match
scores = match.play_match()
input('Press any key to finish.')
    def evaluate(self, state: TwoPlayerGameState) -> float:
        if isinstance(state.board, dict):
            board = state.board
        else:
            board = reversi.from_array_to_dictionary_board(state.board)

        count = 0

        # Inferior edge
        c = board.get((1, 1))
        ini = c
        if c is None:
            count -= get_sign(board.get((2, 2)), state) * 2
        else:
            count += get_sign(c, state) * 4

        inc, nxt_streak, n_streak = edge(board, state, c, 1, False, False)
        count += inc

        # Right edge
        c = board.get((8, 1))
        if c is not None and c == nxt_streak:
            count += get_sign(c, state) * n_streak * 3

        if c is None:
            count -= get_sign(board.get((7, 2)), state) * 2
        else:
            count += get_sign(c, state) * 4

        inc, nxt_streak, n_streak = edge(board, state, c, 8, False, True)
        count += inc

        # Superior edge
        c = board.get((8, 8))
        if c is not None and c == nxt_streak:
            count += get_sign(c, state) * n_streak * 3

        if c is None:
            count -= get_sign(board.get((7, 7)), state) * 2
        else:
            count += get_sign(c, state) * 4

        inc, nxt_streak, n_streak = edge(board, state, c, 8, True, False)
        count += inc

        # Left edge
        c = board.get((1, 8))
        if c is not None and c == nxt_streak:
            count += get_sign(c, state) * n_streak * 3

        if c is None:
            count -= get_sign(board.get((2, 7)), state) * 2
        else:
            count += get_sign(c, state) * 4

        inc, nxt_streak, n_streak = edge(board, state, c, 1, True, True)
        count += inc
        if nxt_streak is not None and nxt_streak == ini:
            count += get_sign(c, state) * n_streak * 3

        if state.is_player_max(state.player1):
            return count
        elif state.is_player_max(state.player2):
            return -count
        else:
            raise ValueError('Player MAX not defined')
Beispiel #16
0
    def evaluate(self, state: TwoPlayerGameState) -> float:
        if isinstance(state.board, dict):
            board = state.board
        else:
            board = reversi.from_array_to_dictionary_board(state.board)

        count = 0

        # Inferior edge
        n_streak = 0
        nxt_streak = None

        c = board.get((1, 1))
        ini = c
        if c is None:
            count -= get_sign(board.get((2, 2)), state) * 2
            cur_streak = None
        else:
            count += get_sign(c, state) * 4
            cur_streak = c

        for i in range(2, 8):
            c = board.get((i, 1))
            count += get_sign(c, state)
            if c == cur_streak and c is not None:
                n_streak += 1
            else:
                if cur_streak is not None:
                    count += get_sign(cur_streak, state) * n_streak * 3
                cur_streak = None

            if cur_streak is None:
                if c == nxt_streak and c is not None:
                    n_streak += 1
                else:
                    nxt_streak = c
                    n_streak = 1

        if cur_streak is not None:
            count += get_sign(c, state) * n_streak * 3

        # Right edge
        c = board.get((8, 1))
        if c is not None and c == nxt_streak:
            count += get_sign(c, state) * n_streak * 3

        cur_streak = c
        nxt_streak = None
        n_streak = 0
        if c is None:
            count -= get_sign(board.get((7, 2)), state) * 2
        else:
            count += get_sign(c, state) * 4

        for i in range(2, 8):
            c = board.get((8, i))
            count += get_sign(c, state)
            if c == cur_streak and c is not None:
                n_streak += 1
            else:
                if cur_streak is not None:
                    count += get_sign(cur_streak, state) * n_streak * 3
                cur_streak = None

            if cur_streak is None:
                if c == nxt_streak and c is not None:
                    n_streak += 1
                else:
                    nxt_streak = c
                    n_streak = 1

        if cur_streak is not None:
            count += get_sign(c, state) * n_streak * 3

        # Superior edge
        c = board.get((8, 8))
        if c is not None and c == nxt_streak:
            count += get_sign(c, state) * n_streak * 3

        cur_streak = c
        nxt_streak = None
        n_streak = 0
        if c is None:
            count -= get_sign(board.get((7, 7)), state) * 2
        else:
            count += get_sign(c, state) * 4

        for i in range(1, 7):
            c = board.get((8 - i, 8))
            count += get_sign(c, state)
            if c == cur_streak and c is not None:
                n_streak += 1
            else:
                if cur_streak is not None:
                    count += get_sign(cur_streak, state) * n_streak * 3
                cur_streak = None

            if cur_streak is None:
                if c == nxt_streak and c is not None:
                    n_streak += 1
                else:
                    nxt_streak = c
                    n_streak = 1

        if cur_streak is not None:
            count += get_sign(c, state) * n_streak * 3

        # Left edge
        c = board.get((1, 8))
        if c is not None and c == nxt_streak:
            count += get_sign(c, state) * n_streak * 3

        cur_streak = c
        nxt_streak = None
        n_streak = 0
        if c is None:
            count -= get_sign(board.get((2, 7)), state) * 2
        else:
            count += get_sign(c, state) * 4

        for i in range(1, 7):
            c = board.get((1, 8 - i))
            count += get_sign(c, state)
            if c == cur_streak and c is not None:
                n_streak += 1
            else:
                if cur_streak is not None:
                    count += get_sign(cur_streak, state) * n_streak * 3
                cur_streak = None

            if cur_streak is None:
                if c == nxt_streak and c is not None:
                    n_streak += 1
                else:
                    nxt_streak = c
                    n_streak = 1

        if cur_streak is not None:
            count += get_sign(c, state) * n_streak * 3
        if nxt_streak is not None and nxt_streak == ini:
            count += get_sign(c, state) * n_streak * 3

        if state.is_player_max(state.player1):
            return count
        elif state.is_player_max(state.player2):
            return - count
        else:
            raise ValueError('Player MAX not defined')