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