src/chess/knight.c

changeset 169
9962f5d98764
parent 163
2a6d83f4677e
child 170
bde99d803caf
equal deleted inserted replaced
168:663676cfef6e 169:9962f5d98764
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->fromrow - move->torow);
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
41 size_t knight_moves(GameState *gamestate, Color c, Row r, File f, Move *moves) {
42
43 size_t count = 0;
44 const int offsets[8][2] = {
45 { 2, 1},
46 { 2, -1},
47 {-2, 1},
48 {-2, -1},
49 { 1, 2},
50 { 1, -2},
51 {-1, 2},
52 {-1, -2}
53 };
54
55 for (size_t i = 0 ; i < 8 ; i++) {
56 int row = r + offsets[i][0];
57 int file = f + offsets[i][1];
58
59 if (isidx(row) && isidx(file)) {
60 moves[count] = (Move){0};
61 moves[count].piece = mkpiece(KNIGHT, c);
62 moves[count].fromrow = r;
63 moves[count].fromfile = f;
64 moves[count].torow = row;
65 moves[count].tofile = file;
66 moves[count].capture = piece_at(gamestate, row, file) != 0;
67 count++;
68 }
69 }
70
71 return count;
72 }

mercurial