Example #1
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')
Example #2
0
"""
initial_player = player_a  # Player who moves first.

# Board at an intermediate state of the game.
initial_board = None

# NOTE Uncoment to use standard initial board.
# initial_board = None  # Standard initial board.

if initial_board is None:
    height, width = 8, 8
else:
    height = len(initial_board)
    width = len(initial_board[0])
    try:
        initial_board = from_array_to_dictionary_board(initial_board)
    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(
    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')
Example #4
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')