def captured(board, move, player):
        opponent_pieces = SeegaRules._get_opponent_neighbours(
            board, move, player)
        captured_pieces = list()
        if opponent_pieces:
            for piece in opponent_pieces:
                if piece != (board.board_shape[0] // 2,
                             board.board_shape[1] // 2):
                    if piece[0] == move[0]:
                        if piece[1] < move[1] and 0 <= piece[1] - 1 < board.board_shape[0] and \
                                board.get_cell_color((piece[0], piece[1] - 1)) == Color(player):
                            captured_pieces.append(piece)

                        if piece[1] > move[1] and 0 <= piece[1] + 1 < board.board_shape[0] and \
                                board.get_cell_color((piece[0], piece[1] + 1)) == Color(player):
                            captured_pieces.append(piece)
                    if piece[1] == move[1]:
                        if piece[0] < move[0] and 0 <= piece[0] - 1 < board.board_shape[0] and \
                                board.get_cell_color((piece[0] - 1, piece[1])) == Color(player):
                            captured_pieces.append(piece)
                        if piece[0] > move[0] and 0 <= piece[0] + 1 < board.board_shape[0] and \
                                board.get_cell_color((piece[0] + 1, piece[1])) == Color(player):
                            captured_pieces.append(piece)

        return captured_pieces
Beispiel #2
0
 def __init__(self, node, parent, attrs):
     self.content = ''
     self.node = node
     # note: circular reference
     self.parent = parent
     self._attrs = {}
     for key, value in attrs.items():
         if key in ('x', 'xpadding', 'y', 'ypadding'):
             value = int(value)
         elif key == 'width' and not value.endswith('%'):
             x1 = int(attrs.get('x', 0))
             x2 = int(attrs.get('x', 0)) + int(value)
             value = x2 - x1
         elif key == 'height' and not value.endswith('%'):
             y1 = int(attrs.get('y', 0))
             y2 = int(attrs.get('y', 0)) + int(value)
             value = y2 - y1
         elif key in ('radius', 'size', 'spacing'):
             value = int(value)
         elif key.find('color') != -1:
             value = Color(value)
         elif key.find('font') != -1:
             value = Font(value)
             value.size = int(value.size)
         self._attrs[str(key).replace('-', '_')] = value
     self._children = []
Beispiel #3
0
    def get_player_actions(state, player, reward_move=False):
        """Provide for a player and at a state all of his possible actions.

        Args:
            state (YoteState): A state object from the yote game.
            player (int): The number of the player making the move.
            reward_move (bool, optional): True if the move is a stealing move. Defaults to False.

        Returns:
            List[YoteAction]: Contains all possible actions for a player at the given state.
        """

        actions = []
        board = state.get_board()
        empty_cells = board.get_all_empty_cells()
        opponent_pieces = board.get_player_pieces_on_board(Color(player * -1))

        if reward_move:
            for piece in opponent_pieces:
                actions.append(YoteAction(action_type=YoteActionType.STEAL_FROM_BOARD, at=piece))
            if state.in_hand[player * -1] > 0:
                actions.append(YoteAction(action_type=YoteActionType.STEAL_FROM_HAND))
            return actions
        else:
            if empty_cells and state.in_hand[player] > 0:
                for cell in empty_cells:
                    actions.append(YoteAction(action_type=YoteActionType.ADD, to=cell))
            player_pieces = board.get_player_pieces_on_board(Color(player))
            for piece in player_pieces:
                moves = YoteRules.get_effective_cell_moves(state, piece, player)
                if moves:
                    for move in moves:
                        actions.append(YoteAction(action_type=YoteActionType.MOVE, at=piece, to=move))
            return actions
Beispiel #4
0
 def __init__(self, node, parent, attrs):
     self.content = ''
     self.node = node
     # note: circular reference
     self.parent = parent
     self._attrs = {}
     for key, value in attrs.items():
         if key in ('x', 'xpadding', 'y', 'ypadding'):
             value = int(value)
         elif key == 'width' and not value.endswith('%'):
             x1 = int(attrs.get('x', 0))
             x2 = int(attrs.get('x', 0)) + int(value)
             value = x2 - x1
         elif key == 'height' and not value.endswith('%'):
             y1 = int(attrs.get('y', 0))
             y2 = int(attrs.get('y', 0)) + int(value)
             value = y2 - y1
         elif key in ('radius', 'size', 'spacing'):
             value = int(value)
         elif key.find('color') != -1:
             value = Color(value)
         elif key.find('font') != -1:
             value = Font(value)
             value.size = int(value.size)
         self._attrs[str(key).replace('-', '_')] = value
     self._children = []
    def make_move(state, action, player):

        # they are in the state object
        """Transform the action of the player to a move. The move is made and the reward computed.

        Args:
            state (YoteState): A state object from the yote game.
            action (Action): An action object containing the move.
            player (int): The number of the player making the move.
            rewarding_move (bool, optional): True if the move is a stealing move. Defaults to False.

        Returns: (next_state, done, next_is_reward): Gives the next state of the game along with the game status and
        the type of the next step.
        """

        board = state.get_board()
        json_action = action.get_json_action()
        action = action.get_action_as_dict()
        phase = state.phase
        reward = 0

        if phase == 1 and action['action_type'] == SeegaActionType.ADD:
            state.in_hand[player] -= 1
            board.fill_cell(action['action']['to'], Color(player))

        elif phase == 2 and action['action_type'] == SeegaActionType.MOVE:
            at = action['action']['at']
            to = action['action']['to']
            captured_pieces = SeegaRules.captured(board, to, player)
            state.captured = captured_pieces
            board.empty_cell(at)
            board.fill_cell(to, Color(player))
            if captured_pieces:
                reward = len(captured_pieces)
                state.boring_moves = 0
                for piece in captured_pieces:
                    board.empty_cell(piece)
            else:
                state.boring_moves += 1

        state.set_board(board)
        state.score[player] += reward
        state.set_latest_player(player)
        state.set_latest_move(json_action)
        if phase == 1:
            if state.in_hand[player] == 0 and state.in_hand[player * -1] == 0:
                state.set_next_player(player)
                state.phase = 2
            elif state.in_hand[player] != 0 and state.in_hand[player] % 2 != 0:
                state.set_next_player(player)
            elif state.in_hand[player] % 2 == 0:
                state.set_next_player(player * -1)
        elif phase == 2:
            if SeegaRules.is_player_stuck(state, player * -1):
                state.set_next_player(player)
            else:
                state.set_next_player(player * -1)

        done = SeegaRules.is_end_game(state)
        return state, done
Beispiel #6
0
    def steal(self, state):
        board = state.get_board()
        empty_cells = board.get_all_empty_cells()
        me = board.get_player_pieces_on_board(Color(self.position))
        opponents = board.get_player_pieces_on_board(Color(-self.position))
        latest_move = state.get_latest_move()
        prior_cell = latest_move["action"]["to"]

        if len(opponents) > 0:
            # Protect just moved piece
            if (prior_cell[0] - 1, prior_cell[1]) in opponents:
                if (prior_cell[0] + 1, prior_cell[1]) in empty_cells:
                    return YoteAction(
                        action_type=YoteActionType.STEAL_FROM_BOARD,
                        at=(prior_cell[0] - 1, prior_cell[1]),
                    )
            if (prior_cell[0] + 1, prior_cell[1]) in opponents:
                if (prior_cell[0] - 1, prior_cell[1]) in empty_cells:
                    return YoteAction(
                        action_type=YoteActionType.STEAL_FROM_BOARD,
                        at=(prior_cell[0] + 1, prior_cell[1]),
                    )
            if (prior_cell[0], prior_cell[1] - 1) in opponents:
                if (prior_cell[0], prior_cell[1] + 1) in empty_cells:
                    return YoteAction(
                        action_type=YoteActionType.STEAL_FROM_BOARD,
                        at=(prior_cell[0], prior_cell[1] - 1),
                    )
            if (prior_cell[0], prior_cell[1] + 1) in opponents:
                if (prior_cell[0], prior_cell[1] - 1) in empty_cells:
                    return YoteAction(
                        action_type=YoteActionType.STEAL_FROM_BOARD,
                        at=(prior_cell[0], prior_cell[1] + 1),
                    )

            for cell in opponents:  # Protect the other pieces
                if (((cell[0] - 1, cell[1]) in me and
                     (cell[0] - 2, cell[1]) in empty_cells)
                        or ((cell[0] + 1, cell[1]) in me and
                            (cell[0] + 2, cell[1]) in empty_cells)
                        or ((cell[0], cell[1] - 1) in me and
                            (cell[0], cell[1] - 2) in empty_cells)
                        or ((cell[0], cell[1] + 1) in me and
                            (cell[0], cell[1] + 2) in empty_cells)):
                    return YoteAction(
                        action_type=YoteActionType.STEAL_FROM_BOARD, at=cell)
            return YoteAction(action_type=YoteActionType.STEAL_FROM_BOARD,
                              at=opponents[0])

        else:
            return YoteAction(action_type=YoteActionType.STEAL_FROM_HAND)
    def get_player_actions(state, player):
        """Provide for a player and at a state all of his possible actions.

        Args:
            state (YoteState): A state object from the yote game.
            player (int, optional): True if the move is a stealing move. Defaults to False.

        Returns:
            List[YoteAction]: Contains all possible actions for a player at the given state.
        """

        actions = []
        phase = state.phase
        board = state.get_board()

        if phase == 1:
            empty_cells = board.get_all_empty_cells_without_center()
            if empty_cells and state.in_hand[player]:
                for cell in empty_cells:
                    actions.append(
                        SeegaAction(action_type=SeegaActionType.ADD, to=cell))
            return actions
        elif phase == 2:
            player_pieces = board.get_player_pieces_on_board(Color(player))
            for piece in player_pieces:
                moves = SeegaRules.get_effective_cell_moves(state, piece)
                if moves:
                    for move in moves:
                        actions.append(
                            SeegaAction(action_type=SeegaActionType.MOVE,
                                        at=piece,
                                        to=move))
            return actions
    def __init__(self, color):
        super(AI, self).__init__(color)
        self.ME = color.value
        self.ME_color = color
        self.OTHER = -color.value
        self.OTHER_color = Color(-color.value)

        self.max_time = None
        self.remaining_time = None
        self.typical_time = None

        self.move_nb = 0
        self.exploring_depth_limit = 1
        self.absolute_max_depth = 1000

        self.playing_barrier = False
        self.reached_win = False
        self.last_action = None
        self.explored_nodes = 0

        self.state_evaluations = dict(
        )  # {state: (static_eval, dynamic_eval, dynamic_eval_depth)
        self.current_eval = 0

        self.board_preferences = np.array([[60, 50, 99, 50, 60],
                                           [50, 31, 1, 33, 50],
                                           [99, 1, -1, 1, 99],
                                           [50, 11, 1, 13, 50],
                                           [60, 50, 99, 50, 60]])[::-1]
        self.highest_values_indices = [
            np.unravel_index(i, self.board_preferences.shape)
            for i in np.argsort(self.board_preferences, axis=None)
        ][::-1]
Beispiel #9
0
    def generate_bars(self, sizes):
        """
        creates the bars if they aren't already, else updates them

        :param sizes: sizes of bars
        :return:
        """
        bar_width = self.surface.get_rect().size[0] / self.size

        for i, y in enumerate(sizes):

            try:
                bar = self.bars[y]
                bar.position = Vector2D.custom(self.surface,
                                               i * bar_width,
                                               y - 1,
                                               inverty=True)
                continue
            except KeyError:
                bar = Rectangle(Vector2D.custom(self.surface,
                                                i * bar_width,
                                                y - 1,
                                                inverty=True),
                                Vector2D(bar_width, y),
                                color=Color.lerp(y / self.max, colors.RED,
                                                 colors.GREEN, colors.BLUE)
                                if self.color is None else self.color)

                self.bars[y] = bar
Beispiel #10
0
    def is_legal_move(state, action, player):

        phase = state.phase
        action = action.get_action_as_dict()

        if state.get_next_player() == player:
            if phase == 1:
                if action[
                        'action_type'] == SeegaActionType.ADD and state.in_hand[
                            player]:
                    empty_cells = state.get_board().get_all_empty_cells()
                    if empty_cells and action['action']['to'] in empty_cells \
                            and not state.get_board().is_center(action['action']['to']):
                        return True
            elif phase == 2:
                if SeegaRules.is_player_stuck(state, player * -1):
                    if action['action'][
                            'at'] in SeegaRules.get_unstucked_piece(
                                state, player * -1):
                        return True
                    return False
                if state.get_next_player() == player:
                    if action['action_type'] == SeegaActionType.MOVE:
                        if state.get_board().get_cell_color(
                                action['action']['at']) == Color(player):
                            effective_moves = SeegaRules.get_effective_cell_moves(
                                state, action['action']['at'])
                            if effective_moves and action['action'][
                                    'to'] in effective_moves:
                                return True
                    return False
            return False
        return False
Beispiel #11
0
    def get_effective_cell_moves(state, cell, player):
        """Give the effective(Only the possible ones) moves a player can make regarding a piece on the board.

        Args:
            state (YoteState): The current game state.
            cell ((int, int)): The coordinates of the piece on the board.
            player (int): The number of the player making the move.

        Returns:
            List: A list containing all the coordinates where the piece can go.
        """
        board = state.get_board()
        if board.is_cell_on_board(cell):
            possibles_moves = YoteRules._get_rules_possibles_moves(cell, board.board_shape)
            effective_moves = []
            i, j = cell
            for move in possibles_moves:
                if board.is_empty_cell(move):
                    effective_moves.append(move)
                elif board.get_cell_color(move) == Color(player * -1):
                    k, l = move
                    if i == k and j < l and board.is_empty_cell((i, j + 2)):
                        effective_moves.append((i, j + 2))
                    elif i == k and l < j and board.is_empty_cell((i, j - 2)):
                        effective_moves.append((i, j - 2))
                    elif j == l and i < k and board.is_empty_cell((i + 2, j)):
                        effective_moves.append((i + 2, j))
                    elif j == l and k < i and board.is_empty_cell((i - 2, j)):
                        effective_moves.append((i - 2, j))
            return effective_moves
Beispiel #12
0
    def _get_opponent_neighbours(board, cell, player):

        possibles_neighbours = SeegaRules._get_rules_possibles_moves(cell, board.board_shape)
        enemies = list()
        for neighbour in possibles_neighbours:
            if board.get_cell_color(neighbour) == Color(player * -1):
                enemies.append(neighbour)
        return enemies
Beispiel #13
0
 def get_unstucked_piece(state, player):
     board = state.get_board()
     player_pieces = board.get_player_pieces_on_board(Color(player))
     piece_to_move = set()
     for piece in player_pieces:
         opponent_pieces = SeegaRules._get_opponent_neighbours(board, piece, player)
         if len(opponent_pieces) > 0:
             for opp_piece in opponent_pieces:
                 if len(SeegaRules.get_effective_cell_moves(state, opp_piece)) > 0:
                     piece_to_move.add(opp_piece)
     return list(piece_to_move)
Beispiel #14
0
    def is_legal_move(state, action, player, rewarding_move=False):  # TODO: Update this function to an more
        # optimized one.
        """Check if an action is a legal move.

        Args:
            state (YoteState): A state object from the yote game.
            action (Action): An action object containing the move.
            player (int): The number of the player making the move.
            rewarding_move (bool, optional): True if the move is a stealing move. Defaults to False.

        Returns:
            bool: True is the move is a legal one and False if else.
        """
        action = action.get_action_as_dict()
        if rewarding_move:
            if player == state.get_next_player() == state.get_latest_player():
                if action['action_type'] == YoteActionType.STEAL_FROM_HAND and state.in_hand[player * -1] > 0:
                    return True
                elif action['action_type'] == YoteActionType.STEAL_FROM_BOARD:
                    opponent_piece = state.get_board().get_player_pieces_on_board(Color(player * -1))
                    if opponent_piece and action['action']['at'] in opponent_piece:
                        return True
            return False
        else:
            if state.get_next_player() == player:
                if action['action_type'] == YoteActionType.ADD and state.in_hand[player] > 0:
                    empty_cells = state.get_board().get_all_empty_cells()
                    if empty_cells and action['action']['to'] in empty_cells:
                        return True
                elif action['action_type'] == YoteActionType.MOVE:
                    if state.get_board().get_cell_color(action['action']['at']) == Color(player):
                        effective_moves = YoteRules.get_effective_cell_moves(state, action['action']['at'], player)
                        if effective_moves and action['action']['to'] in effective_moves:
                            return True
                return False
            return False
Beispiel #15
0
    def bytesToBitmap(self, palette_b: bytes, icon_b: bytes) -> Image.Image:
        palette: list[Color] = Color.BGR555ToColors(palette_b)
        icon_data: list[int] = BytesHelper.bytesToBit4(icon_b)
        target: Image.Image = Image.new('RGB', (32, 32))
        pixels = target.load()
        pixel_i: int = 0
        for pixel_y in range(4):
            for pixel_x in range(4):
                for chunk_y in range(8):
                    for chunk_x in range(8):
                        pixels[chunk_x + pixel_x * 8,
                               chunk_y + pixel_y * 8] = palette[
                                   icon_data[pixel_i]].toTupleNoAlpha()
                        pixel_i += 1

        return target
Beispiel #16
0
# change this as necessary to change sorting algorithm
# sorta = CocktailSort(bars.sizes[:])
# sorta = InsertionSort(bars.sizes[:])
# sorta = CycleSort(bars.sizes[:])
# sorta = QuickSort(bars.sizes[:], 0, len(bars.sizes) - 1)
sorta = algorithms.MergeSort(bars.sizes[:])

# limit queue size to be safe
ac = AlgorithmController(sorta, maxsize=10000)
ac.start()

# button to control algorithm flow
flip_button = Button(Text('', color=colors.WHITE),
                     size=Vector2D(70, 25),
                     color=Color(0, 0, 0, 0),
                     onclick=lambda _: should_sort.flip())
flip_button.position = Vector2D(
    Vector2D.center(screen.get_rect(),
                    screen.get_rect().size).x - (flip_button.size.x / 2), 0)
flip_button.onhover = Hover(BLACK_TEXT_WHITE_BACKGROUND,
                            WHITE_TEXT_TRANSPARENT_BACKGROUND)


def fbflip(val):
    flip_button.text.text = 'RUNNING' if val else 'STOPPED'


fbflip(should_sort.get())
should_sort.on_flip = fbflip
manager = WidgetManager([flip_button])
Beispiel #17
0
    def provide(self, state):
        board = state.get_board()
        empty_cells = board.get_all_empty_cells()
        me = board.get_player_pieces_on_board(Color(self.position))
        opponents = board.get_player_pieces_on_board(Color(-self.position))
        available_moves = YoteRules.get_player_actions(state, self.position)

        def is_dangerous_cell(cell):
            if ((cell[0] - 1, cell[1]) in opponents and
                (cell[0] + 1, cell[1]) in empty_cells
                    or (cell[0] + 1, cell[1]) in opponents and
                (cell[0] - 1, cell[1]) in empty_cells
                    or (cell[0], cell[1] - 1) in opponents and
                (cell[0], cell[1] + 1) in empty_cells
                    or (cell[0], cell[1] + 1) in opponents and
                (cell[0], cell[1] - 1) in empty_cells):
                return True
            else:
                return False

        if state.get_latest_player() == self.position:
            return self.steal(state)

        for cell in me:  # Just take the opponents piece
            if (cell[0] - 1, cell[1]) in opponents:
                if (cell[0] - 2, cell[1]) in empty_cells:
                    return YoteAction(
                        action_type=YoteActionType.MOVE,
                        at=cell,
                        to=(cell[0] - 2, cell[1]),
                    )
            if (cell[0] + 1, cell[1]) in opponents:
                if (cell[0] + 2, cell[1]) in empty_cells:
                    return YoteAction(
                        action_type=YoteActionType.MOVE,
                        at=cell,
                        to=(cell[0] + 2, cell[1]),
                    )
            if (cell[0], cell[1] - 1) in opponents:
                if (cell[0], cell[1] - 2) in empty_cells:
                    return YoteAction(
                        action_type=YoteActionType.MOVE,
                        at=cell,
                        to=(cell[0], cell[1] - 2),
                    )
            if (cell[0], cell[1] + 1) in opponents:
                if (cell[0], cell[1] + 2) in empty_cells:
                    return YoteAction(
                        action_type=YoteActionType.MOVE,
                        at=cell,
                        to=(cell[0], cell[1] + 2),
                    )

        for cell in me:  #  just run away
            if (cell[0], cell[1] - 1) in opponents:
                if ((cell[0], cell[1] + 1) in empty_cells and
                    (cell[0], cell[1] + 2) not in opponents and not (
                        (cell[0] - 1, cell[1] + 1) in opponents
                        and cell[0] + 1,
                        cell[1] + 1,
                    ) in empty_cells
                        or (cell[0] + 1, cell[1] + 1) in opponents and
                    (cell[0] - 1, cell[1] + 1) in empty_cells):
                    return YoteAction(
                        action_type=YoteActionType.MOVE,
                        at=cell,
                        to=(cell[0], cell[1] + 1),
                    )
            if (cell[0] - 1, cell[1]) in opponents:
                if ((cell[0] + 1, cell[1]) in empty_cells and
                    (cell[0] + 2, cell[1]) not in opponents and not (
                        (cell[0] + 1, cell[1] - 1) in opponents
                        and cell[0] + 1,
                        cell[1] + 1,
                    ) in empty_cells
                        or (cell[0] + 1, cell[1] + 1) in opponents and
                    (cell[0] + 1, cell[1] - 1) in empty_cells):
                    return YoteAction(
                        action_type=YoteActionType.MOVE,
                        at=cell,
                        to=(cell[0] + 1, cell[1]),
                    )
            if (cell[0] + 1, cell[1]) in opponents:
                if ((cell[0] - 1, cell[1]) in empty_cells and
                    (cell[0] - 2, cell[1]) not in opponents and not (
                        (cell[0] - 1, cell[1] - 1) in opponents
                        and cell[0] - 1,
                        cell[1] + 1,
                    ) in empty_cells
                        or (cell[0] - 1, cell[1] + 1) in opponents and
                    (cell[0] - 1, cell[1] - 1) in empty_cells):
                    return YoteAction(
                        action_type=YoteActionType.MOVE,
                        at=cell,
                        to=(cell[0] - 1, cell[1]),
                    )
            if (cell[0], cell[1] + 1) in opponents:
                if ((cell[0], cell[1] - 1) in empty_cells and
                    (cell[0], cell[1] - 2) not in opponents and not (
                        (cell[0] - 1, cell[1] - 1) in opponents
                        and cell[0] + 1,
                        cell[1] - 1,
                    ) in empty_cells
                        or (cell[0] + 1, cell[1] - 1) in opponents and
                    (cell[0] - 1, cell[1] - 1) in empty_cells):
                    return YoteAction(
                        action_type=YoteActionType.MOVE,
                        at=cell,
                        to=(cell[0], cell[1] - 1),
                    )

        for cell in me:
            if (cell[0], cell[1] - 1) in empty_cells and (
                    cell[0],
                    cell[1] - 2,
            ) not in opponents:
                if ((cell[0] + 1, cell[1] - 1) in opponents and
                    (cell[0] - 1, cell[1] - 1) not in empty_cells
                        or (cell[0] - 1, cell[1] - 1) in opponents and
                    (cell[0] + 1, cell[1] - 1) not in empty_cells):
                    return YoteAction(
                        action_type=YoteActionType.MOVE,
                        at=cell,
                        to=(cell[0], cell[1] - 1),
                    )
            if (cell[0], cell[1] + 1) in empty_cells and (
                    cell[0],
                    cell[1] + 2,
            ) not in opponents:
                if ((cell[0] + 1, cell[1] + 1) in opponents and
                    (cell[0] - 1, cell[1] + 1) not in opponents
                        or (cell[0] - 1, cell[1] + 1) in opponents and
                    (cell[0] + 1, cell[1] + 1) not in opponents):
                    return YoteAction(
                        action_type=YoteActionType.MOVE,
                        at=cell,
                        to=(cell[0], cell[1] + 1),
                    )
            if (cell[0] - 1, cell[1]) in empty_cells and (
                    cell[0] - 2,
                    cell[1],
            ) not in opponents:
                if ((cell[0] - 1, cell[1] - 1) in opponents and
                    (cell[0] - 1, cell[1] - 1) not in opponents
                        or (cell[0] - 1, cell[1] + 1) in opponents and
                    (cell[0] - 1, cell[1] - 1) not in opponents):
                    return YoteAction(
                        action_type=YoteActionType.MOVE,
                        at=cell,
                        to=(cell[0] - 1, cell[1]),
                    )
            if (cell[0] + 1, cell[1]) in empty_cells and (
                    cell[0] + 2,
                    cell[1],
            ) not in opponents:
                if ((cell[0] + 1, cell[1] - 1) in opponents and
                    (cell[0] + 1, cell[1] + 1) not in opponents
                        or (cell[0] + 1, cell[1] + 1) in opponents and
                    (cell[0] + 1, cell[1] - 1) not in opponents):
                    return YoteAction(
                        action_type=YoteActionType.MOVE,
                        at=cell,
                        to=(cell[0] + 1, cell[1]),
                    )

        for cell in me:
            if (cell[0], cell[1] - 1) in empty_cells and (
                    cell[0],
                    cell[1] - 2,
            ) not in opponents:
                if ((cell[0], cell[1] - 3) in opponents and
                    (cell[0], cell[1] - 2) not in opponents
                        or (cell[0] + 2, cell[1] - 1) in opponents and
                    (cell[0] + 1, cell[1] - 1) not in opponents
                        or (cell[0] - 2, cell[1] - 1) in opponents and
                    (cell[0] - 1, cell[1] - 1) not in opponents):
                    return YoteAction(
                        action_type=YoteActionType.MOVE,
                        at=cell,
                        to=(cell[0], cell[1] - 1),
                    )
            if (cell[0], cell[1] + 1) in empty_cells and (
                    cell[0],
                    cell[1] + 2,
            ) not in opponents:
                if ((cell[0], cell[1] + 3) in opponents and
                    (cell[0], cell[1] + 2) not in opponents
                        or (cell[0] + 2, cell[1] + 1) in opponents and
                    (cell[0] + 1, cell[1] + 1) not in opponents
                        or (cell[0] - 2, cell[1] + 1) in opponents and
                    (cell[0] - 1, cell[1] + 1) not in opponents):
                    return YoteAction(
                        action_type=YoteActionType.MOVE,
                        at=cell,
                        to=(cell[0], cell[1] + 1),
                    )
            if (cell[0] - 1, cell[1]) in empty_cells and (
                    cell[0] - 2,
                    cell[1],
            ) not in opponents:
                if ((cell[0] - 3, cell[1]) in opponents and
                    (cell[0] - 2, cell[1]) not in opponents
                        or (cell[0] - 1, cell[1] - 2) in opponents and
                    (cell[0] - 1, cell[1] - 1) not in opponents
                        or (cell[0] - 1, cell[1] + 2) in opponents and
                    (cell[0] - 1, cell[1] + 1) not in opponents):
                    return YoteAction(
                        action_type=YoteActionType.MOVE,
                        at=cell,
                        to=(cell[0] - 1, cell[1]),
                    )
            if (cell[0] + 1, cell[1]) in empty_cells and (
                    cell[0] + 2,
                    cell[1],
            ) not in opponents:
                if ((cell[0] + 3, cell[1]) in opponents and
                    (cell[0] + 2, cell[1]) not in opponents
                        or (cell[0] + 1, cell[1] - 2) in opponents and
                    (cell[0] + 1, cell[1] - 1) not in opponents
                        or (cell[0] + 1, cell[1] + 2) in opponents and
                    (cell[0] + 1, cell[1] + 1) not in opponents):
                    return YoteAction(
                        action_type=YoteActionType.MOVE,
                        at=cell,
                        to=(cell[0] + 1, cell[1]),
                    )

        if state.get_player_info(self.position)["in_hand"] > 0:
            return self.add(board)
        else:
            return available_moves[0]
Beispiel #18
0
    def add(self, board):
        empty_cells = board.get_all_empty_cells()
        me = board.get_player_pieces_on_board(Color(self.position))
        opponents = board.get_player_pieces_on_board(Color(-self.position))

        for cell in opponents:  # on board_imit
            if cell[1] - 1 == 0 and (cell[0], cell[1] - 1) in empty_cells:
                return YoteAction(action_type=YoteActionType.ADD,
                                  to=(cell[0], cell[1] - 1))
            elif cell[0] - 1 == 0 and (cell[0] - 1, cell[1]) in empty_cells:
                return YoteAction(action_type=YoteActionType.ADD,
                                  to=(cell[0] - 1, cell[1]))
            elif (cell[0] + 1 == board.board_shape[0]
                  and (cell[0] + 1, cell[1]) in empty_cells):
                return YoteAction(action_type=YoteActionType.ADD,
                                  to=(cell[0] + 1, cell[1]))
            elif (cell[1] + 1 == board.board_shape[1]
                  and (cell[0], cell[1] + 1) in empty_cells):
                return YoteAction(action_type=YoteActionType.ADD,
                                  to=(cell[0], cell[1] + 1))

        for cell in opponents:  # consecutives
            if (cell[0], cell[1] - 2) in me and (cell[0],
                                                 cell[1] - 1) in empty_cells:
                return YoteAction(action_type=YoteActionType.ADD,
                                  to=(cell[0], cell[1] - 1))
            if (cell[0] - 2, cell[1]) in me and (cell[0] - 1,
                                                 cell[1]) in empty_cells:
                return YoteAction(action_type=YoteActionType.ADD,
                                  to=(cell[0] - 1, cell[1]))
            if (cell[0] + 2, cell[1]) in me and (cell[0] + 1,
                                                 cell[1]) in empty_cells:
                return YoteAction(action_type=YoteActionType.ADD,
                                  to=(cell[0] + 1, cell[1]))
            if (cell[0], cell[1] + 2) in me and (cell[0],
                                                 cell[1] + 1) in empty_cells:
                return YoteAction(action_type=YoteActionType.ADD,
                                  to=(cell[0], cell[1] + 1))

        for cell in opponents:  # block
            if ((cell[0], cell[1] - 2) in empty_cells
                    and (cell[0], cell[1] - 1) in empty_cells
                    and not ((cell[0], cell[1] - 3) in opponents)):
                if (cell[0], cell[1] - 2) in empty_cells:
                    return YoteAction(action_type=YoteActionType.ADD,
                                      to=(cell[0], cell[1] - 2))
            if ((cell[0] - 2, cell[1]) in empty_cells
                    and (cell[0] - 1, cell[1]) in empty_cells
                    and not ((cell[0] - 3, cell[1]) not in opponents)):
                if (cell[0] - 2, cell[1]) in empty_cells:
                    return YoteAction(action_type=YoteActionType.ADD,
                                      to=(cell[0] - 2, cell[1]))
            if ((cell[0] + 2, cell[1]) in empty_cells
                    and (cell[0] + 1, cell[1]) in empty_cells
                    and not ((cell[0] + 3, cell[1]) not in opponents)):
                if (cell[0] + 2, cell[1]) in empty_cells:
                    return YoteAction(action_type=YoteActionType.ADD,
                                      to=(cell[0] + 2, cell[1]))
            if ((cell[0], cell[1] + 2) in empty_cells
                    and (cell[0], cell[1] + 1) in empty_cells
                    and not ((cell[0], cell[1] + 3) not in opponents)):
                if (cell[0], cell[1] + 2) in empty_cells:
                    return YoteAction(action_type=YoteActionType.ADD,
                                      to=(cell[0], cell[1] + 2))

        return YoteAction(action_type=YoteActionType.ADD, to=empty_cells[0])
Beispiel #19
0
    def make_move(state, action, player, rewarding_move=False):  # TODO : done and next_is_reward can be removed as
        # they are in the state object
        """Transform the action of the player to a move. The move is made and the reward computed. 

        Args:
            state (YoteState): A state object from the yote game.
            action (Action): An action object containing the move.
            player (int): The number of the player making the move.
            rewarding_move (bool, optional): True if the move is a stealing move. Defaults to False.

        Returns: (next_state, done, next_is_reward): Gives the next state of the game along with the game status and
        the type of the next step.
        """
        board = state.get_board()
        json_action = action.get_json_action()
        action = action.get_action_as_dict()
        captured = None
        reward = 0
        next_is_reward = False
        previous_is_reward = False
        if rewarding_move:
            state.boring_moves = 0
            previous_is_reward = True
            if action['action_type'] == YoteActionType.STEAL_FROM_HAND:
                reward += 1
                state.in_hand[player * -1] -= 1
            elif action['action_type'] == YoteActionType.STEAL_FROM_BOARD:
                board.empty_cell(action['action']['at'])
                reward += 1
        else:
            if action['action_type'] == YoteActionType.ADD:
                state.boring_moves += 1
                state.in_hand[player] -= 1
                board.fill_cell(action['action']['to'], Color(player))
            elif action['action_type'] == YoteActionType.MOVE:
                at = action['action']['at']
                to = action['action']['to']

                def distance(cell_1, cell_2):
                    import math
                    return math.sqrt((cell_1[0] - cell_2[0]) ** 2 + (cell_1[1] - cell_2[1]) ** 2)

                board.empty_cell(at)
                board.fill_cell(to, Color(player))
                if int(distance(at, to)) == 1:
                    state.boring_moves += 1
                elif int(distance(at, to)) > 1:
                    state.boring_moves = 0
                    next_is_reward = True
                    board.fill_cell(to, Color(player))
                    if at[0] == to[0] and at[1] < to[1]:
                        board.empty_cell((at[0], at[1] + 1))
                        captured = (at[0], at[1] + 1)
                    elif at[0] == to[0] and at[1] > to[1]:
                        board.empty_cell((at[0], at[1] - 1))
                        captured = (at[0], at[1] - 1)
                    elif at[1] == to[1] and at[0] < to[0]:
                        board.empty_cell((at[0] + 1, at[1]))
                        captured = (at[0] + 1, at[1])
                    elif at[1] == to[1] and at[0] > to[0]:
                        board.empty_cell((at[0] - 1, at[1]))
                        captured = (at[0] - 1, at[1])
                    reward += 1

        state.set_board(board)
        state.score[player] += reward
        state.captured = captured
        state.rewarding_move = next_is_reward
        state.previous_is_reward = previous_is_reward
        state.set_latest_player(player)
        state.set_latest_move(json_action)
        if next_is_reward:
            state.set_next_player(player)
        else:
            state.set_next_player(player * -1)

        done = YoteRules.is_end_game(state)
        return state, done, next_is_reward
Beispiel #20
0
 def move_piece(self, at, to, player):
     x, y = to[0], to[1]
     self.squares[x][y].set_piece(Piece(player, Color(player).name))
     x, y = at[0], at[1]
     self.squares[x][y].remove_piece()
Beispiel #21
0
def BLACK_TEXT_WHITE_BACKGROUND(button):
    button.mutate_color(Color(255, 255, 255, 255))
    button.text.color = colors.BLACK
Beispiel #22
0
            player_type[i] = player_type[i][:-3]
    agents = {}

    # load the agents
    k = -1
    for i in range(2):
        if player_type[i] != 'human':
            j = player_type[i].rfind('/')
            # extract the dir from the agent
            dir = player_type[i][:j]
            # add the dir to the system path
            sys.path.append(dir)
            # extract the agent filename
            file = player_type[i][j + 1:]
            # create the agent instance
            agents[k] = getattr(__import__(file), 'AI')(Color(k))
            k *= -1
    if None in agents:
        raise Exception('Problems in  AI players instances. \n'
                        'Usage:\n'
                        '-t allowed time for each ai \n'
                        '\t total number of seconds credited to each player \n'
                        '-ai0 ai0_file.py \n'
                        '\t path to the ai that will play as player 0 \n'
                        '-ai1 ai1_file.py\n'
                        '\t path to the ai that will play as player 1 \n'
                        '-s sleep time \n'
                        '\t time(in second) to show the board(or move)')
    game = YoteGUI(app, (5, 6),
                   agents,
                   sleep_time=sleep_time,
Beispiel #23
0
 def add_piece(self, cell, player):
     x, y = cell[0], cell[1]
     self.squares[x][y].set_piece(Piece(player, Color(player).name))