|
| 1 | +//the board structure |
| 2 | +const |
| 3 | + P = 0; |
| 4 | + N = 1; |
| 5 | + B = 2; |
| 6 | + R = 3; |
| 7 | + Q = 4; |
| 8 | + K = 5; |
| 9 | + E = -1; |
| 10 | + WHITE = 0; |
| 11 | + BLACK = 1; |
| 12 | + Pval = 100; |
| 13 | + Qval = 900; |
| 14 | + Nval = 300; |
| 15 | + Bval = 330; |
| 16 | + Rval = 500; |
| 17 | + Kval = 99999; |
| 18 | + INF = 999999; |
| 19 | + typedef unsigned long U64; |
| 20 | + typedef bool bool; |
| 21 | + typedef struct _board //104 bytes |
| 22 | + { |
| 23 | + U64 AllPieces; //All the pieces on the board |
| 24 | + U64 PiecesSide[2]; //All the pieces belonging to each side |
| 25 | + U64 Pieces[12]; //Specific pieces on the board eg Pieces[R] |
| 26 | + U64 hashkey; //the hash key for the transposition table |
| 27 | + unsigned char PieceTypes[64]; //All the piece types according to squares |
| 28 | + unsigned char KingPos[2]; //King position for a side |
| 29 | + unsigned char EP; //The enpassant square |
| 30 | + unsigned char castling; //Castling privilages - format defined below |
| 31 | + unsigned char fifty; //Fifty move count |
| 32 | + unsigned char SF; //Search Flags (see Search Related section) |
| 33 | + bool side; //the side to play |
| 34 | + bool xside; //the side opposite to play |
| 35 | +} board; |
| 36 | + |
| 37 | +#define LSB(X) ((X) & -(X)) |
| 38 | +typedef unsigned int move; |
| 39 | +int pieceValue[12]; |
| 40 | +//full static exchange evaluvation |
| 41 | +int SEE(const board *pos, const move m) |
| 42 | +{ |
| 43 | + //The variables are set to the inital state where the move in question has been played |
| 44 | + int SEEscore = pieceValue[extractCapture(m)]; //The score to be returned |
| 45 | + int SEEmax, SEEmin; //Maximum and minimum SEE scores (alphbeta approach) |
| 46 | + int target = extractTo(m); //The square that we are doing a SEE on |
| 47 | + int currentPieceValue = pieceValue[extractPiece(m)]; //Value of the piece currently on the target square |
| 48 | + U64 used = toBit(extractFrom(m)); //Used attackers |
| 49 | + //Battery attacks will be calculated instead of regular attacks |
| 50 | + //Piece order isn't a problem (except when dealing with Queens) |
| 51 | + U64 occupancy = (pos->AllPieces ^ used) & |
| 52 | + ~(pos->Pieces[B] | pos->Pieces[R] | ((pos->Pieces[P]) & (Pcaps(target, WHITE) | Pcaps(target, BLACK)))); |
| 53 | + |
| 54 | + U64 attackers; //attackers of each piece type from both sides |
| 55 | + U64 attackersSide; //attackers for each piece type for a particular side |
| 56 | + U64 attackersPiece; //attackers for a particular color and a particular type |
| 57 | + |
| 58 | + //handle enpassant and promotion |
| 59 | + if (currentPieceValue == Pval) |
| 60 | + { |
| 61 | + if (extractEP(m)) |
| 62 | + { |
| 63 | + used |= toBit(extractEP(m)); |
| 64 | + occupancy ^= toBit(extractEP(m)); |
| 65 | + SEEscore = Pval; |
| 66 | + } |
| 67 | + else if (extractPromotion(m) != E) |
| 68 | + { |
| 69 | + SEEscore += pieceValue[extractPromotion(m)] - Pval; |
| 70 | + currentPieceValue = Qval; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + //these are the bounds, we will be doing an alphabeta-like search in SEE |
| 75 | + SEEmax = SEEscore; //upperbound |
| 76 | + SEEmin = -INF; //lowerbound |
| 77 | + |
| 78 | + //Generate attackers |
| 79 | + attackers = attacksToOcc(*pos, target, occupancy) ^ used; |
| 80 | + |
| 81 | + //Loop Through Opponent Captures |
| 82 | + while (attackersSide = attackers & pos->PiecesSide[pos->xside]) |
| 83 | + { |
| 84 | + SEEscore -= currentPieceValue; |
| 85 | + if (SEEscore >= SEEmax) |
| 86 | + return SEEmax; |
| 87 | + if (SEEscore > SEEmin) |
| 88 | + SEEmin = SEEscore; |
| 89 | + |
| 90 | + if (attackersPiece = attackersSide & pos->Pieces[P]) //Pawn |
| 91 | + { |
| 92 | + used |= LSB(attackersPiece); |
| 93 | + attackers ^= LSB(attackersPiece); |
| 94 | + currentPieceValue = Pval; |
| 95 | + } |
| 96 | + else if (attackersPiece = attackersSide & pos->Pieces[N]) //Knight |
| 97 | + { |
| 98 | + attackers ^= LSB(attackersPiece); |
| 99 | + currentPieceValue = Nval; |
| 100 | + } |
| 101 | + else if (attackersPiece = attackersSide & pos->Pieces[B]) //Bishop |
| 102 | + { |
| 103 | + attackers ^= LSB(attackersPiece); |
| 104 | + used |= LSB(attackersPiece); |
| 105 | + currentPieceValue = Bval; |
| 106 | + } |
| 107 | + else if (attackersPiece = attackersSide & pos->Pieces[R]) //Rook |
| 108 | + { |
| 109 | + attackers ^= LSB(attackersPiece); |
| 110 | + used |= LSB(attackersPiece); |
| 111 | + currentPieceValue = Rval; |
| 112 | + } |
| 113 | + else if (attackersPiece = attackersSide & pos->Pieces[Q]) //Queen |
| 114 | + { |
| 115 | + used |= LSB(attackersPiece); |
| 116 | + occupancy ^= LSB(attackersPiece); |
| 117 | + attackers = attacksBQRToOcc(*pos, target, occupancy) ^ used; |
| 118 | + currentPieceValue = Qval; |
| 119 | + } |
| 120 | + else //King |
| 121 | + { |
| 122 | + attackers ^= toBit(pos->KingPos[pos->xside]); |
| 123 | + currentPieceValue = Kval; |
| 124 | + } |
| 125 | + //Loop Through My Captures |
| 126 | + if (!(attackersSide = attackers & pos->PiecesSide[pos->side])) |
| 127 | + break; |
| 128 | + |
| 129 | + SEEscore += currentPieceValue; |
| 130 | + if (SEEscore <= SEEmin) |
| 131 | + return SEEmin; |
| 132 | + if (SEEscore < SEEmax) |
| 133 | + SEEmax = SEEscore; |
| 134 | + |
| 135 | + if (attackersPiece = attackersSide & pos->Pieces[P]) //Pawn |
| 136 | + { |
| 137 | + used |= LSB(attackersPiece); |
| 138 | + attackers ^= LSB(attackersPiece); |
| 139 | + currentPieceValue = Pval; |
| 140 | + } |
| 141 | + else if (attackersPiece = attackersSide & pos->Pieces[N]) //Knight |
| 142 | + { |
| 143 | + attackers ^= LSB(attackersPiece); |
| 144 | + currentPieceValue = Nval; |
| 145 | + } |
| 146 | + else if (attackersPiece = attackersSide & pos->Pieces[B]) //Bishop |
| 147 | + { |
| 148 | + attackers ^= LSB(attackersPiece); |
| 149 | + used |= LSB(attackersPiece); |
| 150 | + currentPieceValue = Bval; |
| 151 | + } |
| 152 | + else if (attackersPiece = attackersSide & pos->Pieces[R]) //Rook |
| 153 | + { |
| 154 | + attackers ^= LSB(attackersPiece); |
| 155 | + used |= LSB(attackersPiece); |
| 156 | + currentPieceValue = Rval; |
| 157 | + } |
| 158 | + else if (attackersPiece = attackersSide & pos->Pieces[Q]) //Queen |
| 159 | + { |
| 160 | + used |= LSB(attackersPiece); |
| 161 | + occupancy ^= LSB(attackersPiece); |
| 162 | + attackers |= attacksBQRToOcc(*pos, target, occupancy) & ~used; |
| 163 | + currentPieceValue = Qval; |
| 164 | + } |
| 165 | + else //King |
| 166 | + { |
| 167 | + attackers ^= toBit(pos->KingPos[pos->side]); |
| 168 | + currentPieceValue = Kval; |
| 169 | + } |
| 170 | + } |
| 171 | + if (SEEscore < SEEmin) |
| 172 | + return SEEmin; |
| 173 | + if (SEEscore > SEEmax) |
| 174 | + return SEEmax; |
| 175 | + return SEEscore; |
| 176 | +} |
0 commit comments