src/game.c

changeset 14
970748b9a73b
parent 13
faec61c4901f
child 15
7ffd66591afe
equal deleted inserted replaced
13:faec61c4901f 14:970748b9a73b
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 board[move->fromrow][move->fromfile] = 0; 81 msrc(board, move) = 0;
82 // TODO: care for en passant capture 82 // TODO: care for en passant capture
83 board[move->torow][move->tofile] = move->piece; 83 mdst(board, move) = move->piece;
84 84
85 /* castling */ 85 /* castling */
86 if ((move->piece & PIECE_MASK) == KING && 86 if ((move->piece & PIECE_MASK) == KING &&
87 move->fromfile == fileidx('e')) { 87 move->fromfile == fileidx('e')) {
88 uint8_t color = move->piece & COLOR_MASK; 88 uint8_t color = move->piece & COLOR_MASK;
110 if (!chkidx(move)) { 110 if (!chkidx(move)) {
111 return FALSE; 111 return FALSE;
112 } 112 }
113 113
114 /* does piece exist */ 114 /* does piece exist */
115 result = board[move->fromrow][move->fromfile] == move->piece; 115 result = msrc(board, move) == move->piece;
116 116
117 /* can't capture own pieces */
118 if ((mdst(board, move) & COLOR_MASK) == (move->piece & COLOR_MASK)) {
119 return FALSE;
120 }
121
122 /* validate individual rules */
117 switch (move->piece & PIECE_MASK) { 123 switch (move->piece & PIECE_MASK) {
118 case PAWN: 124 case PAWN:
119 result = result && pawn_chkrules(board, move); 125 result = result && pawn_chkrules(board, move);
120 result = result && !pawn_isblocked(board, move); 126 result = result && !pawn_isblocked(board, move);
121 break; 127 break;
245 if (!move->piece) { 251 if (!move->piece) {
246 move->piece = PAWN; 252 move->piece = PAWN;
247 move->fromfile = fileidx(mstr[0]); 253 move->fromfile = fileidx(mstr[0]);
248 } 254 }
249 } else { 255 } else {
250 /* move (e.g. "Ndf3") */ 256 /* move (e.g. "Ndf3", "N2c3") */
251 move->fromfile = fileidx(mstr[1]); 257 move->fromfile = isfile(mstr[1]) ?
258 fileidx(mstr[1]) : rowidx(mstr[1]);
252 } 259 }
253 move->tofile = fileidx(mstr[2]); 260 move->tofile = fileidx(mstr[2]);
254 move->torow = rowidx(mstr[3]); 261 move->torow = rowidx(mstr[3]);
255 } else if (len == 5) { 262 } else if (len == 5) {
256 if (strcmp(mstr, "O-O-O") == 0) { 263 if (strcmp(mstr, "O-O-O") == 0) {
364 } else { 371 } else {
365 apply_move(board, &move); 372 apply_move(board, &move);
366 if (move.checkmate) { 373 if (move.checkmate) {
367 printw("Checkmate!"); 374 printw("Checkmate!");
368 return 1; 375 return 1;
376 } else {
377 return 0;
369 } 378 }
370 } 379 }
371 } else { 380 } else {
372 printw("Can't interpret move - please use algebraic notation."); 381 printw("Can't interpret move - please use algebraic notation.");
373 } 382 }

mercurial