Tue, 25 Aug 2026 19:42:15 +0200
flip File and Rank parameters into correct order
resolves #956
--- a/src/chess/bishop.c Tue Aug 25 18:59:18 2026 +0200 +++ b/src/chess/bishop.c Tue Aug 25 19:42:15 2026 +0200 @@ -54,7 +54,7 @@ } size_t bishop_moves(const GameState *gamestate, - Color c, Rank r, File f, Move *moves) { + Color c, File f, Rank r, Move *moves) { size_t count = 0; const int directions[4][2] = { @@ -75,7 +75,7 @@ moves[count].fromfile = f; moves[count].torank = rank; moves[count].tofile = file; - moves[count].capture = piece_at(gamestate, rank, file) != 0; + moves[count].capture = piece_at(gamestate, file, rank) != 0; count++; rank += directions[i][0];
--- a/src/chess/bishop.h Tue Aug 25 18:59:18 2026 +0200 +++ b/src/chess/bishop.h Tue Aug 25 19:42:15 2026 +0200 @@ -41,9 +41,9 @@ #define BISHOP_MOVES_MAX 13 size_t bishop_moves(const GameState *gamestate, - Color c, Rank r, File f, Move *moves); -#define bishop_moves_allowed(gamestate, color, rank, file, moves) \ - filter_moves_allowed(gamestate, color, rank, file, moves, bishop_moves) + Color c, File f, Rank r, Move *moves); +#define bishop_moves_allowed(gamestate, color, file, rank, moves) \ + filter_moves_allowed(gamestate, color, file, rank, moves, bishop_moves) #ifdef __cplusplus }
--- a/src/chess/fen.c Tue Aug 25 18:59:18 2026 +0200 +++ b/src/chess/fen.c Tue Aug 25 19:42:15 2026 +0200 @@ -43,7 +43,7 @@ str[i++] = '0'+skip; skip = 0; } - switch (piece_at(gamestate, rank, file)) { + switch (piece_at(gamestate, file, rank)) { case WHITE|KING: str[i++] = 'K'; break; case WHITE|QUEEN: str[i++] = 'Q'; break; case WHITE|BISHOP: str[i++] = 'B'; break; @@ -121,11 +121,11 @@ str[0] = '-'; for (int file = 0 ; file < 8 ; file++) { - if (enpassant_threat_exists(gamestate, 3, file)) { + if (enpassant_threat_exists(gamestate, file, 3)) { str[0] = filechr(file); str[1] = rankchr(2); } - if (enpassant_threat_exists(gamestate, 4, file)) { + if (enpassant_threat_exists(gamestate, file, 4)) { str[0] = filechr(file); str[1] = rankchr(5); }
--- a/src/chess/king.c Tue Aug 25 18:59:18 2026 +0200 +++ b/src/chess/king.c Tue Aug 25 19:42:15 2026 +0200 @@ -31,7 +31,7 @@ #include "king.h" static bool king_castling_chkmoved( - const GameState *gamestate, Rank rank, File file) { + const GameState *gamestate, File file, Rank rank) { for (unsigned i = 0; i < gamestate->movecount; i++) { if (gamestate->moves[i].fromfile == file @@ -55,9 +55,9 @@ (move->tofile == fileidx('c') || move->tofile == fileidx('g'))) { return !king_castling_chkmoved(gamestate, - move->fromrank, move->fromfile) && - !king_castling_chkmoved(gamestate, move->fromrank, - move->tofile == fileidx('c') ? 0 : 7); + move->fromfile, move->fromrank) && + !king_castling_chkmoved(gamestate, + move->tofile == fileidx('c') ? 0 : 7, move->fromrank); } else { return false; } @@ -82,14 +82,14 @@ incheck = is_check_position(gamestate); } blocked |= incheck || gamestate->board[move->torank][midfile] || - is_covered(gamestate, move->torank, midfile, op_color); + is_covered(gamestate, midfile, move->torank, op_color); } return blocked; } size_t king_moves(const GameState *gamestate, - Color c, Rank r, File f, Move *moves) { + Color c, File f, Rank r, Move *moves) { size_t count = 0; Piece king = mkpiece(KING, c); @@ -113,7 +113,7 @@ moves[count].fromfile = f; moves[count].torank = torank; moves[count].tofile = tofile; - moves[count].capture = piece_at(gamestate, torank, tofile) != 0; + moves[count].capture = piece_at(gamestate, tofile, torank) != 0; count++; } }
--- a/src/chess/king.h Tue Aug 25 18:59:18 2026 +0200 +++ b/src/chess/king.h Tue Aug 25 19:42:15 2026 +0200 @@ -42,9 +42,9 @@ #define KING_MOVES_MAX 8 size_t king_moves(const GameState *gamestate, - Color c, Rank r, File f, Move *moves); -#define king_moves_allowed(gamestate, color, rank, file, moves) \ - filter_moves_allowed(gamestate, color, rank, file, moves, king_moves) + Color c, File f, Rank r, Move *moves); +#define king_moves_allowed(gamestate, color, file, rank, moves) \ + filter_moves_allowed(gamestate, color, file, rank, moves, king_moves) #ifdef __cplusplus }
--- a/src/chess/knight.c Tue Aug 25 18:59:18 2026 +0200 +++ b/src/chess/knight.c Tue Aug 25 19:42:15 2026 +0200 @@ -39,7 +39,7 @@ } size_t knight_moves(const GameState *gamestate, - Color c, Rank r, File f, Move *moves) { + Color c, File f, Rank r, Move *moves) { size_t count = 0; const int offsets[8][2] = { @@ -64,7 +64,7 @@ moves[count].fromfile = f; moves[count].torank = rank; moves[count].tofile = file; - moves[count].capture = piece_at(gamestate, rank, file) != 0; + moves[count].capture = piece_at(gamestate, file, rank) != 0; count++; } }
--- a/src/chess/knight.h Tue Aug 25 18:59:18 2026 +0200 +++ b/src/chess/knight.h Tue Aug 25 19:42:15 2026 +0200 @@ -41,9 +41,9 @@ #define KNIGHT_MOVES_MAX 8 size_t knight_moves(const GameState *gamestate, - Color c, Rank r, File f, Move *moves); -#define knight_moves_allowed(gamestate, color, rank, file, moves) \ - filter_moves_allowed(gamestate, color, rank, file, moves, knight_moves) + Color c, File f, Rank r, Move *moves); +#define knight_moves_allowed(gamestate, color, file, rank, moves) \ + filter_moves_allowed(gamestate, color, file, rank, moves, knight_moves) #ifdef __cplusplus }
--- a/src/chess/pawn.c Tue Aug 25 18:59:18 2026 +0200 +++ b/src/chess/pawn.c Tue Aug 25 19:42:15 2026 +0200 @@ -53,8 +53,9 @@ move->fromfile == move->tofile + 1 || move->fromfile == move->tofile - 1)) { - return piece_at(gamestate, move->torank, move->tofile) || - enpassant_threat_exists(gamestate, move->fromrank, move->tofile); + return piece_at(gamestate, move->tofile, move->torank) || + enpassant_threat_exists(gamestate, + move->tofile, move->fromrank); } else { return false; } @@ -72,16 +73,16 @@ bool pawn_isblocked(const GameState *gamestate, const Move *move) { if (move->torank == move->fromrank + 1 || move->torank == move->fromrank - 1) { - return piece_at(gamestate, move->torank, move->tofile) + return piece_at(gamestate, move->tofile, move->torank) && !move->capture; } else { - return piece_at(gamestate, move->torank, move->tofile) || + return piece_at(gamestate, move->tofile, move->torank) || gamestate->board[(move->fromrank + move->torank) / 2][move->tofile]; } } size_t pawn_moves(const GameState *gamestate, - Color c, Rank r, File f, Move *moves) { + Color c, File f, Rank r, Move *moves) { (void) gamestate; /* no (theoretical) move depends on the game state */ size_t count = 0; int rankdelta = c == WHITE ? 1 : -1;
--- a/src/chess/pawn.h Tue Aug 25 18:59:18 2026 +0200 +++ b/src/chess/pawn.h Tue Aug 25 19:42:15 2026 +0200 @@ -41,9 +41,9 @@ #define PAWN_MOVES_MAX 4 size_t pawn_moves(const GameState *gamestate, - Color c, Rank r, File f, Move *moves); -#define pawn_moves_allowed(gamestate, color, rank, file, moves) \ - filter_moves_allowed(gamestate, color, rank, file, moves, pawn_moves) + Color c, File f, Rank r, Move *moves); +#define pawn_moves_allowed(gamestate, color, file, rank, moves) \ + filter_moves_allowed(gamestate, color, file, rank, moves, pawn_moves) #ifdef __cplusplus }
--- a/src/chess/queen.c Tue Aug 25 18:59:18 2026 +0200 +++ b/src/chess/queen.c Tue Aug 25 19:42:15 2026 +0200 @@ -45,7 +45,7 @@ } size_t queen_moves(const GameState *gamestate, - Color c, Rank r, File f, Move *moves) { + Color c, File f, Rank r, Move *moves) { size_t count = 0; const int directions[8][2] = { @@ -70,7 +70,7 @@ moves[count].fromfile = f; moves[count].torank = rank; moves[count].tofile = file; - moves[count].capture = piece_at(gamestate, rank, file) != 0; + moves[count].capture = piece_at(gamestate, file, rank) != 0; count++; rank += directions[i][0];
--- a/src/chess/queen.h Tue Aug 25 18:59:18 2026 +0200 +++ b/src/chess/queen.h Tue Aug 25 19:42:15 2026 +0200 @@ -41,9 +41,9 @@ #define QUEEN_MOVES_MAX 27 size_t queen_moves(const GameState *gamestate, - Color c, Rank r, File f, Move *moves); -#define queen_moves_allowed(gamestate, color, rank, file, moves) \ - filter_moves_allowed(gamestate, color, rank, file, moves, queen_moves) + Color c, File f, Rank r, Move *moves); +#define queen_moves_allowed(gamestate, color, file, rank, moves) \ + filter_moves_allowed(gamestate, color, file, rank, moves, queen_moves) #ifdef __cplusplus }
--- a/src/chess/rook.c Tue Aug 25 18:59:18 2026 +0200 +++ b/src/chess/rook.c Tue Aug 25 19:42:15 2026 +0200 @@ -60,7 +60,7 @@ } size_t rook_moves(const GameState *gamestate, - Color c, Rank r, File f, Move *moves) { + Color c, File f, Rank r, Move *moves) { size_t count = 0; const int directions[4][2] = { { 1, 0}, @@ -80,7 +80,7 @@ moves[count].fromfile = f; moves[count].torank = rank; moves[count].tofile = file; - moves[count].capture = piece_at(gamestate, rank, file) != 0; + moves[count].capture = piece_at(gamestate, file, rank) != 0; count++; rank += directions[i][0];
--- a/src/chess/rook.h Tue Aug 25 18:59:18 2026 +0200 +++ b/src/chess/rook.h Tue Aug 25 19:42:15 2026 +0200 @@ -41,9 +41,9 @@ #define ROOK_MOVES_MAX 14 size_t rook_moves(const GameState *gamestate, - Color c, Rank r, File f, Move *moves); -#define rook_moves_allowed(gamestate, color, rank, file, moves) \ - filter_moves_allowed(gamestate, color, rank, file, moves, rook_moves) + Color c, File f, Rank r, Move *moves); +#define rook_moves_allowed(gamestate, color, file, rank, moves) \ + filter_moves_allowed(gamestate, color, file, rank, moves, rook_moves) #ifdef __cplusplus }
--- a/src/chess/rules.c Tue Aug 25 18:59:18 2026 +0200 +++ b/src/chess/rules.c Tue Aug 25 19:42:15 2026 +0200 @@ -126,22 +126,22 @@ } size_t piece_moves_allowed(const GameState *gamestate, - Rank r, File f, Move *moves) { - Piece p = piece_at(gamestate, r, f); + File f, Rank r, Move *moves) { + Piece p = piece_at(gamestate, f, r); Color c = piece_color(p); switch (piece_type(p)) { case KING: - return king_moves_allowed(gamestate, c, r, f, moves); + return king_moves_allowed(gamestate, c, f, r, moves); case QUEEN: - return queen_moves_allowed(gamestate, c, r, f, moves); + return queen_moves_allowed(gamestate, c, f, r, moves); case ROOK: - return rook_moves_allowed(gamestate, c, r, f, moves); + return rook_moves_allowed(gamestate, c, f, r, moves); case KNIGHT: - return knight_moves_allowed(gamestate, c, r, f, moves); + return knight_moves_allowed(gamestate, c, f, r, moves); case BISHOP: - return bishop_moves_allowed(gamestate, c, r, f, moves); + return bishop_moves_allowed(gamestate, c, f, r, moves); case PAWN: - return pawn_moves_allowed(gamestate, c, r, f, moves); + return pawn_moves_allowed(gamestate, c, f, r, moves); default: return 0; } @@ -154,8 +154,8 @@ Move moves[QUEEN_MOVES_MAX]; for (Rank r = 0; r < 8; r++) { for (File f = 0; f < 8; f++) { - if (piece_color(piece_at(gamestate, r, f)) == next_player - && piece_moves_allowed(gamestate, r, f, moves) > 0) { + if (piece_color(piece_at(gamestate, f, r)) == next_player + && piece_moves_allowed(gamestate, f, r, moves) > 0) { return false; } } @@ -205,11 +205,11 @@ bool op_has_bbishop = false, op_has_wbishop = false; for (Rank r = 0 ; r < 8 ; r++) { for (File f = 0 ; f < 8 ; f++) { - Piece p = piece_at(gamestate, r, f); + Piece p = piece_at(gamestate, f, r); if (piece_color(p) == color) { piece_count[piece_type(p)]++; if (piece_type(p) == BISHOP) { - if (field_color(r, f) == WHITE) { + if (field_color(f, r) == WHITE) { has_wbishop = true; } else { has_bbishop = true; @@ -218,7 +218,7 @@ } else { op_piece_count[piece_type(p)]++; if (piece_type(p) == BISHOP) { - if (field_color(r, f) == WHITE) { + if (field_color(f, r) == WHITE) { op_has_wbishop = true; } else { op_has_bbishop = true; @@ -320,29 +320,29 @@ static void apply_move_internal(GameState *gamestate, Move *move) { /* en passant capture */ if (move->capture && piece_type(move->piece) == PAWN && - piece_at(gamestate, move->torank, move->tofile) == 0) { - piece_remove(gamestate, move->fromrank, move->tofile); + piece_at(gamestate, move->tofile, move->torank) == 0) { + piece_remove(gamestate, move->tofile, move->fromrank); } /* remove old en passant threats */ for (File file = 0 ; file < 8 ; file++) { - enpassant_threat_remove(gamestate, 3, file); - enpassant_threat_remove(gamestate, 4, file); + enpassant_threat_remove(gamestate, file, 3); + enpassant_threat_remove(gamestate, file, 4); } /* move (and maybe capture or promote) */ - piece_remove(gamestate, move->fromrank, move->fromfile); + piece_remove(gamestate, move->fromfile, move->fromrank); if (move->promotion) { - piece_set(gamestate, move->torank, move->tofile, move->promotion); + piece_set(gamestate, move->tofile, move->torank, move->promotion); } else { - piece_set(gamestate, move->torank, move->tofile, move->piece); + piece_set(gamestate, move->tofile, move->torank, move->piece); } /* add new en passant threat */ if (piece_type(move->piece) == PAWN && ( (move->fromrank == 1 && move->torank == 3) || (move->fromrank == 6 && move->torank == 4))) { - enpassant_threat_add(gamestate, move->torank, move->tofile); + enpassant_threat_add(gamestate, move->tofile, move->torank); } /* castling */ @@ -424,7 +424,7 @@ Rank opkingrank = 0; for (Rank rank = 0 ; rank < 8 ; rank++) { for (File file = 0 ; file < 8 ; file++) { - Piece p = piece_at(&simulation, rank, file); + Piece p = piece_at(&simulation, file, rank); if (p == mkpiece(KING, oppcolor)) { opkingfile = file; opkingrank = rank; @@ -435,7 +435,7 @@ /* determine if the opposing king is now threatened */ Move threats[16]; size_t threatcount; - bool incheck = get_threats(&simulation, opkingrank, opkingfile, + bool incheck = get_threats(&simulation, opkingfile, opkingrank, piececolor, threats, &threatcount); if (!incheck) { @@ -457,7 +457,7 @@ continue; /* check if escape field is already covered (threatened) */ - if (is_covered(&simulation, er, ef, piececolor)) + if (is_covered(&simulation, ef, er, piececolor)) continue; /* check if an attacking piece blocks the field */ @@ -472,7 +472,7 @@ move_retaliate.tofile = ef; move_retaliate.capture = true; apply_move_internal(&sim_retaliate, &move_retaliate); - canescape = !is_covered(&sim_retaliate, er, ef, piececolor); + canescape = !is_covered(&sim_retaliate, ef, er, piececolor); gamestate_cleanup(&sim_retaliate); continue; } @@ -485,7 +485,7 @@ /* can't escape, can the king be rescued? */ if (!canescape && threatcount == 1) { canescape = is_protected(&simulation, - threats[0].fromrank, threats[0].fromfile, oppcolor); + threats[0].fromfile, threats[0].fromrank, oppcolor); } /* can't capture, can he block? */ @@ -502,7 +502,7 @@ while (!canescape && file != threat->tofile - d) { file += d; canescape |= is_protected(&simulation, - threat->torank, file, oppcolor); + file, threat->torank, oppcolor); } } else if (threat->fromfile == threat->tofile) { /* rook aspect (on file) */ @@ -511,7 +511,7 @@ while (!canescape && rank != threat->torank - d) { rank += d; canescape |= is_protected(&simulation, - rank, threat->tofile, oppcolor); + threat->tofile, rank, oppcolor); } } else { /* bishop aspect */ @@ -524,8 +524,8 @@ && rank != threat->torank - dr) { rank += dr; file += df; - canescape |= is_protected(&simulation, rank, file, - oppcolor); + canescape |= is_protected(&simulation, + file, rank, oppcolor); } } } @@ -566,7 +566,7 @@ /* resolve ambiguities, if any */ Move candidates[16]; size_t ccount; - if (get_real_candidates(gamestate, move->torank, move->tofile, + if (get_real_candidates(gamestate, move->tofile, move->torank, piece_color(move->piece), candidates, &ccount)) { unsigned int ambranks = 0, ambfiles = 0, ambpiece = 0; for (size_t i = 0 ; i < ccount ; i++) { @@ -637,12 +637,12 @@ } /* does piece exist */ - if (piece_at(gamestate, move->fromrank, move->fromfile) != move->piece) { + if (piece_at(gamestate, move->fromfile, move->fromrank) != move->piece) { return PIECE_NOT_FOUND; } /* is there any piece at the destination? */ - Piece piece_at_dst = piece_at(gamestate, move->torank, move->tofile); + Piece piece_at_dst = piece_at(gamestate, move->tofile, move->torank); /* can't capture own pieces */ if (piece_color(piece_at_dst) == piece_color(move->piece)) { @@ -654,7 +654,7 @@ /* ... or the capture happens en passant */ if (!move->capture || piece_type(move->piece) != PAWN || !enpassant_threat_exists(gamestate, - move->fromrank, move->tofile)) { + move->tofile, move->fromrank)) { return RULES_VIOLATED; } } @@ -709,14 +709,14 @@ Rank kingrank = 0; for (Rank rank = 0 ; rank < 8 ; rank++) { for (File file = 0 ; file < 8 ; file++) { - Piece p = piece_at(&simulation, rank, file); + Piece p = piece_at(&simulation, file, rank); if (p == mkpiece(KING, piececolor)) { kingfile = file; kingrank = rank; } } } - if (is_covered(&simulation, kingrank, kingfile, oppcolor)) { + if (is_covered(&simulation, kingfile, kingrank, oppcolor)) { if (piece_type(move->piece) == KING) { result = KING_MOVES_INTO_CHECK; } else { @@ -752,21 +752,21 @@ return VALID_MOVE_SEMANTICS; } -Piece piece_at(const GameState *gamestate, Rank rank, File file) { +Piece piece_at(const GameState *gamestate, File file, Rank rank) { return gamestate->board[rank][file] & (PIECE_MASK|COLOR_MASK); } -void piece_set(GameState *gamestate, Rank rank, File file, Piece piece) { +void piece_set(GameState *gamestate, File file, Rank rank, Piece piece) { gamestate->board[rank][file] = piece; } -bool get_candidates(const GameState *gamestate, Rank rank, File file, +bool get_candidates(const GameState *gamestate, File file, Rank rank, Color color, Move *moves, size_t *movecount) { Move candidates[32]; size_t ccount = 0; for (Rank r = 0 ; r < 8 ; r++) { for (File f = 0 ; f < 8 ; f++) { - Piece p = piece_at(gamestate, r, f); + Piece p = piece_at(gamestate, f, r); if (piece_color(p) == color) { /* non-capturing move */ memset(&(candidates[ccount]), 0, sizeof(Move)); @@ -809,22 +809,22 @@ return result; } -bool get_threats(const GameState *gamestate, Rank rank, File file, +bool get_threats(const GameState *gamestate, File file, Rank rank, Color color, Move *threats, size_t *threatcount) { /* simulate a capturing move on the target position */ Color opcolor = opponent_color(color); GameState simulation = gamestate_copy_sim(gamestate); - if (piece_color(piece_at(&simulation, rank, file)) != opcolor) { + if (piece_color(piece_at(&simulation, file, rank)) != opcolor) { /* set a fake pawn if the field is not occupied by the opponent */ - piece_set(&simulation, rank, file, mkpiece(PAWN, opcolor)); + piece_set(&simulation, file, rank, mkpiece(PAWN, opcolor)); } Move candidates[16]; size_t ccount = 0; for (Rank r = 0 ; r < 8 ; r++) { for (File f = 0 ; f < 8 ; f++) { - Piece p = piece_at(&simulation, r, f); + Piece p = piece_at(&simulation, f, r); if (piece_color(p) == color) { memset(&(candidates[ccount]), 0, sizeof(Move)); candidates[ccount].piece = p; @@ -874,7 +874,7 @@ Rank kingrank = 0; for (Rank rank = 0 ; rank < 8 ; rank++) { for (File file = 0 ; file < 8 ; file++) { - if (piece_at(&simulation, rank, file) == mkpiece(KING, color)) { + if (piece_at(&simulation, file, rank) == mkpiece(KING, color)) { kingfile = file; kingrank = rank; } @@ -882,13 +882,13 @@ } bool covered = is_covered(&simulation, - kingrank, kingfile, opponent_color(color)); + kingfile, kingrank, opponent_color(color)); gamestate_cleanup(&simulation); return covered; } -bool get_real_candidates(const GameState *gamestate, Rank rank, File file, +bool get_real_candidates(const GameState *gamestate, File file, Rank rank, Color color, Move *moves, size_t *movecount) { if (movecount) { @@ -897,7 +897,7 @@ Move candidates[16]; size_t ccount; - if (get_candidates(gamestate, rank, file, color, candidates, &ccount)) { + if (get_candidates(gamestate, file, rank, color, candidates, &ccount)) { bool result = false; for (size_t i = 0 ; i < ccount ; i++) { if (!is_pinned(gamestate, &candidates[i])) { @@ -913,7 +913,7 @@ } } -bool get_real_threats(const GameState *gamestate, Rank rank, File file, +bool get_real_threats(const GameState *gamestate, File file, Rank rank, Color color, Move *threats, size_t *threatcount) { if (threatcount) { @@ -922,7 +922,7 @@ Move candidates[16]; size_t ccount; - if (get_threats(gamestate, rank, file, color, candidates, &ccount)) { + if (get_threats(gamestate, file, rank, color, candidates, &ccount)) { bool result = false; for (size_t i = 0 ; i < ccount ; i++) { if (!is_pinned(gamestate, &candidates[i])) { @@ -950,7 +950,7 @@ size_t candidatecount; /* determine all candidate moves and sort out the invalid ones */ - if (get_candidates(gamestate, move->torank, move->tofile, color, + if (get_candidates(gamestate, move->tofile, move->torank, color, candidates, &candidatecount)) { bool found = false; @@ -1186,11 +1186,12 @@ return eval_move1(mstr, &move, color); } -bool is_protected(const GameState *gamestate, Rank rank, File file, Color color) { +bool is_protected(const GameState *gamestate, + File file, Rank rank, Color color) { Move candidates[16]; size_t ccount; /* we need all candidates - not only threats! */ - if (get_candidates(gamestate, rank, file, color, candidates, &ccount)) { + if (get_candidates(gamestate, file, rank, color, candidates, &ccount)) { for (size_t i = 0 ; i < ccount ; i++) { /* skip the king */ if (piece_type(candidates[i].piece) == KING) continue; @@ -1273,11 +1274,11 @@ } size_t filter_moves_allowed(const GameState *gamestate, - Color c, Rank r, File f, Move *moves, moves_generator_func func) { + Color c, File f, Rank r, Move *moves, moves_generator_func func) { /* worst case: the queen has the most moves */ Move candidates[QUEEN_MOVES_MAX]; - size_t candidatecount = func(gamestate, c, r, f, candidates); + size_t candidatecount = func(gamestate, c, f, r, candidates); size_t count = 0; for (size_t i = 0 ; i < candidatecount ; i++) {
--- a/src/chess/rules.h Tue Aug 25 18:59:18 2026 +0200 +++ b/src/chess/rules.h Tue Aug 25 19:42:15 2026 +0200 @@ -201,17 +201,17 @@ static inline void enpassant_threat_add(GameState *gamestate, - Rank rank, File file) { + File file, Rank rank) { gamestate->board[rank][file] |= ENPASSANT_THREAT; } static inline void enpassant_threat_remove(GameState *gamestate, - Rank rank, File file) { + File file, Rank rank) { gamestate->board[rank][file] &= ~ENPASSANT_THREAT; } static inline bool enpassant_threat_exists(const GameState *gamestate, - Rank rank, File file) { + File file, Rank rank) { return gamestate->board[rank][file] & ENPASSANT_THREAT; } @@ -229,7 +229,7 @@ return gamestate->moves[gamestate->movecount - 1].check; } -static inline Color field_color(Rank r, File f) { +static inline Color field_color(File f, Rank r) { return (r + f) % 2 == 0 ? BLACK : WHITE; } @@ -289,35 +289,35 @@ * Returns the piece at the specified position. * * @param gamestate the current game state + * @param file the file * @param rank the rank - * @param file the file * @return the piece at the specified position */ -Piece piece_at(const GameState *gamestate, Rank rank, File file); +Piece piece_at(const GameState *gamestate, File file, Rank rank); /** * Places a piece at the specified position in the current game state. * * @param gamestate the current game state + * @param file the file * @param rank the rank - * @param file the file * @param piece the piece to place at the specified position */ -void piece_set(GameState *gamestate, Rank rank, File file, Piece piece); +void piece_set(GameState *gamestate, File file, Rank rank, Piece piece); /** * Removes the piece at the specified position in the current game state. * * @param gamestate the current game state + * @param file the file * @param rank the rank - * @param file the file */ -static inline void piece_remove(GameState *gamestate, Rank rank, File file) { - piece_set(gamestate, rank, file, 0); +static inline void piece_remove(GameState *gamestate, File file, Rank rank) { + piece_set(gamestate, file, rank, 0); } typedef size_t(*moves_generator_func)(const GameState *gamestate, - Color c, Rank r, File f, Move *moves); + Color c, File f, Rank r, Move *moves); /** * Calculates all allowed moves for a specific piece. @@ -325,13 +325,13 @@ * Use the macros for the specific pieces instead. * * @param gamestate the current gamestate + * @param f the file 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, - Rank r, File f, Move *moves); + File f, Rank r, Move *moves); /** * Internal function used to filter out illegal moves. @@ -340,14 +340,14 @@ * * @param gamestate the current gamestate * @param c color of the piece + * @param f the file 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, Rank r, File f, Move *moves, moves_generator_func func); + Color c, File f, Rank r, Move *moves, moves_generator_func func); /** * Determines a list of theoretically possible moves to the specified field. @@ -359,8 +359,8 @@ * must be set, too. * * @param gamestate the current game state + * @param file file 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 * (must be large enough, 16 is always enough) @@ -368,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, Rank rank, File file, +bool get_candidates(const GameState *gamestate, File file, Rank rank, Color color, Move* moves, size_t* movecount); /** @@ -382,8 +382,8 @@ * must be set, too. * * @param gamestate the current game state + * @param file file 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 * (must be large enough, 16 is always enough) @@ -391,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, Rank rank, File file, +bool get_real_candidates(const GameState *gamestate, File file, Rank rank, Color color, Move* moves, size_t* movecount); /** @@ -404,8 +404,8 @@ * must be set, too. * * @param gamestate the current game state + * @param file file 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 * (must be large enough, 16 is always enough) @@ -413,7 +413,7 @@ * @return true, if any piece of the specified color threatens the specified * field */ -bool get_threats(const GameState *gamestate, Rank rank, File file, +bool get_threats(const GameState *gamestate, File file, Rank rank, Color color, Move* threats, size_t* threatcount); /** @@ -424,8 +424,8 @@ * must be set, too. * * @param gamestate the current game state + * @param file file 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 * (must be large enough, 16 is always enough) @@ -433,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, Rank rank, File file, +bool get_real_threats(const GameState *gamestate, File file, Rank rank, Color color, Move* threats, size_t* threatcount); /** @@ -443,30 +443,30 @@ * capture an opponent piece on this field, regardless of being pinned. * * @param gamestate the current game state + * @param file file 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, rank, file, color) \ - get_threats(gamestate, rank, file, color, NULL, NULL) +#define is_covered(gamestate, file, rank, color) \ + get_threats(gamestate, file, rank, color, NULL, NULL) /** * Checks, if a specified field is attacked by a piece of a certain color. - * + * * I.e. the field is threatened by a piece AND this piece is not pinned and * therefore able to perform the move. - * + * * @param gamestate the current game state + * @param file file 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, rank, file, color) \ - get_real_threats(gamestate, rank, file, color, NULL, NULL) +#define is_attacked(gamestate, file, rank, color) \ + get_real_threats(gamestate, file, rank, color, NULL, NULL) /** * Checks, if a specified field is protected by a piece of a certain color. @@ -475,13 +475,14 @@ * that field or move to that field (and is not pinned). * * @param gamestate the current game state + * @param file file 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, Rank rank, File file, Color color); +bool is_protected(const GameState *gamestate, + File file, Rank rank, Color color); /** * Checks, if the specified move cannot be performed, because the piece is
--- a/src/main.c Tue Aug 25 18:59:18 2026 +0200 +++ b/src/main.c Tue Aug 25 19:42:15 2026 +0200 @@ -286,7 +286,7 @@ for (Rank y = 0 ; y < 8 ; y++) { for (File x = 0 ; x < 8 ; x++) { - Piece piece = piece_at(gamestate, y, x); + Piece piece = piece_at(gamestate, x, y); Color col = piece_color(piece); char piecestr[5];
--- a/test/test-rules-helper.c Tue Aug 25 18:59:18 2026 +0200 +++ b/test/test-rules-helper.c Tue Aug 25 19:42:15 2026 +0200 @@ -44,7 +44,7 @@ CX_TEST_DO { for (Rank r = 0 ; r < 8 ; r++) { for (File f = 0 ; f < 8 ; f++) { - CX_TEST_ASSERT(field_color(r, f) == colors[r][f]); + CX_TEST_ASSERT(field_color(f, r) == colors[r][f]); } } }