| 29 |
29 |
| 30 #include "rules.h" |
30 #include "rules.h" |
| 31 #include "rook.h" |
31 #include "rook.h" |
| 32 |
32 |
| 33 bool rook_chkrules(const Move *move) { |
33 bool rook_chkrules(const Move *move) { |
| 34 return move->torow == move->fromrow || move->tofile == move->fromfile; |
34 return move->torank == move->fromrank || move->tofile == move->fromfile; |
| 35 } |
35 } |
| 36 |
36 |
| 37 bool rook_isblocked(const GameState *gamestate, const Move *move) { |
37 bool rook_isblocked(const GameState *gamestate, const Move *move) { |
| 38 |
38 |
| 39 if (move->torow == move->fromrow) { |
39 if (move->torank == move->fromrank) { |
| 40 int d = move->tofile > move->fromfile ? 1 : -1; |
40 int d = move->tofile > move->fromfile ? 1 : -1; |
| 41 uint8_t f = move->fromfile; |
41 File f = move->fromfile; |
| 42 while (f != move->tofile-d) { |
42 while (f != move->tofile-d) { |
| 43 f += d; |
43 f += d; |
| 44 if (gamestate->board[move->fromrow][f]) { |
44 if (gamestate->board[move->fromrank][f]) { |
| 45 return true; |
45 return true; |
| 46 } |
46 } |
| 47 } |
47 } |
| 48 } else { |
48 } else { |
| 49 int d = move->torow > move->fromrow ? 1 : -1; |
49 int d = move->torank > move->fromrank ? 1 : -1; |
| 50 uint8_t r = move->fromrow; |
50 Rank r = move->fromrank; |
| 51 while (r != move->torow - d) { |
51 while (r != move->torank - d) { |
| 52 r += d; |
52 r += d; |
| 53 if (gamestate->board[r][move->fromfile]) { |
53 if (gamestate->board[r][move->fromfile]) { |
| 54 return true; |
54 return true; |
| 55 } |
55 } |
| 56 } |
56 } |
| 58 |
58 |
| 59 return false; |
59 return false; |
| 60 } |
60 } |
| 61 |
61 |
| 62 size_t rook_moves(const GameState *gamestate, |
62 size_t rook_moves(const GameState *gamestate, |
| 63 Color c, Row r, File f, Move *moves) { |
63 Color c, Rank r, File f, Move *moves) { |
| 64 size_t count = 0; |
64 size_t count = 0; |
| 65 const int directions[4][2] = { |
65 const int directions[4][2] = { |
| 66 { 1, 0}, |
66 { 1, 0}, |
| 67 {-1, 0}, |
67 {-1, 0}, |
| 68 { 0, 1}, |
68 { 0, 1}, |
| 69 { 0, -1} |
69 { 0, -1} |
| 70 }; |
70 }; |
| 71 |
71 |
| 72 for (size_t i = 0 ; i < 4 ; i++) { |
72 for (size_t i = 0 ; i < 4 ; i++) { |
| 73 int row = r + directions[i][0]; |
73 Rank rank = r + directions[i][0]; |
| 74 int file = f + directions[i][1]; |
74 File file = f + directions[i][1]; |
| 75 |
75 |
| 76 while (isidx(row) && isidx(file)) { |
76 while (isidx(rank) && isidx(file)) { |
| 77 moves[count] = (Move){0}; |
77 moves[count] = (Move){0}; |
| 78 moves[count].piece = mkpiece(ROOK, c); |
78 moves[count].piece = mkpiece(ROOK, c); |
| 79 moves[count].fromrow = r; |
79 moves[count].fromrank = r; |
| 80 moves[count].fromfile = f; |
80 moves[count].fromfile = f; |
| 81 moves[count].torow = row; |
81 moves[count].torank = rank; |
| 82 moves[count].tofile = file; |
82 moves[count].tofile = file; |
| 83 moves[count].capture = piece_at(gamestate, row, file) != 0; |
83 moves[count].capture = piece_at(gamestate, rank, file) != 0; |
| 84 count++; |
84 count++; |
| 85 |
85 |
| 86 row += directions[i][0]; |
86 rank += directions[i][0]; |
| 87 file += directions[i][1]; |
87 file += directions[i][1]; |
| 88 } |
88 } |
| 89 } |
89 } |
| 90 |
90 |
| 91 return count; |
91 return count; |