def evaluate(self): self.evaluated_pos += 1 value = self.game.material_value for sq in range(64): if( self.game.board[sq] != 0 ): fig = abs(self.game.board[sq]) color = const.WHITE if self.game.board[sq] > 0 else const.BLACK value_piece = 0 value_piece += aiv.POSSITION_VALUE[color][fig][sq] if( fig == const.PAWN ): #---pawn structure--- #pawn chain if( self.game.figures[const.PAWN][color] & bb.pawn_attacks[not color][sq] ): value_piece += aiv.PAWN_IS_IN_CHAIN[color][bb.rank(sq)] #free pawn if( not self.game.figures[const.PAWN][not color] & bb.lines[bb.line(sq)] ): value_piece += aiv.PAWN_FREE[color][bb.rank(sq)] value += value_piece * (-1 if color else 1) for color in (0, 1): if( self.castling_root[color] and self.game.note[1][color] != [0, 0] and not self.castling_move_done ): value += const.CASTLING_RIGHT_LOSS_PENALTY * (-1 if color else 1) return value* (-1 if self.game.playerTurn==const.BLACK else 1)
def updateHistoryList(self): self.gui.history_lb.delete(0, tk.END) #move = self.game.history[-1][0] for history_note in self.history: move = history_note[0] sq_start = bo.getBits(move, *const.MOVE_START) sq_dest = bo.getBits(move, *const.MOVE_DEST) fig_start = bo.getBits(move, *const.MOVE_FIG_START) fig_cap = bo.getBits(move, *const.MOVE_FIG_CAPTURE) fig_prom = bo.getBits(move, *const.MOVE_PROM) + 2 move_type = bo.getBits(move, *const.MOVE_TYPE) color = bo.getBits(move, *const.MOVE_COLOR) rank_start = bb.rank(sq_start) rank_dest = bb.rank(sq_dest) line_start = bb.line(sq_start) line_dest = bb.line(sq_dest) comb_chr = '-' if (fig_cap != 0): comb_chr = 'x' suffix = '' if (move_type == const.PROMOTION): suffix = " =" + const.ASCII_FIG[color][fig_prom] history_entry = "%c%c%i%c%c%i%s" % ( const.ASCII_FIG[color][fig_start], chr(97 + line_start), rank_start + 1, comb_chr, chr(97 + line_dest), rank_dest + 1, suffix) self.gui.addToHistory(history_entry)
def updateHistoryList(self): self.gui.history_lb.delete(0, tk.END) # move = self.game.history[-1][0] for history_note in self.history: move = history_note[0] sq_start = bo.getBits(move, *const.MOVE_START) sq_dest = bo.getBits(move, *const.MOVE_DEST) fig_start = bo.getBits(move, *const.MOVE_FIG_START) fig_cap = bo.getBits(move, *const.MOVE_FIG_CAPTURE) fig_prom = bo.getBits(move, *const.MOVE_PROM) + 2 move_type = bo.getBits(move, *const.MOVE_TYPE) color = bo.getBits(move, *const.MOVE_COLOR) rank_start = bb.rank(sq_start) rank_dest = bb.rank(sq_dest) line_start = bb.line(sq_start) line_dest = bb.line(sq_dest) comb_chr = "-" if fig_cap != 0: comb_chr = "x" suffix = "" if move_type == const.PROMOTION: suffix = " =" + const.ASCII_FIG[color][fig_prom] history_entry = "%c%c%i%c%c%i%s" % ( const.ASCII_FIG[color][fig_start], chr(97 + line_start), rank_start + 1, comb_chr, chr(97 + line_dest), rank_dest + 1, suffix, ) self.gui.addToHistory(history_entry)
def createChessboard(self): edgeLength= min( self.height, self.width ) #border self.create_rectangle( 0, 0, edgeLength, edgeLength, fill= const.COLORS["border"] ) #tiles for sq in range(64): self.create_rectangle( self.getTilePos( sq ), self.addVec( self.getTilePos( sq ), (self.dispScale*const.DEFAULTTILEWIDTH, self.dispScale*const.DEFAULTTILEWIDTH)), fill= const.COLORS["tiles"][ (sq+bb.rank(sq))%2 ] )
def createChessboard(self): edgeLength = min(self.height, self.width) #border self.create_rectangle(0, 0, edgeLength, edgeLength, fill=const.COLORS["border"]) #tiles for sq in range(64): self.create_rectangle( self.getTilePos(sq), self.addVec(self.getTilePos(sq), (self.dispScale * const.DEFAULTTILEWIDTH, self.dispScale * const.DEFAULTTILEWIDTH)), fill=const.COLORS["tiles"][(sq + bb.rank(sq)) % 2])
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
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
def getTilePos(self, sq): return ( self.dispScale * ( const.DEFAULTBORDERWIDTH+const.DEFAULTTILEWIDTH*(bb.line(sq)) ), \ self.dispScale * ( const.DEFAULTBORDERWIDTH+const.DEFAULTTILEWIDTH*(7-bb.rank(sq)) ) )
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