[go: up one dir, main page]

Menu

[r805]: / positionc.c  Maximize  Restore  History

Download this file

217 lines (187 with data), 7.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#undef addBonus
#undef COLOR
#undef OPPCOLOR
#ifdef PERSPECTIVE_WHITE
#define COLOR WHITE
#define OPPCOLOR BLACK
#define addBonus(position, bonus) (position->balance += bonus);
#else
#define COLOR BLACK
#define OPPCOLOR WHITE
#define addBonus(position, bonus) (position->balance -= bonus);
#endif
#ifdef PERSPECTIVE_WHITE
static int makeWhiteMove(Variation * variation, const Move move)
#else
static int makeBlackMove(Variation * variation, const Move move)
#endif
{
Position *position = &variation->singlePosition;
PlyInfo *plyInfo = &variation->plyInfo[variation->ply++];
const Square from = getFromSquare(move);
const Square to = getToSquare(move);
const Piece newPiece = getNewPiece(move);
const Piece movingPiece = position->piece[from];
const Piece capturedPiece = position->piece[to];
const Bitboard minTo = minValue[to];
const Bitboard maxFrom = maxValue[from];
int result = 0;
assert(to == from || pieceType(capturedPiece) != KING);
variation->positionHistory[POSITION_HISTORY_OFFSET - 1 + variation->ply] =
plyInfo->hashValue = position->hashValue;
plyInfo->currentMove = move;
plyInfo->balance = position->balance;
plyInfo->pieceCount = position->pieceCount;
variation->plyInfo[variation->ply].staticValueAvailable = FALSE;
variation->plyInfo[variation->ply].gainsUpdated = FALSE;
position->hashValue = ~position->hashValue;
if (position->enPassantSquare != NO_SQUARE)
{
position->hashValue ^= GENERATED_KEYTABLE[0][position->enPassantSquare];
}
position->enPassantSquare = NO_SQUARE;
position->activeColor = OPPCOLOR;
if (to == from)
{
assert(checkVariation(variation) == 0);
return result; /* Nullmove */
}
plyInfo->captured = capturedPiece;
position->piecesOfColor[COLOR] &= maxFrom;
position->piecesOfColor[COLOR] |= minTo;
position->piecesOfType[movingPiece] &= maxFrom;
position->hashValue ^=
GENERATED_KEYTABLE[movingPiece][from] ^
GENERATED_KEYTABLE[movingPiece][to];
position->castlingRights &=
remainingCastlings[to] & remainingCastlings[from];
if (position->castlingRights != plyInfo->castlingRights)
{
position->hashValue ^=
GENERATED_KEYTABLE[0][plyInfo->castlingRights] ^
GENERATED_KEYTABLE[0][position->castlingRights];
}
position->halfMoveClock++;
position->piece[to] = movingPiece;
position->piece[from] = NO_PIECE;
addBonus(position, pieceSquareBonus[movingPiece][to] -
pieceSquareBonus[movingPiece][from]);
if (capturedPiece != NO_PIECE)
{
position->halfMoveClock = 0;
position->piecesOfColor[OPPCOLOR] &= ~minTo;
position->piecesOfType[capturedPiece] &= ~minTo;
position->numberOfPieces[OPPCOLOR]--;
if (pieceType(capturedPiece) == PAWN)
{
position->numberOfPawns[OPPCOLOR]--;
position->pawnHashValue ^= GENERATED_KEYTABLE[capturedPiece][to];
}
else
{
position->pieceCount -= (capturedPiece == (BISHOP | OPPCOLOR) ?
bishopPieceCountWeight[OPPCOLOR][to] :
pieceCountWeight[capturedPiece]);
}
position->hashValue ^= GENERATED_KEYTABLE[capturedPiece][to];
addBonus(position, pieceSquareBonus[capturedPiece][to]);
}
if (pieceType(movingPiece) == PAWN)
{
position->halfMoveClock = 0;
position->pawnHashValue ^=
GENERATED_KEYTABLE[movingPiece][from] ^
GENERATED_KEYTABLE[movingPiece][to];
if (distance(from, to) == 2)
{
position->enPassantSquare = (Square) ((from + to) >> 1);
position->hashValue ^=
GENERATED_KEYTABLE[0][position->enPassantSquare];
}
else if (to == plyInfo->enPassantSquare)
{
const Square captureSquare =
(Square) (to + (rank(from) - rank(to)) * 8);
const Piece capturedPawn = position->piece[captureSquare];
clearSquare(position->piecesOfColor[OPPCOLOR], captureSquare);
clearSquare(position->piecesOfType[capturedPawn], captureSquare);
position->hashValue ^=
GENERATED_KEYTABLE[capturedPawn][captureSquare];
position->pawnHashValue ^=
GENERATED_KEYTABLE[capturedPawn][captureSquare];
addBonus(position, pieceSquareBonus[capturedPawn][captureSquare]);
plyInfo->restoreSquare1 = captureSquare;
plyInfo->restorePiece1 = capturedPawn;
position->piece[captureSquare] = NO_PIECE;
position->numberOfPieces[OPPCOLOR]--;
position->numberOfPawns[OPPCOLOR]--;
}
else if (newPiece != NO_PIECE)
{
const Piece effectiveNewPiece = (Piece) (newPiece | COLOR);
plyInfo->restoreSquare1 = from;
plyInfo->restorePiece1 = movingPiece;
position->piece[to] = effectiveNewPiece;
position->numberOfPawns[COLOR]--;
position->pieceCount +=
(newPiece == (Piece) BISHOP ?
bishopPieceCountWeight[COLOR][to] :
pieceCountWeight[effectiveNewPiece]);
position->hashValue ^=
GENERATED_KEYTABLE[movingPiece][to] ^
GENERATED_KEYTABLE[effectiveNewPiece][to];
position->pawnHashValue ^= GENERATED_KEYTABLE[movingPiece][to];
addBonus(position, pieceSquareBonus[effectiveNewPiece][to] -
pieceSquareBonus[movingPiece][to]);
setSquare(position->piecesOfType[position->piece[to]], to);
}
}
else if (pieceType(movingPiece) == KING)
{
position->king[COLOR] = to;
if (distance(from, to) == 2)
{
const Square rookFrom = rookOrigin[to];
const Square rookTo = (Square) ((from + to) >> 1);
const Piece movingRook = position->piece[rookFrom];
plyInfo->restoreSquare1 = rookFrom;
plyInfo->restorePiece1 = movingRook;
plyInfo->restoreSquare2 = rookTo;
plyInfo->restorePiece2 = position->piece[rookTo];
position->piece[rookFrom] = NO_PIECE;
position->piece[rookTo] = movingRook;
position->halfMoveClock = 0;
setSquare(position->piecesOfColor[COLOR], rookTo);
clearSquare(position->piecesOfColor[COLOR], rookFrom);
setSquare(position->piecesOfType[movingRook], rookTo);
clearSquare(position->piecesOfType[movingRook], rookFrom);
position->hashValue ^=
GENERATED_KEYTABLE[movingRook][rookFrom] ^
GENERATED_KEYTABLE[movingRook][rookTo];
addBonus(position, pieceSquareBonus[movingRook][rookTo] -
pieceSquareBonus[movingRook][rookFrom]);
if (getDirectAttackers(position, from, OPPCOLOR,
position->allPieces) != EMPTY_BITBOARD ||
getDirectAttackers(position, rookTo, OPPCOLOR,
position->allPieces) != EMPTY_BITBOARD)
{
result = 1; /* castling move was not legal */
}
}
}
setSquare(position->piecesOfType[position->piece[to]], to);
position->allPieces =
position->piecesOfColor[WHITE] | position->piecesOfColor[BLACK];
assert(checkVariation(variation) == 0);
#ifdef TRACE_POSITIONS
if (variation->nodes >= POSITION_TRACE_START &&
variation->nodes <= POSITION_TRACE_START + POSITION_TRACE_QUANTITY)
{
char buffer[128];
getMoveDump(move, buffer);
logDebug("pos %llu after %s: %llu\n",
variation->nodes, buffer, variation->singlePosition.hashValue);
}
#endif
return result;
}