src/chess/king.c

changeset 169
9962f5d98764
parent 163
2a6d83f4677e
child 170
bde99d803caf
equal deleted inserted replaced
168:663676cfef6e 169:9962f5d98764
27 * 27 *
28 */ 28 */
29 29
30 #include "rules.h" 30 #include "rules.h"
31 #include "king.h" 31 #include "king.h"
32
33 #include <string.h>
34 32
35 static bool king_castling_chkmoved( 33 static bool king_castling_chkmoved(
36 const GameState *gamestate, Row row, File file) { 34 const GameState *gamestate, Row row, File file) {
37 35
38 for (unsigned i = 0; i < gamestate->movecount; i++) { 36 for (unsigned i = 0; i < gamestate->movecount; i++) {
87 is_covered(gamestate, move->torow, midfile, opponent_color); 85 is_covered(gamestate, move->torow, midfile, opponent_color);
88 } 86 }
89 87
90 return blocked; 88 return blocked;
91 } 89 }
90
91 size_t king_moves(GameState *gamestate, Color c, Row r, File f, Move *moves) {
92
93 size_t count = 0;
94 Piece king = mkpiece(KING, c);
95
96 for (int dr = -1 ; dr <= 1 ; dr++) {
97 for (int df = -1 ; df <= 1 ; df++) {
98 if (dr == 0 && df == 0) {
99 continue;
100 }
101
102 Row torow = r + dr;
103 File tofile = f + df;
104
105 if (!isidx(torow) || !isidx(tofile)) {
106 continue;
107 }
108
109 moves[count] = (Move){0};
110 moves[count].piece = king;
111 moves[count].fromrow = r;
112 moves[count].fromfile = f;
113 moves[count].torow = torow;
114 moves[count].tofile = tofile;
115 moves[count].capture = piece_at(gamestate, torow, tofile) != 0;
116 count++;
117 }
118 }
119
120 Row homerow = c == WHITE ? 0 : 7;
121 if (r == homerow && f == fileidx('e')) {
122 moves[count] = (Move){0};
123 moves[count].piece = king;
124 moves[count].fromrow = r;
125 moves[count].fromfile = f;
126 moves[count].torow = r;
127 moves[count].tofile = fileidx('c');
128 count++;
129
130 moves[count] = (Move){0};
131 moves[count].piece = king;
132 moves[count].fromrow = r;
133 moves[count].fromfile = f;
134 moves[count].torow = r;
135 moves[count].tofile = fileidx('g');
136 count++;
137 }
138
139 return count;
140 }

mercurial