# HG changeset patch # User Mike Becker # Date 1780844182 -7200 # Node ID 5762f2b5f87a8a242f34b9a60e40e0d029447c83 # Parent c33567d61ba7849520f0a90a30ebea190a0058e0 extracts FEN code into a separate compilation unit relates to #842 diff -r c33567d61ba7 -r 5762f2b5f87a src/Makefile --- a/src/Makefile Sun Jun 07 14:55:20 2026 +0200 +++ b/src/Makefile Sun Jun 07 16:56:22 2026 +0200 @@ -50,7 +50,7 @@ $(CC) -o $@ $(CFLAGS) -c $< $(BUILDDIR)/main.o: main.c chess/rules.h chess/pgn.h chess/rules.h \ - input.h network.h colors.h + chess/fen.h input.h network.h colors.h @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< diff -r c33567d61ba7 -r 5762f2b5f87a src/chess/Makefile --- a/src/chess/Makefile Sun Jun 07 14:55:20 2026 +0200 +++ b/src/chess/Makefile Sun Jun 07 16:56:22 2026 +0200 @@ -28,7 +28,7 @@ include ../../config.mk -SRC = pawn.c rook.c knight.c bishop.c queen.c king.c rules.c pgn.c +SRC = pawn.c rook.c knight.c bishop.c queen.c king.c rules.c fen.c pgn.c OBJ = $(SRC:%.c=$(BUILDDIR)/%.o) all: $(BUILDDIR)/libchess$(LIB_EXT) FORCE @@ -42,6 +42,10 @@ @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< +$(BUILDDIR)/fen.o: fen.c fen.h rules.h + @echo "Compiling $<" + $(CC) -o $@ $(CFLAGS) -c $< + $(BUILDDIR)/king.o: king.c rules.h king.h @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< diff -r c33567d61ba7 -r 5762f2b5f87a src/chess/fen.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/chess/fen.c Sun Jun 07 16:56:22 2026 +0200 @@ -0,0 +1,171 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2026 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "fen.h" + +#include +#include + +static size_t fen_pieces(char *str, GameState *gamestate) { + size_t i = 0; + for (int row = 7 ; row >= 0 ; row--) { + unsigned int skip = 0; + for (int file = 0 ; file < 8 ; file++) { + if (gamestate->board[row][file]) { + if (skip > 0) { + str[i++] = '0'+skip; + skip = 0; + } + switch (gamestate->board[row][file] & ~ENPASSANT_THREAT) { + case WHITE|KING: str[i++] = 'K'; break; + case WHITE|QUEEN: str[i++] = 'Q'; break; + case WHITE|BISHOP: str[i++] = 'B'; break; + case WHITE|KNIGHT: str[i++] = 'N'; break; + case WHITE|ROOK: str[i++] = 'R'; break; + case WHITE|PAWN: str[i++] = 'P'; break; + case BLACK|KING: str[i++] = 'k'; break; + case BLACK|QUEEN: str[i++] = 'q'; break; + case BLACK|BISHOP: str[i++] = 'b'; break; + case BLACK|KNIGHT: str[i++] = 'n'; break; + case BLACK|ROOK: str[i++] = 'r'; break; + case BLACK|PAWN: str[i++] = 'p'; break; + } + } else { + skip++; + } + } + if (skip > 0) { + str[i++] = '0'+skip; + } + if (row > 0) { + str[i++] = '/'; + } + } + + return i; +} + +static size_t fen_color(char *str, GameState *gamestate) { + uint8_t color = opponent_color(gamestate->movecount > 0 ? + (last_move(gamestate).piece & COLOR_MASK) : BLACK); + + str[0] = color == WHITE ? 'w' : 'b'; + + return 1; +} + +static bool fen_castling_chkmoved(GameState *gamestate, + uint8_t row, uint8_t file) { + + for (unsigned i = 0 ; i < gamestate->movecount ; i++) { + if (gamestate->moves[i].fromfile == file + && gamestate->moves[i].fromrow == row) { + return true; + } + } + + return false; +} + +static size_t fen_castling(char *str, GameState *gamestate) { + bool K, Q, k, q; + + if (fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('e'))) { + K = Q = false; + } else { + K = !fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('h')); + Q = !fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('a')); + } + if (fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('e'))) { + k = q = false; + } else { + k = !fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('h')); + q = !fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('a')); + } + + size_t i = 0; + if (K) str[i++] = 'K'; + if (Q) str[i++] = 'Q'; + if (k) str[i++] = 'k'; + if (q) str[i++] = 'q'; + if (!i) str[i++] = '-'; + + return i; +} + +static size_t fen_enpassant(char *str, GameState *gamestate) { + + str[0] = '-'; str[1] = '\0'; + + for (int file = 0 ; file < 8 ; file++) { + if (gamestate->board[3][file] & ENPASSANT_THREAT) { + str[0] = filechr(file); + str[1] = rowchr(2); + } + if (gamestate->board[4][file] & ENPASSANT_THREAT) { + str[0] = filechr(file); + str[1] = rowchr(5); + } + } + + return str[0] == '-' ? 1 : 2; +} + +static size_t fen_halfmove(char *str, GameState *gamestate) { + unsigned int hm = 0; + for (unsigned int i = 0; i < gamestate->movecount; i++) { + if (gamestate->moves[i].capture + || (gamestate->moves[i].piece & PIECE_MASK) == PAWN) { + hm = 0; + } else { + hm++; + } + } + + return sprintf(str, "%u", hm); +} + +static size_t fen_movenr(char *str, GameState *gamestate) { + return sprintf(str, "%u", gamestate->movecount); +} + +void fen_compute(char *str, GameState *gamestate) { + str += fen_pieces(str, gamestate); + *str = ' '; str++; + str += fen_color(str, gamestate); + *str = ' '; str++; + str += fen_castling(str, gamestate); + *str = ' '; str++; + str += fen_enpassant(str, gamestate); + *str = ' '; str++; + str += fen_halfmove(str, gamestate); + *str = ' '; str++; + str += fen_movenr(str, gamestate); + str[0] = '\0'; +} diff -r c33567d61ba7 -r 5762f2b5f87a src/chess/fen.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/chess/fen.h Sun Jun 07 16:56:22 2026 +0200 @@ -0,0 +1,51 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2026 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef FEN_H +#define FEN_H + +#include "rules.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Computes the FEN representation of the current game state. + * + * @param str a buffer large enough to hold the FEN string + * @param gamestate the current game state + */ +void fen_compute(char *str, GameState *gamestate); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* FEN_H */ diff -r c33567d61ba7 -r 5762f2b5f87a src/chess/pgn.c --- a/src/chess/pgn.c Sun Jun 07 14:55:20 2026 +0200 +++ b/src/chess/pgn.c Sun Jun 07 16:56:22 2026 +0200 @@ -362,141 +362,3 @@ free(moveblk); } - -static size_t fen_pieces(char *str, GameState *gamestate) { - size_t i = 0; - for (int row = 7 ; row >= 0 ; row--) { - unsigned int skip = 0; - for (int file = 0 ; file < 8 ; file++) { - if (gamestate->board[row][file]) { - if (skip > 0) { - str[i++] = '0'+skip; - skip = 0; - } - switch (gamestate->board[row][file] & ~ENPASSANT_THREAT) { - case WHITE|KING: str[i++] = 'K'; break; - case WHITE|QUEEN: str[i++] = 'Q'; break; - case WHITE|BISHOP: str[i++] = 'B'; break; - case WHITE|KNIGHT: str[i++] = 'N'; break; - case WHITE|ROOK: str[i++] = 'R'; break; - case WHITE|PAWN: str[i++] = 'P'; break; - case BLACK|KING: str[i++] = 'k'; break; - case BLACK|QUEEN: str[i++] = 'q'; break; - case BLACK|BISHOP: str[i++] = 'b'; break; - case BLACK|KNIGHT: str[i++] = 'n'; break; - case BLACK|ROOK: str[i++] = 'r'; break; - case BLACK|PAWN: str[i++] = 'p'; break; - } - } else { - skip++; - } - } - if (skip > 0) { - str[i++] = '0'+skip; - } - if (row > 0) { - str[i++] = '/'; - } - } - - return i; -} - -static size_t fen_color(char *str, GameState *gamestate) { - uint8_t color = opponent_color(gamestate->movecount > 0 ? - (last_move(gamestate).piece & COLOR_MASK) : BLACK); - - str[0] = color == WHITE ? 'w' : 'b'; - - return 1; -} - -static bool fen_castling_chkmoved(GameState *gamestate, - uint8_t row, uint8_t file) { - - for (unsigned i = 0 ; i < gamestate->movecount ; i++) { - if (gamestate->moves[i].fromfile == file - && gamestate->moves[i].fromrow == row) { - return true; - } - } - - return false; -} - -static size_t fen_castling(char *str, GameState *gamestate) { - bool K, Q, k, q; - - if (fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('e'))) { - K = Q = false; - } else { - K = !fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('h')); - Q = !fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('a')); - } - if (fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('e'))) { - k = q = false; - } else { - k = !fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('h')); - q = !fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('a')); - } - - size_t i = 0; - if (K) str[i++] = 'K'; - if (Q) str[i++] = 'Q'; - if (k) str[i++] = 'k'; - if (q) str[i++] = 'q'; - if (!i) str[i++] = '-'; - - return i; -} - -static size_t fen_enpassant(char *str, GameState *gamestate) { - - str[0] = '-'; str[1] = '\0'; - - for (int file = 0 ; file < 8 ; file++) { - if (gamestate->board[3][file] & ENPASSANT_THREAT) { - str[0] = filechr(file); - str[1] = rowchr(2); - } - if (gamestate->board[4][file] & ENPASSANT_THREAT) { - str[0] = filechr(file); - str[1] = rowchr(5); - } - } - - return str[0] == '-' ? 1 : 2; -} - -static size_t fen_halfmove(char *str, GameState *gamestate) { - unsigned int hm = 0; - for (unsigned int i = 0; i < gamestate->movecount; i++) { - if (gamestate->moves[i].capture - || (gamestate->moves[i].piece & PIECE_MASK) == PAWN) { - hm = 0; - } else { - hm++; - } - } - - return sprintf(str, "%u", hm); -} - -static size_t fen_movenr(char *str, GameState *gamestate) { - return sprintf(str, "%u", gamestate->movecount); -} - -void compute_fen(char *str, GameState *gamestate) { - str += fen_pieces(str, gamestate); - *str = ' '; str++; - str += fen_color(str, gamestate); - *str = ' '; str++; - str += fen_castling(str, gamestate); - *str = ' '; str++; - str += fen_enpassant(str, gamestate); - *str = ' '; str++; - str += fen_halfmove(str, gamestate); - *str = ' '; str++; - str += fen_movenr(str, gamestate); - str[0] = '\0'; -} diff -r c33567d61ba7 -r 5762f2b5f87a src/main.c --- a/src/main.c Sun Jun 07 14:55:20 2026 +0200 +++ b/src/main.c Sun Jun 07 16:56:22 2026 +0200 @@ -31,6 +31,7 @@ #include "chess/rules.h" #include "chess/pgn.h" +#include "chess/fen.h" #include "input.h" #include "network.h" #include "colors.h" @@ -279,7 +280,7 @@ static void draw_board(GameState *gamestate, uint8_t perspective) { char fen[90]; - compute_fen(fen, gamestate); + fen_compute(fen, gamestate); mvaddstr(0, 0, fen); for (uint8_t y = 0 ; y < 8 ; y++) {