示例#1
0
def get_horiz_vert(
    position: BoardPosition,
    ally_positions: List[BoardPosition],
    enemy_positions: List[BoardPosition],
):
    """Get possible horizontal and vertical positions given a position and the enemies and allies."""
    moves = []
    right_ok = True
    left_ok = True
    down_ok = True
    up_ok = True

    for offset in range(1, 8):
        # Right
        if right_ok and position.check_valid(offset, 0):
            m = position.get_offset(offset, 0)
            if m in ally_positions:
                right_ok = False
            elif m in enemy_positions:
                right_ok = False
                moves.append(m)
            else:
                moves.append(m)

        # Left
        if left_ok and position.check_valid(-offset, 0):
            m = position.get_offset(-offset, 0)
            if m in ally_positions:
                left_ok = False
            elif m in enemy_positions:
                left_ok = False
                moves.append(m)
            else:
                moves.append(m)

        # Up
        if up_ok and position.check_valid(0, offset):
            m = position.get_offset(0, offset)
            if m in ally_positions:
                up_ok = False
            elif m in enemy_positions:
                up_ok = False
                moves.append(m)
            else:
                moves.append(m)

        # Down
        if down_ok and position.check_valid(0, -offset):
            m = position.get_offset(0, -offset)
            if m in ally_positions:
                down_ok = False
            elif m in enemy_positions:
                down_ok = False
                moves.append(m)
            else:
                moves.append(m)

    return moves
示例#2
0
def get_diag(
    position: BoardPosition,
    ally_positions: List[BoardPosition],
    enemy_positions: List[BoardPosition],
):
    """Get possible diagonal positions given a position and the enemies and allies."""
    moves = []
    ul_ok = True
    ur_ok = True
    dl_ok = True
    dr_ok = True

    for offset in range(1, 8):
        # Up and right
        if ur_ok and position.check_valid(offset, offset):
            m = position.get_offset(offset, offset)
            if m in ally_positions:
                ur_ok = False
            elif m in enemy_positions:
                ur_ok = False
                moves.append(m)
            else:
                moves.append(m)

        # Up and left
        if ul_ok and position.check_valid(-offset, offset):
            m = position.get_offset(-offset, offset)
            if m in ally_positions:
                ul_ok = False
            elif m in enemy_positions:
                ul_ok = False
                moves.append(m)
            else:
                moves.append(m)

        # Down and right
        if dr_ok and position.check_valid(offset, -offset):
            m = position.get_offset(offset, -offset)
            if m in ally_positions:
                dr_ok = False
            elif m in enemy_positions:
                dr_ok = False
                moves.append(m)
            else:
                moves.append(m)

        # Down
        if dl_ok and position.check_valid(-offset, -offset):
            m = position.get_offset(-offset, -offset)
            if m in ally_positions:
                dl_ok = False
            elif m in enemy_positions:
                dl_ok = False
                moves.append(m)
            else:
                moves.append(m)

    return moves
示例#3
0
    def init_pieces(self):
        order = PIECE_ORDER if self.side == Side.WHITE else reversed(
            PIECE_ORDER)
        for col, piece_cls in enumerate(order):
            row_idx = 0 if self.side == Side.WHITE else 7
            piece = piece_cls(self.side,
                              BoardPosition(col, row_idx),
                              scale=CHARACTER_SCALING)
            self.pieces.append(piece)

            # Add a pawn
            row_idx = 1 if self.side == Side.WHITE else 6
            pawn = Pawn(self.side,
                        BoardPosition(col, row_idx),
                        scale=CHARACTER_SCALING)
            self.pieces.append(pawn)
示例#4
0
文件: views.py 项目: akern40/PyChess
    def on_mouse_press(self, x: float, y: float, button: int, _modifiers: int):
        if button != arcade.MOUSE_BUTTON_LEFT:
            return

        current_player = self.white_player if self.white_turn else self.black_player
        opponent = self.black_player if self.white_turn else self.white_player

        position = BoardPosition.get_from_pixels(x, y)
        if position.check_valid(0, 0):
            change_turn = current_player.update(position, opponent)
            if change_turn:
                self.white_turn = not self.white_turn
示例#5
0
 def __init__(self, side: Side, board_position: BoardPosition,
              filename: str, **kwargs):
     center_x, center_y = board_position.get_center()
     kwargs.pop("center_x", None)
     kwargs.pop("center_y", None)
     super().__init__(filename,
                      center_x=center_x,
                      center_y=center_y,
                      **kwargs)
     self.side = side
     self.board_position = board_position
     self.letter = ""
示例#6
0
文件: views.py 项目: akern40/PyChess
    def draw_board(self):
        """Draw the underlying board."""
        arcade.draw_lrtb_rectangle_outline(
            0,
            SCREEN_WIDTH - WIDTH_BUFFER,
            SCREEN_HEIGHT,
            0,
            arcade.csscolor.BLACK,
            border_width=10,
        )

        current_player = self.white_player if self.white_turn else self.black_player
        opponent = self.black_player if self.white_turn else self.white_player

        color_white = False
        for row in range(8):
            for col in range(8):
                position = BoardPosition(col, row)

                # Get color based on boolean
                if current_player.selected_piece is not None and (
                        current_player.selected_piece.board_position
                        == position or position
                        in current_player.selected_piece.get_possible_moves(
                            current_player.pieces,
                            opponent.pieces,
                            get_en_passant_position(opponent),
                        )):
                    color = OFFWHITE_COLOR if color_white else OFFBLACK_COLOR
                else:
                    color = WHITE_COLOR if color_white else BLACK_COLOR

                # Draw a filled rectangle
                arcade.draw_lrtb_rectangle_filled(
                    position.left,
                    position.right,
                    position.top,
                    position.bot,
                    color,
                )
                # Switch color based on column
                color_white = not color_white
            # Switch starting color based on row
            color_white = not color_white
示例#7
0
 def set_board_position(self, board_position: BoardPosition):
     self.center_x, self.center_y = board_position.get_center()
     self.board_position = board_position