Example #1
0
    def isUnderAttack(self, sq, color):
        result = False
        
        attack_diag =   bb.diagonal_attacks_lr[sq][bb.getDiagonalByteLR(self.all_figures[const.BOTH], sq)] | \
                        bb.diagonal_attacks_rl[sq][bb.getDiagonalByteRL(self.all_figures[const.BOTH], sq)]
        
        attack_rank_line =  bb.line_attacks[sq][bb.getLineByte(self.all_figures[const.BOTH], bb.line(sq))] | \
                            bb.rank_attacks[sq][bb.getRankByte(self.all_figures[const.BOTH], bb.rank(sq))]
        
        #check for enemys
        if( bb.knight_attacks[sq] & self.figures[const.KNIGHT][not color] or \
            bb.pawn_attacks[color][sq] & self.figures[const.PAWN][not color] or \
            bb.king_attacks[sq] & self.figures[const.KING][not color] or \
            attack_diag & self.figures[const.BISHOP][not color] or \
            attack_diag & self.figures[const.QUEEN][not color] or \
            attack_rank_line & self.figures[const.ROOK][not color] or \
            attack_rank_line & self.figures[const.QUEEN][not color] ):
            
            result = True

        return result
Example #2
0
    def isUnderAttack(self, sq, color):
        result = False

        attack_diag =   bb.diagonal_attacks_lr[sq][bb.getDiagonalByteLR(self.all_figures[const.BOTH], sq)] | \
                        bb.diagonal_attacks_rl[sq][bb.getDiagonalByteRL(self.all_figures[const.BOTH], sq)]

        attack_rank_line =  bb.line_attacks[sq][bb.getLineByte(self.all_figures[const.BOTH], bb.line(sq))] | \
                            bb.rank_attacks[sq][bb.getRankByte(self.all_figures[const.BOTH], bb.rank(sq))]

        #check for enemys
        if( bb.knight_attacks[sq] & self.figures[const.KNIGHT][not color] or \
            bb.pawn_attacks[color][sq] & self.figures[const.PAWN][not color] or \
            bb.king_attacks[sq] & self.figures[const.KING][not color] or \
            attack_diag & self.figures[const.BISHOP][not color] or \
            attack_diag & self.figures[const.QUEEN][not color] or \
            attack_rank_line & self.figures[const.ROOK][not color] or \
            attack_rank_line & self.figures[const.QUEEN][not color] ):

            result = True

        return result
Example #3
0
 def getPossibleMoves(self, color):
     pieces = self.all_figures[color]
     
     result = []
     
     sq = 0
     while(pieces and sq<64):
         if(pieces & bb.mask64[sq]):
             moves = 0
             fig = abs(self.board[sq])
             if(fig == const.PAWN ):
                 #single step
                 moves |= bb.pawn_singlestep[color][sq] & ~self.all_figures[const.BOTH]
                 #double step
                 if(moves):
                     moves |= bb.pawn_doublestep[color][sq] & ~self.all_figures[const.BOTH]
                 #capture
                 moves |= bb.pawn_attacks[color][sq] & self.all_figures[not color]
                 #en-passant capture
                 moves |= bb.pawn_attacks[color][sq] & ( self.note[0] << (40 if color==const.WHITE else 16))
     
             elif(fig == const.KING ):
                 moves |= bb.king_attacks[sq] & ~self.all_figures[const.BOTH]
                 moves |= (bb.king_attacks[sq] & ~self.all_figures[color] ) & self.all_figures[not color]
                 #castling
                 for side in (const.CASTLING_LEFT, const.CASTLING_RIGHT):
                     if(not self.note[1][color][side] and \
                        not (bb.castling_no_fig[color][side] & self.all_figures[const.BOTH]) and \
                        not self.isUnderAttack(bb.castling_no_attack_sq[color][side], color) and \
                        not self.isUnderAttack(bb.castling_king_start_sq[color], color)):
                         moves |= bb.castling_king_dest[color][side]
     
             else:
                 if(fig == const.KNIGHT):
                     moves |= bb.knight_attacks[sq]
                 else:
                     if(fig == const.BISHOP or fig == const.QUEEN):
                         moves |=   bb.diagonal_attacks_lr[sq][bb.getDiagonalByteLR(self.all_figures[const.BOTH], sq)] | \
                                             bb.diagonal_attacks_rl[sq][bb.getDiagonalByteRL(self.all_figures[const.BOTH], sq)] 
                     if(fig == const.ROOK or fig == const.QUEEN):
                         moves |=   bb.line_attacks[sq][bb.getLineByte(self.all_figures[const.BOTH], bb.line(sq))] | \
                                             bb.rank_attacks[sq][bb.getRankByte(self.all_figures[const.BOTH], bb.rank(sq))]
                     
                 moves &= ~self.all_figures[color]
                     
             dest = 0
             while(moves and dest<64):
                 if(moves & bb.mask64[dest]):
                     
                     move_type = const.NORMAL_MOVE
                     if(bb.mask64[dest] & self.all_figures[not color]):
                         move_type = const.CAPTURE
                     
                     if(fig == const.KING and abs(bb.line(dest)-bb.line(sq)) == 2):
                         move_type = const.CASTLING
                     elif(fig == const.PAWN):
                         if(abs(bb.rank(dest)-bb.rank(sq))==2):
                             move_type = const.DOUBLE_STEP
                         elif(bb.line(dest) != bb.line(sq) and self.board[dest] == 0):
                             move_type = const.ENPASSANT_CAPTURE
                         elif(bb.rank(dest) == 7-7*color):
                             move_type = const.PROMOTION
                     
                     move = move_type | (sq << const.MOVE_START[0]) | (dest << const.MOVE_DEST[0]) | (abs(self.board[sq]) << const.MOVE_FIG_START[0]) | (abs(self.board[dest]) << const.MOVE_FIG_CAPTURE[0]) | (color << const.MOVE_COLOR[0])
                     
                     if(move_type == const.PROMOTION):
                         for fig_prom in (const.PROM_KNIGHT, const.PROM_BISHOP, const.PROM_ROOK, const.PROM_QUEEN):
                             result.append( move | (fig_prom << const.MOVE_PROM[0]) )
                     else:
                         result.append(move)
                         
                     move ^= bb.mask64[dest]
                 dest +=1 
             pieces &= ~bb.mask64[sq]
         sq += 1
         
     return result
