Mon, 31 Mar 2014 14:00:58 +0200
implemented rook + some fixes
src/chess/bishop.c | file | annotate | diff | comparison | revisions | |
src/chess/rook.c | file | annotate | diff | comparison | revisions | |
src/chess/rules.c | file | annotate | diff | comparison | revisions | |
src/game.c | file | annotate | diff | comparison | revisions |
1.1 --- a/src/chess/bishop.c Mon Mar 31 11:41:08 2014 +0200 1.2 +++ b/src/chess/bishop.c Mon Mar 31 14:00:58 2014 +0200 1.3 @@ -42,13 +42,13 @@ 1.4 uint8_t y = move->fromrow; 1.5 uint8_t x = move->fromfile; 1.6 1.7 - do { 1.8 + while (x != move->tofile-dx && y != move->torow-dy) { 1.9 x += dx; 1.10 y += dy; 1.11 if (board[y][x]) { 1.12 return 1; 1.13 } 1.14 - } while (x != move->tofile && y != move->torow); 1.15 + } 1.16 1.17 return 0; 1.18 }
2.1 --- a/src/chess/rook.c Mon Mar 31 11:41:08 2014 +0200 2.2 +++ b/src/chess/rook.c Mon Mar 31 14:00:58 2014 +0200 2.3 @@ -31,16 +31,122 @@ 2.4 #include "rook.h" 2.5 2.6 _Bool rook_chkrules(Move *move) { 2.7 - // TODO: implement 2.8 + return move->torow == move->fromrow || move->tofile == move->fromfile; 2.9 +} 2.10 + 2.11 +_Bool rook_isblocked(Board board, Move *move) { 2.12 + 2.13 + if (move->torow == move->fromrow) { 2.14 + int d = move->tofile > move->fromfile ? 1 : -1; 2.15 + uint8_t f = move->fromfile; 2.16 + while (f != move->tofile-d) { 2.17 + f += d; 2.18 + if (board[move->fromrow][f]) { 2.19 + return 1; 2.20 + } 2.21 + } 2.22 + } else { 2.23 + int d = move->torow > move->fromrow ? 1 : -1; 2.24 + uint8_t r = move->fromrow; 2.25 + while (r != move->torow - d) { 2.26 + r += d; 2.27 + if (board[r][move->fromfile]) { 2.28 + return 1; 2.29 + } 2.30 + } 2.31 + } 2.32 + 2.33 return 0; 2.34 } 2.35 2.36 -_Bool rook_isblocked(Board board, Move *move) { 2.37 - // TODO: implement 2.38 - return 1; 2.39 +static int rook_getloc_fixedrow(Board board, Move *move) { 2.40 + uint8_t file = POS_UNSPECIFIED; 2.41 + for (uint8_t f = 0 ; f < 8 ; f++) { 2.42 + if (board[move->fromrow][f] == move->piece) { 2.43 + if (file == POS_UNSPECIFIED) { 2.44 + file = f; 2.45 + } else { 2.46 + return AMBIGUOUS_MOVE; 2.47 + } 2.48 + } 2.49 + } 2.50 + if (file == POS_UNSPECIFIED) { 2.51 + return INVALID_POSITION; 2.52 + } else { 2.53 + move->fromfile = file; 2.54 + return VALID_MOVE_SYNTAX; 2.55 + } 2.56 +} 2.57 + 2.58 +static int rook_getloc_fixedfile(Board board, Move *move) { 2.59 + uint8_t row = POS_UNSPECIFIED; 2.60 + for (uint8_t r = 0 ; r < 8 ; r++) { 2.61 + if (board[r][move->fromfile] == move->piece) { 2.62 + if (row == POS_UNSPECIFIED) { 2.63 + row = r; 2.64 + } else { 2.65 + return AMBIGUOUS_MOVE; 2.66 + } 2.67 + } 2.68 + } 2.69 + if (row == POS_UNSPECIFIED) { 2.70 + return INVALID_POSITION; 2.71 + } else { 2.72 + move->fromrow = row; 2.73 + return VALID_MOVE_SYNTAX; 2.74 + } 2.75 } 2.76 2.77 int rook_getlocation(Board board, Move *move) { 2.78 - // TODO: implement 2.79 - return INVALID_MOVE_SYNTAX; 2.80 + 2.81 + if (move->fromfile != POS_UNSPECIFIED) { 2.82 + if (move->fromfile == move->tofile) { 2.83 + return rook_getloc_fixedfile(board, move); 2.84 + } else { 2.85 + if (board[move->torow][move->fromfile] == move->piece) { 2.86 + move->fromrow = move->torow; 2.87 + return VALID_MOVE_SYNTAX; 2.88 + } else { 2.89 + return INVALID_POSITION; 2.90 + } 2.91 + } 2.92 + } 2.93 + 2.94 + if (move->fromrow != POS_UNSPECIFIED) { 2.95 + if (move->fromrow == move->torow) { 2.96 + return rook_getloc_fixedrow(board, move); 2.97 + } else { 2.98 + if (board[move->fromrow][move->tofile] == move->piece) { 2.99 + move->fromfile = move->tofile; 2.100 + return VALID_MOVE_SYNTAX; 2.101 + } else { 2.102 + return INVALID_POSITION; 2.103 + } 2.104 + } 2.105 + } 2.106 + 2.107 + Move chkrowmove = *move, chkfilemove = *move; 2.108 + 2.109 + chkrowmove.fromrow = move->torow; 2.110 + int chkrow = rook_getloc_fixedrow(board, &chkrowmove); 2.111 + 2.112 + chkfilemove.fromfile = move->tofile; 2.113 + int chkfile = rook_getloc_fixedfile(board, &chkfilemove); 2.114 + 2.115 + if ((chkrow == VALID_MOVE_SYNTAX && chkfile == VALID_MOVE_SYNTAX) || 2.116 + chkrow == AMBIGUOUS_MOVE || chkfile == AMBIGUOUS_MOVE) { 2.117 + return AMBIGUOUS_MOVE; 2.118 + } 2.119 + 2.120 + if (chkrow == VALID_MOVE_SYNTAX) { 2.121 + *move = chkrowmove; 2.122 + return VALID_MOVE_SYNTAX; 2.123 + } 2.124 + 2.125 + if (chkfile == VALID_MOVE_SYNTAX) { 2.126 + *move = chkfilemove; 2.127 + return VALID_MOVE_SYNTAX; 2.128 + } 2.129 + 2.130 + return INVALID_POSITION; 2.131 }
3.1 --- a/src/chess/rules.c Mon Mar 31 11:41:08 2014 +0200 3.2 +++ b/src/chess/rules.c Mon Mar 31 14:00:58 2014 +0200 3.3 @@ -127,6 +127,11 @@ 3.4 return 0; 3.5 } 3.6 3.7 + /* must move */ 3.8 + if (move->fromfile == move->tofile && move->fromrow == move->torow) { 3.9 + return 0; 3.10 + } 3.11 + 3.12 /* does piece exist */ 3.13 result = msrc(board, move) == move->piece; 3.14
4.1 --- a/src/game.c Mon Mar 31 11:41:08 2014 +0200 4.2 +++ b/src/game.c Mon Mar 31 14:00:58 2014 +0200 4.3 @@ -164,11 +164,11 @@ 4.4 code = net_recieve_code(opponent); 4.5 move.check = code == NETCODE_CHECK; 4.6 move.checkmate = code == NETCODE_CHECKMATE; 4.7 - addmove(movelist, &move); 4.8 if (code == NETCODE_DECLINE) { 4.9 printw("Invalid move."); 4.10 } else { 4.11 apply_move(board, &move); 4.12 + addmove(movelist, &move); 4.13 if (move.checkmate) { 4.14 printw("Checkmate!"); 4.15 clrtoeol();