src/chess/fen.c

Sun, 07 Jun 2026 16:56:22 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 07 Jun 2026 16:56:22 +0200
changeset 132
5762f2b5f87a
permissions
-rw-r--r--

extracts FEN code into a separate compilation unit

relates to #842

/*
 * 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 <stdlib.h>
#include <stdio.h>

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';
}

mercurial