Example #4
0
    def getPossibleMoves(self, color):
        pieces = self.all_figures[color]

        result = []

        sq = 0
        while (pieces and sq < 64):
            if (pieces & bb.mask64[sq]):
                moves = 0
                fig = abs(self.board[sq])
                if (fig == const.PAWN):
                    #single step
                    moves |= bb.pawn_singlestep[color][sq] & ~self.all_figures[
                        const.BOTH]
                    #double step
                    if (moves):
                        moves |= bb.pawn_doublestep[color][
                            sq] & ~self.all_figures[const.BOTH]
                    #capture
                    moves |= bb.pawn_attacks[color][sq] & self.all_figures[
                        not color]
                    #en-passant capture
                    moves |= bb.pawn_attacks[color][sq] & (
                        self.note[0] << (40 if color == const.WHITE else 16))

                elif (fig == const.KING):
                    moves |= bb.king_attacks[sq] & ~self.all_figures[
                        const.BOTH]
                    moves |= (bb.king_attacks[sq] & ~self.all_figures[color]
                              ) & self.all_figures[not color]
                    #castling
                    for side in (const.CASTLING_LEFT, const.CASTLING_RIGHT):
                        if(not self.note[1][color][side] and \
                           not (bb.castling_no_fig[color][side] & self.all_figures[const.BOTH]) and \
                           not self.isUnderAttack(bb.castling_no_attack_sq[color][side], color) and \
                           not self.isUnderAttack(bb.castling_king_start_sq[color], color)):
                            moves |= bb.castling_king_dest[color][side]

                else:
                    if (fig == const.KNIGHT):
                        moves |= bb.knight_attacks[sq]
                    else:
                        if (fig == const.BISHOP or fig == const.QUEEN):
                            moves |=   bb.diagonal_attacks_lr[sq][bb.getDiagonalByteLR(self.all_figures[const.BOTH], sq)] | \
                                                bb.diagonal_attacks_rl[sq][bb.getDiagonalByteRL(self.all_figures[const.BOTH], sq)]
                        if (fig == const.ROOK or fig == const.QUEEN):
                            moves |=   bb.line_attacks[sq][bb.getLineByte(self.all_figures[const.BOTH], bb.line(sq))] | \
                                                bb.rank_attacks[sq][bb.getRankByte(self.all_figures[const.BOTH], bb.rank(sq))]

                    moves &= ~self.all_figures[color]

                dest = 0
                while (moves and dest < 64):
                    if (moves & bb.mask64[dest]):

                        move_type = const.NORMAL_MOVE
                        if (bb.mask64[dest] & self.all_figures[not color]):
                            move_type = const.CAPTURE

                        if (fig == const.KING
                                and abs(bb.line(dest) - bb.line(sq)) == 2):
                            move_type = const.CASTLING
                        elif (fig == const.PAWN):
                            if (abs(bb.rank(dest) - bb.rank(sq)) == 2):
                                move_type = const.DOUBLE_STEP
                            elif (bb.line(dest) != bb.line(sq)
                                  and self.board[dest] == 0):
                                move_type = const.ENPASSANT_CAPTURE
                            elif (bb.rank(dest) == 7 - 7 * color):
                                move_type = const.PROMOTION

                        move = move_type | (sq << const.MOVE_START[0]) | (
                            dest << const.MOVE_DEST[0]) | (abs(
                                self.board[sq]) << const.MOVE_FIG_START[0]) | (
                                    abs(self.board[dest]) <<
                                    const.MOVE_FIG_CAPTURE[0]) | (
                                        color << const.MOVE_COLOR[0])

                        if (move_type == const.PROMOTION):
                            for fig_prom in (const.PROM_KNIGHT,
                                             const.PROM_BISHOP,
                                             const.PROM_ROOK,
                                             const.PROM_QUEEN):
                                result.append(move | (
                                    fig_prom << const.MOVE_PROM[0]))
                        else:
                            result.append(move)

                        move ^= bb.mask64[dest]
                    dest += 1
                pieces &= ~bb.mask64[sq]
            sq += 1

        return result