src/game.c

changeset 15
7ffd66591afe
parent 14
970748b9a73b
child 16
a298c6637c30
equal deleted inserted replaced
14:970748b9a73b 15:7ffd66591afe
76 * 76 *
77 * @param board the current board state 77 * @param board the current board state
78 * @param move the move to apply 78 * @param move the move to apply
79 */ 79 */
80 static void apply_move(Board board, Move *move) { 80 static void apply_move(Board board, Move *move) {
81 uint8_t piece = move->piece & PIECE_MASK;
82 uint8_t color = move->piece & COLOR_MASK;
83
84 /* en passant capture */
85 if (move->capture && piece == PAWN &&
86 mdst(board, move) == 0) {
87 board[move->fromrow][move->tofile] = 0;
88 }
89
90 /* remove old en passant threats */
91 for (uint8_t file = 0 ; file < 8 ; file++) {
92 board[3][file] &= ~ENPASSANT_THREAT;
93 board[4][file] &= ~ENPASSANT_THREAT;
94 }
95
96 /* add new en passant threat */
97 if (piece == PAWN && (
98 (move->fromrow == 1 && move->torow == 3) ||
99 (move->fromrow == 6 && move->torow == 4))) {
100 move->piece |= ENPASSANT_THREAT;
101 }
102
103 /* move (and maybe capture) */
81 msrc(board, move) = 0; 104 msrc(board, move) = 0;
82 // TODO: care for en passant capture
83 mdst(board, move) = move->piece; 105 mdst(board, move) = move->piece;
84 106
85 /* castling */ 107 /* castling */
86 if ((move->piece & PIECE_MASK) == KING && 108 if (piece == KING &&
87 move->fromfile == fileidx('e')) { 109 move->fromfile == fileidx('e')) {
88 uint8_t color = move->piece & COLOR_MASK;
89 110
90 if (move->tofile == fileidx('g')) { 111 if (move->tofile == fileidx('g')) {
91 board[move->torow][fileidx('h')] = 0; 112 board[move->torow][fileidx('h')] = 0;
92 board[move->torow][fileidx('f')] = color|ROOK; 113 board[move->torow][fileidx('f')] = color|ROOK;
93 } else if (move->tofile == fileidx('c')) { 114 } else if (move->tofile == fileidx('c')) {
243 move->torow = rowidx(mstr[2]); 264 move->torow = rowidx(mstr[2]);
244 } 265 }
245 266
246 } else if (len == 4) { 267 } else if (len == 4) {
247 move->piece = getpiece(mstr[0]); 268 move->piece = getpiece(mstr[0]);
269 if (!move->piece) {
270 move->piece = PAWN;
271 move->fromfile = fileidx(mstr[0]);
272 }
248 if (mstr[1] == 'x') { 273 if (mstr[1] == 'x') {
249 /* capture (e.g. "Nxf3", "dxe5") */ 274 /* capture (e.g. "Nxf3", "dxe5") */
250 move->capture = TRUE; 275 move->capture = TRUE;
251 if (!move->piece) { 276 } else {
252 move->piece = PAWN; 277 /* move (e.g. "Ndf3", "N2c3", "e2e4") */
253 move->fromfile = fileidx(mstr[0]); 278 if (isfile(mstr[1])) {
279 move->fromfile = fileidx(mstr[1]);
280 if (move->piece == PAWN) {
281 move->piece = 0;
282 }
283 } else {
284 move->fromrow = rowidx(mstr[1]);
254 } 285 }
255 } else {
256 /* move (e.g. "Ndf3", "N2c3") */
257 move->fromfile = isfile(mstr[1]) ?
258 fileidx(mstr[1]) : rowidx(mstr[1]);
259 } 286 }
260 move->tofile = fileidx(mstr[2]); 287 move->tofile = fileidx(mstr[2]);
261 move->torow = rowidx(mstr[3]); 288 move->torow = rowidx(mstr[3]);
262 } else if (len == 5) { 289 } else if (len == 5) {
263 if (strcmp(mstr, "O-O-O") == 0) { 290 if (strcmp(mstr, "O-O-O") == 0) {

mercurial