| 31 #include "rules.h" |
31 #include "rules.h" |
| 32 #include <stdlib.h> |
32 #include <stdlib.h> |
| 33 |
33 |
| 34 bool knight_chkrules(const Move *move) { |
34 bool knight_chkrules(const Move *move) { |
| 35 int dx = abs(move->fromfile - move->tofile); |
35 int dx = abs(move->fromfile - move->tofile); |
| 36 int dy = abs(move->fromrow - move->torow); |
36 int dy = abs(move->fromrank - move->torank); |
| 37 |
37 |
| 38 return (dx == 2 && dy == 1) || (dx == 1 && dy == 2); |
38 return (dx == 2 && dy == 1) || (dx == 1 && dy == 2); |
| 39 } |
39 } |
| 40 |
40 |
| 41 size_t knight_moves(const GameState *gamestate, |
41 size_t knight_moves(const GameState *gamestate, |
| 42 Color c, Row r, File f, Move *moves) { |
42 Color c, Rank r, File f, Move *moves) { |
| 43 |
43 |
| 44 size_t count = 0; |
44 size_t count = 0; |
| 45 const int offsets[8][2] = { |
45 const int offsets[8][2] = { |
| 46 { 2, 1}, |
46 { 2, 1}, |
| 47 { 2, -1}, |
47 { 2, -1}, |
| 52 {-1, 2}, |
52 {-1, 2}, |
| 53 {-1, -2} |
53 {-1, -2} |
| 54 }; |
54 }; |
| 55 |
55 |
| 56 for (size_t i = 0 ; i < 8 ; i++) { |
56 for (size_t i = 0 ; i < 8 ; i++) { |
| 57 int row = r + offsets[i][0]; |
57 Rank rank = r + offsets[i][0]; |
| 58 int file = f + offsets[i][1]; |
58 File file = f + offsets[i][1]; |
| 59 |
59 |
| 60 if (isidx(row) && isidx(file)) { |
60 if (isidx(rank) && isidx(file)) { |
| 61 moves[count] = (Move){0}; |
61 moves[count] = (Move){0}; |
| 62 moves[count].piece = mkpiece(KNIGHT, c); |
62 moves[count].piece = mkpiece(KNIGHT, c); |
| 63 moves[count].fromrow = r; |
63 moves[count].fromrank = r; |
| 64 moves[count].fromfile = f; |
64 moves[count].fromfile = f; |
| 65 moves[count].torow = row; |
65 moves[count].torank = rank; |
| 66 moves[count].tofile = file; |
66 moves[count].tofile = file; |
| 67 moves[count].capture = piece_at(gamestate, row, file) != 0; |
67 moves[count].capture = piece_at(gamestate, rank, file) != 0; |
| 68 count++; |
68 count++; |
| 69 } |
69 } |
| 70 } |
70 } |
| 71 |
71 |
| 72 return count; |
72 return count; |