src/chess/rules.h

changeset 179
5ef724e21702
parent 173
e541b6002933
equal deleted inserted replaced
178:724e8acff6f8 179:5ef724e21702
142 /** a premove that shall be evaluated next time it's our turn */ 142 /** a premove that shall be evaluated next time it's our turn */
143 char premove[8]; 143 char premove[8];
144 bool checkmate; 144 bool checkmate;
145 bool stalemate; 145 bool stalemate;
146 bool threefold; 146 bool threefold;
147 /** drawn due to insufficient material on both sides */
148 bool nomaterial;
147 /** flag is only set when players agreed on remis */ 149 /** flag is only set when players agreed on remis */
148 bool remis; 150 bool remis;
149 bool wresign; 151 bool wresign;
150 bool bresign; 152 bool bresign;
151 /** this flag is only supposed to be set when the opponent disconnects */ 153 /** this flag is only supposed to be set when the opponent disconnects */
194 Row row, File file) { 196 Row row, File file) {
195 return gamestate->board[row][file] & ENPASSANT_THREAT; 197 return gamestate->board[row][file] & ENPASSANT_THREAT;
196 } 198 }
197 199
198 static inline bool is_game_drawn(const GameState *gamestate) { 200 static inline bool is_game_drawn(const GameState *gamestate) {
199 return gamestate->threefold || gamestate->stalemate || gamestate->remis; 201 return gamestate->threefold || gamestate->stalemate
202 || gamestate->nomaterial || gamestate->remis;
200 } 203 }
201 204
202 static inline bool is_game_running(const GameState *gamestate) { 205 static inline bool is_game_running(const GameState *gamestate) {
203 return !(gamestate->checkmate || gamestate->wresign || gamestate->bresign 206 return !(gamestate->checkmate || gamestate->wresign || gamestate->bresign
204 || is_game_drawn(gamestate) || gamestate->review); 207 || is_game_drawn(gamestate) || gamestate->review);
205 } 208 }
206 209
207 static inline bool is_check_position(const GameState *gamestate) { 210 static inline bool is_check_position(const GameState *gamestate) {
208 return gamestate->moves[gamestate->movecount - 1].check; 211 return gamestate->moves[gamestate->movecount - 1].check;
212 }
213
214 static inline Color field_color(Row r, File f) {
215 return (r + f) % 2 == 0 ? BLACK : WHITE;
209 } 216 }
210 217
211 /** 218 /**
212 * Initializes a game state and prepares the chess board. 219 * Initializes a game state and prepares the chess board.
213 * @param gamestate the game state to initialize 220 * @param gamestate the game state to initialize
598 * @param gamestate the current game state 605 * @param gamestate the current game state
599 * @return true if the game is in a threefold repetition position 606 * @return true if the game is in a threefold repetition position
600 */ 607 */
601 bool check_threefold_repetition(const GameState *gamestate); 608 bool check_threefold_repetition(const GameState *gamestate);
602 609
610 /**
611 * Checks if neither side has enough material left to win the game.
612 *
613 * Returns true if for both players one of the following three cases are true:
614 * - they have only the king left
615 * - they have a king + knight and the opponent has king + queens
616 * - they have a king + bishop and the opponent doesn't have
617 * opposite color bishops or knights or pawns
618 *
619 * @param gamestate the current game state
620 * @return true if neither side can win due to insufficient material
621 */
622 bool check_no_material(const GameState *gamestate);
623
603 #endif /* RULES_H */ 624 #endif /* RULES_H */
604 625

mercurial