diff -r d1420f5c5704 -r 619f07c95894 src/chess/rules.h --- a/src/chess/rules.h Tue Aug 25 18:35:18 2026 +0200 +++ b/src/chess/rules.h Tue Aug 25 18:59:18 2026 +0200 @@ -52,20 +52,20 @@ #if __STDC_VERSION__ < 202310L /* since #warning is also a C23 feature, the only hope is this: */ #pragma GCC warning "Type safety for enums is only available since C23" -#define enum_uint8_t(name) enum e##name -#define typedef_enum_uint8_t(name) typedef uint8_t name +#define enum_byte(name) enum e##name +#define typedef_enum_byte(name) typedef uint8_t name #else -#define enum_uint8_t(name) enum e##name : uint8_t -#define typedef_enum_uint8_t(name) typedef enum e##name name +#define enum_byte(name) enum e##name : uint8_t +#define typedef_enum_byte(name) typedef enum e##name name #endif #define ENPASSANT_THREAT 0x40u -enum_uint8_t(Color) { +enum_byte(Color) { WHITE = 0x10u, BLACK = 0x20u, }; -typedef_enum_uint8_t(Color); +typedef_enum_byte(Color); static inline Color opponent_color(Color color) { return color == WHITE ? BLACK : WHITE; @@ -81,7 +81,7 @@ #define QUEEN 0x05u #define KING 0x06u -enum_uint8_t(Piece) { +enum_byte(Piece) { WPAWN = WHITE|PAWN, WROOK = WHITE|ROOK, WKNIGHT = WHITE|KNIGHT, @@ -95,20 +95,20 @@ BQUEEN = BLACK|QUEEN, BKING = BLACK|KING, }; -typedef_enum_uint8_t(Piece); +typedef_enum_byte(Piece); #define POS_UNSPECIFIED 255u -enum_uint8_t(Row) { +enum_byte(Rank) { RANK_1 = 0, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, RANK_UNSPECIFIED = POS_UNSPECIFIED }; -typedef_enum_uint8_t(Row); +typedef_enum_byte(Rank); -enum_uint8_t(File) { +enum_byte(File) { FILE_A = 0, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, FILE_UNSPECIFIED = POS_UNSPECIFIED }; -typedef_enum_uint8_t(File); +typedef_enum_byte(File); typedef uint8_t Board[8][8]; @@ -123,9 +123,9 @@ uint64_t movetime; /* the time for this move in microseconds */ Piece piece; File fromfile; - Row fromrow; + Rank fromrank; File tofile; - Row torow; + Rank torank; Piece promotion; bool check; /* must always be set if checkmate is set */ bool checkmate; @@ -185,37 +185,34 @@ #define piece_color(piece) ((uint8_t)(piece)&COLOR_MASK) #define mkpiece(type,color) (Piece)((type)|(color)) -#define mdst(m) (m)->torow, (m)->tofile -#define msrc(m) (m)->fromrow, (m)->fromfile - /** Checks if the index is specified and valid. */ static inline bool isidx(uint8_t idx) {return idx < 8;} /** Checks if the index is unspecified or valid. */ static inline bool isidxr(uint8_t idx) {return idx==POS_UNSPECIFIED || idx<8;} static inline bool isfile(char file) {return file >= 'a' && file <= 'h';} -static inline bool isrow(char row) {return row >= '1' && row <= '8';} +static inline bool isrank(char rank) {return rank >= '1' && rank <= '8';} -static inline Row rowidx(char row) {return row-'1';} +static inline Rank rankidx(char rank) {return rank-'1';} static inline File fileidx(char file) {return file-'a';} -static inline char rowchr(Row row) {return (char)row+'1';} +static inline char rankchr(Rank rank) {return (char)rank+'1';} static inline char filechr(File file) {return (char)file+'a';} static inline void enpassant_threat_add(GameState *gamestate, - Row row, File file) { - gamestate->board[row][file] |= ENPASSANT_THREAT; + Rank rank, File file) { + gamestate->board[rank][file] |= ENPASSANT_THREAT; } static inline void enpassant_threat_remove(GameState *gamestate, - Row row, File file) { - gamestate->board[row][file] &= ~ENPASSANT_THREAT; + Rank rank, File file) { + gamestate->board[rank][file] &= ~ENPASSANT_THREAT; } static inline bool enpassant_threat_exists(const GameState *gamestate, - Row row, File file) { - return gamestate->board[row][file] & ENPASSANT_THREAT; + Rank rank, File file) { + return gamestate->board[rank][file] & ENPASSANT_THREAT; } static inline bool is_game_drawn(const GameState *gamestate) { @@ -232,7 +229,7 @@ return gamestate->moves[gamestate->movecount - 1].check; } -static inline Color field_color(Row r, File f) { +static inline Color field_color(Rank r, File f) { return (r + f) % 2 == 0 ? BLACK : WHITE; } @@ -292,35 +289,35 @@ * Returns the piece at the specified position. * * @param gamestate the current game state - * @param row the row + * @param rank the rank * @param file the file * @return the piece at the specified position */ -Piece piece_at(const GameState *gamestate, Row row, File file); +Piece piece_at(const GameState *gamestate, Rank rank, File file); /** * Places a piece at the specified position in the current game state. * * @param gamestate the current game state - * @param row the row + * @param rank the rank * @param file the file * @param piece the piece to place at the specified position */ -void piece_set(GameState *gamestate, Row row, File file, Piece piece); +void piece_set(GameState *gamestate, Rank rank, File file, Piece piece); /** * Removes the piece at the specified position in the current game state. * * @param gamestate the current game state - * @param row the row + * @param rank the rank * @param file the file */ -static inline void piece_remove(GameState *gamestate, Row row, File file) { - piece_set(gamestate, row, file, 0); +static inline void piece_remove(GameState *gamestate, Rank rank, File file) { + piece_set(gamestate, rank, file, 0); } typedef size_t(*moves_generator_func)(const GameState *gamestate, - Color c, Row r, File f, Move *moves); + Color c, Rank r, File f, Move *moves); /** * Calculates all allowed moves for a specific piece. @@ -328,13 +325,13 @@ * Use the macros for the specific pieces instead. * * @param gamestate the current gamestate - * @param r the row of the piece + * @param r the rank of the piece * @param f the file of the piece * @param moves target array for the list of moves * @return the number of moves stored in the @p moves array */ size_t piece_moves_allowed(const GameState *gamestate, - Row r, File f, Move *moves); + Rank r, File f, Move *moves); /** * Internal function used to filter out illegal moves. @@ -343,14 +340,14 @@ * * @param gamestate the current gamestate * @param c color of the piece - * @param r the row of the piece + * @param r the rank of the piece * @param f the file of the piece * @param moves target array for the list of moves * @param func a function that unconditionally generates the moves * @return the number of moves stored in the @p moves array */ size_t filter_moves_allowed(const GameState *gamestate, - Color c, Row r, File f, Move *moves, moves_generator_func func); + Color c, Rank r, File f, Move *moves, moves_generator_func func); /** * Determines a list of theoretically possible moves to the specified field. @@ -362,7 +359,7 @@ * must be set, too. * * @param gamestate the current game state - * @param row row of the field to check + * @param rank rank of the field to check * @param file file of the field to check * @param color the color of the piece that should move to the field * @param moves the array where to store the moves @@ -371,7 +368,7 @@ * @return true, if any piece of the specified color can move to the specified * field regardless of being pinned */ -bool get_candidates(const GameState *gamestate, Row row, File file, +bool get_candidates(const GameState *gamestate, Rank rank, File file, Color color, Move* moves, size_t* movecount); /** @@ -385,7 +382,7 @@ * must be set, too. * * @param gamestate the current game state - * @param row row of the field to check + * @param rank rank of the field to check * @param file file of the field to check * @param color the color of the piece that should move to the field * @param moves the array where to store the moves @@ -394,7 +391,7 @@ * @return true, if any piece of the specified color can move to the specified * field and is not pinned */ -bool get_real_candidates(const GameState *gamestate, Row row, File file, +bool get_real_candidates(const GameState *gamestate, Rank rank, File file, Color color, Move* moves, size_t* movecount); /** @@ -407,7 +404,7 @@ * must be set, too. * * @param gamestate the current game state - * @param row row of the field to check + * @param rank rank of the field to check * @param file file of the field to check * @param color the color of the piece that should threaten the field * @param threats the array where to store the threats @@ -416,7 +413,7 @@ * @return true, if any piece of the specified color threatens the specified * field */ -bool get_threats(const GameState *gamestate, Row row, File file, +bool get_threats(const GameState *gamestate, Rank rank, File file, Color color, Move* threats, size_t* threatcount); /** @@ -427,7 +424,7 @@ * must be set, too. * * @param gamestate the current game state - * @param row row of the field to check + * @param rank rank of the field to check * @param file file of the field to check * @param color the color of the piece that should threaten the field * @param threats the array where to store the threats @@ -436,7 +433,7 @@ * @return true, if any piece of the specified color threatens the specified * field and is not pinned */ -bool get_real_threats(const GameState *gamestate, Row row, File file, +bool get_real_threats(const GameState *gamestate, Rank rank, File file, Color color, Move* threats, size_t* threatcount); /** @@ -446,14 +443,14 @@ * capture an opponent piece on this field, regardless of being pinned. * * @param gamestate the current game state - * @param row row of the field to check + * @param rank rank of the field to check * @param file file of the field to check * @param color the color of the piece that should cover the field * @return true, if any piece of the specified color threatens the specified * field */ -#define is_covered(gamestate, row, file, color) \ - get_threats(gamestate, row, file, color, NULL, NULL) +#define is_covered(gamestate, rank, file, color) \ + get_threats(gamestate, rank, file, color, NULL, NULL) /** * Checks, if a specified field is attacked by a piece of a certain color. @@ -462,14 +459,14 @@ * therefore able to perform the move. * * @param gamestate the current game state - * @param row row of the field to check + * @param rank rank of the field to check * @param file file of the field to check * @param color the color of the piece that should cover the field * @return true, if any piece of the specified color threatens the specified * field and could capture an opponent piece */ -#define is_attacked(gamestate, row, file, color) \ - get_real_threats(gamestate, row, file, color, NULL, NULL) +#define is_attacked(gamestate, rank, file, color) \ + get_real_threats(gamestate, rank, file, color, NULL, NULL) /** * Checks, if a specified field is protected by a piece of a certain color. @@ -478,13 +475,13 @@ * that field or move to that field (and is not pinned). * * @param gamestate the current game state - * @param row row of the field to check + * @param rank rank of the field to check * @param file file of the field to check * @param color the color of the piece that should cover the field * @return true, if any piece (excluding the king) of the specified color * can move to the specified field (including capturing moves) */ -bool is_protected(const GameState *gamestate, Row row, File file, Color color); +bool is_protected(const GameState *gamestate, Rank rank, File file, Color color); /** * Checks, if the specified move cannot be performed, because the piece is