Exemplo n.º 1
0
    def possible_moves(self, pieces):
        possible_moves = []
        possible_moves += self.step_possible(pieces, 1, 0, 1)
        possible_moves += self.step_possible(pieces, -1, 0, 1)
        possible_moves += self.step_possible(pieces, 0, 1, 1)
        possible_moves += self.step_possible(pieces, 0, -1, 1)
        possible_moves += self.step_possible(pieces, 1, 1, 1)
        possible_moves += self.step_possible(pieces, 1, -1, 1)
        possible_moves += self.step_possible(pieces, -1, 1, 1)
        possible_moves += self.step_possible(pieces, -1, -1, 1)

        if not self.has_moved:
            castle_rooks = []
            row = 0 if self.color == PieceColor.BLACK else 7
            for piece in pieces:
                if piece.color == self.color and piece.piece_type == PieceType.ROOK and not piece.has_moved:
                    castle_rooks.append(piece)

            for rook in castle_rooks:
                lowest_column = min(rook.column + 1, self.column + 1)
                highest_column = max(rook.column, self.column)
                possible = True
                for column in range(lowest_column, highest_column):
                    piece = board.get_piece(pieces, board.get_box(row, column))
                    if piece:
                        possible = False
                        break
                    if board.is_chess(
                            board.pieces_after_move(
                                pieces,
                                board.get_move(self,
                                               board.get_box(row, column),
                                               None, None)), self.color):
                        possible = False
                        break

                if possible:
                    direction = 1 if self.column < rook.column else -1
                    extra_move = board.get_move(
                        rook,
                        board.get_box(
                            row, self.column + direction * 2 + direction * -1),
                        None, None)
                    move = board.get_move(
                        self, board.get_box(row, self.column + direction * 2),
                        None, extra_move)
                    possible_moves.append(move)

        return possible_moves
Exemplo n.º 2
0
    def step_possible(self, pieces, column_step_multiplier, row_step_multiplier, step_limit=None, allow_capture=True, allow_non_capture=True):
        moves = []
        while True:
            try_steps = len(moves) + 1
            column = self.column + try_steps * column_step_multiplier
            row = self.row + try_steps * row_step_multiplier
            box = board.get_box(row, column)
            capture = board.get_piece(pieces, box)
            move = board.get_move(self, box, capture, None)

            if not (0 <= row <= 7) or not (0 <= column <= 7):
                break

            piece = board.get_piece(pieces, box)
            if piece and piece.color != self.color and allow_capture:
                moves.append(move)
                break
            if not piece and allow_non_capture:
                moves.append(move)
                if step_limit and len(moves) >= step_limit:
                    break
            else:
                break

        return moves
Exemplo n.º 3
0
 def run(self):
     while True:
         text = input('Click a box: ')
         if len(text) != 2 or not text[0] in columns or not int(
                 text[1]) in list(range(1, 9)):
             print('Invalid box!')
             continue
         row = 8 - int(text[1])
         column = columns.index(text[0])
         box = board.get_box(row, column)
         self.callback(box)
Exemplo n.º 4
0
    def possible_moves(self, pieces):
        direction = -1 if self.color == PieceColor.WHITE else 1
        steps = 2 if not self.has_moved else 1
        moves = []
        moves += self.step_possible(pieces, 0, direction, steps, False)
        moves += self.step_possible(pieces, 1, direction, 1, True, False)
        moves += self.step_possible(pieces, -1, direction, 1, True, False)

        if self.row == (3 if direction == -1 else 4):
            to_right = board.get_piece(
                pieces, board.get_box(self.row, self.column + 1))
            if to_right and to_right.color != self.color and to_right.piece_type == PieceType.PAWN and to_right.can_be_en_passented:
                move = self.step_possible(pieces, 1, direction, 1, False)[0]
                move['capture'] = to_right
                moves.append(move)

            to_left = board.get_piece(pieces,
                                      board.get_box(self.row, self.column - 1))
            if to_left and to_left.color != self.color and to_left.piece_type == PieceType.PAWN and to_left.can_be_en_passented:
                move = self.step_possible(pieces, -1, direction, 1, False)[0]
                move['capture'] = to_left
                moves.append(move)

        return moves
Exemplo n.º 5
0
pygame.display.set_icon(screen_icon)

clock = pygame.time.Clock()

player1 = User(PieceColor.WHITE)
player2 = SmartBot(PieceColor.BLACK)
game = game.Game([player1, player2], BOX_WIDTH)

while True:
    click = None
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            row = math.floor(event.pos[1] / BOX_WIDTH)
            column = math.floor(event.pos[0] / BOX_WIDTH)
            click = board.get_box(row, column)

    text_click = text_select.get_text_select()
    if text_click:
        click = text_click
        text_select.set_text_select(None)

    game.handle_turn(game.pieces, click)
    game.render(display_surface, BOX_WIDTH, colors.BLACK, colors.WHITE, font)

    pygame.display.update()

    clock.tick(FPS)