Sat, 22 Mar 2014 16:25:49 +0100
implemented castling (without validation)
src/game.c | file | annotate | diff | comparison | revisions |
1.1 --- a/src/game.c Sat Mar 22 16:04:02 2014 +0100 1.2 +++ b/src/game.c Sat Mar 22 16:25:49 2014 +0100 1.3 @@ -74,6 +74,20 @@ 1.4 board[move->fromrow][move->fromfile] = 0; 1.5 // TODO: care for en passant capture 1.6 board[move->torow][move->tofile] = move->piece; 1.7 + 1.8 + /* castling */ 1.9 + if ((move->piece & PIECE_MASK) == KING && 1.10 + move->fromfile == fileidx('e')) { 1.11 + uint8_t color = move->piece & COLOR_MASK; 1.12 + 1.13 + if (move->tofile == fileidx('g')) { 1.14 + board[move->torow][fileidx('h')] = 0; 1.15 + board[move->torow][fileidx('f')] = color|ROOK; 1.16 + } else if (move->tofile == fileidx('c')) { 1.17 + board[move->torow][fileidx('a')] = 0; 1.18 + board[move->torow][fileidx('d')] = color|ROOK; 1.19 + } 1.20 + } 1.21 } 1.22 1.23 static _Bool validate_move(Board board, uint8_t mycolor, Move *move) { 1.24 @@ -82,7 +96,7 @@ 1.25 /* does piece exist */ 1.26 result &= board[move->fromrow][move->fromfile] == move->piece; 1.27 1.28 - /* is move rule conform */ 1.29 + /* does move comply to rules */ 1.30 // TODO: make it so 1.31 1.32 /* is piece blocked */ 1.33 @@ -94,24 +108,24 @@ 1.34 return result; 1.35 } 1.36 1.37 -static _Bool eval_move(Board board, uint8_t mycolor, char *movestr, Move *move) { 1.38 +static _Bool eval_move(Board board, uint8_t mycolor, char *mstr, Move *move) { 1.39 memset(move, 0, sizeof(Move)); 1.40 1.41 - size_t len = strlen(movestr); 1.42 + size_t len = strlen(mstr); 1.43 1.44 /* remove check */ 1.45 - if (movestr[len-1] == '+') { 1.46 - len--; movestr[len] = '\0'; 1.47 + if (mstr[len-1] == '+') { 1.48 + len--; mstr[len] = '\0'; 1.49 move->check = TRUE; 1.50 } 1.51 1.52 if (len == 2) { 1.53 /* pawn move (e.g. "e4") */ 1.54 - if (isfile(movestr[0]) && isrow(movestr[1])) { 1.55 + if (isfile(mstr[0]) && isrow(mstr[1])) { 1.56 move->piece = PAWN; 1.57 - move->fromfile = move->tofile = fileidx(movestr[0]); 1.58 - move->torow = rowidx(movestr[1]); 1.59 - move->fromrow = rowidx(movestr[1]) + (mycolor == WHITE ? -1 : 1); 1.60 + move->fromfile = move->tofile = fileidx(mstr[0]); 1.61 + move->torow = rowidx(mstr[1]); 1.62 + move->fromrow = rowidx(mstr[1]) + (mycolor == WHITE ? -1 : 1); 1.63 if (move->fromrow > 6) { 1.64 move->piece = 0; 1.65 } else { 1.66 @@ -127,11 +141,11 @@ 1.67 } 1.68 } 1.69 } else if (len == 3) { 1.70 - if (strcmp(movestr, "0-0") == 0) { 1.71 + if (strcmp(mstr, "O-O") == 0) { 1.72 /* king side castling */ 1.73 move->piece = KING; 1.74 move->fromfile = fileidx('e'); 1.75 - move->fromfile = fileidx('g'); 1.76 + move->tofile = fileidx('g'); 1.77 move->fromrow = move->torow = mycolor == WHITE ? 0 : 7; 1.78 } else { 1.79 /* unambiguous move (e.g. "Nf3") */ 1.80 @@ -143,13 +157,19 @@ 1.81 /* unambiguous capture (e.g. "Nxf3", "dxe5") */ 1.82 1.83 } else if (len == 5) { 1.84 - /* queen side castling "O-O-O" */ 1.85 - 1.86 - /* ambiguous capture (e.g. "Ndxf3") */ 1.87 - 1.88 - /* long notation move (e.g. "Nc5a4") */ 1.89 - 1.90 - /* long notation capture (e.g. "e5xf6") */ 1.91 + if (strcmp(mstr, "O-O-O") == 0) { 1.92 + /* queen side castling "O-O-O" */ 1.93 + move->piece = KING; 1.94 + move->fromfile = fileidx('e'); 1.95 + move->tofile = fileidx('c'); 1.96 + move->fromrow = move->torow = mycolor == WHITE ? 0 : 7; 1.97 + } else { 1.98 + /* ambiguous capture (e.g. "Ndxf3") */ 1.99 + 1.100 + /* long notation move (e.g. "Nc5a4") */ 1.101 + 1.102 + /* long notation capture (e.g. "e5xf6") */ 1.103 + } 1.104 } else if (len == 6) { 1.105 /* long notation capture (e.g. "Nc5xf3") */ 1.106 }