src/chess/fen.c

changeset 132
5762f2b5f87a
equal deleted inserted replaced
131:c33567d61ba7 132:5762f2b5f87a
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2026 Mike Becker. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30 #include "fen.h"
31
32 #include <stdlib.h>
33 #include <stdio.h>
34
35 static size_t fen_pieces(char *str, GameState *gamestate) {
36 size_t i = 0;
37 for (int row = 7 ; row >= 0 ; row--) {
38 unsigned int skip = 0;
39 for (int file = 0 ; file < 8 ; file++) {
40 if (gamestate->board[row][file]) {
41 if (skip > 0) {
42 str[i++] = '0'+skip;
43 skip = 0;
44 }
45 switch (gamestate->board[row][file] & ~ENPASSANT_THREAT) {
46 case WHITE|KING: str[i++] = 'K'; break;
47 case WHITE|QUEEN: str[i++] = 'Q'; break;
48 case WHITE|BISHOP: str[i++] = 'B'; break;
49 case WHITE|KNIGHT: str[i++] = 'N'; break;
50 case WHITE|ROOK: str[i++] = 'R'; break;
51 case WHITE|PAWN: str[i++] = 'P'; break;
52 case BLACK|KING: str[i++] = 'k'; break;
53 case BLACK|QUEEN: str[i++] = 'q'; break;
54 case BLACK|BISHOP: str[i++] = 'b'; break;
55 case BLACK|KNIGHT: str[i++] = 'n'; break;
56 case BLACK|ROOK: str[i++] = 'r'; break;
57 case BLACK|PAWN: str[i++] = 'p'; break;
58 }
59 } else {
60 skip++;
61 }
62 }
63 if (skip > 0) {
64 str[i++] = '0'+skip;
65 }
66 if (row > 0) {
67 str[i++] = '/';
68 }
69 }
70
71 return i;
72 }
73
74 static size_t fen_color(char *str, GameState *gamestate) {
75 uint8_t color = opponent_color(gamestate->movecount > 0 ?
76 (last_move(gamestate).piece & COLOR_MASK) : BLACK);
77
78 str[0] = color == WHITE ? 'w' : 'b';
79
80 return 1;
81 }
82
83 static bool fen_castling_chkmoved(GameState *gamestate,
84 uint8_t row, uint8_t file) {
85
86 for (unsigned i = 0 ; i < gamestate->movecount ; i++) {
87 if (gamestate->moves[i].fromfile == file
88 && gamestate->moves[i].fromrow == row) {
89 return true;
90 }
91 }
92
93 return false;
94 }
95
96 static size_t fen_castling(char *str, GameState *gamestate) {
97 bool K, Q, k, q;
98
99 if (fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('e'))) {
100 K = Q = false;
101 } else {
102 K = !fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('h'));
103 Q = !fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('a'));
104 }
105 if (fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('e'))) {
106 k = q = false;
107 } else {
108 k = !fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('h'));
109 q = !fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('a'));
110 }
111
112 size_t i = 0;
113 if (K) str[i++] = 'K';
114 if (Q) str[i++] = 'Q';
115 if (k) str[i++] = 'k';
116 if (q) str[i++] = 'q';
117 if (!i) str[i++] = '-';
118
119 return i;
120 }
121
122 static size_t fen_enpassant(char *str, GameState *gamestate) {
123
124 str[0] = '-'; str[1] = '\0';
125
126 for (int file = 0 ; file < 8 ; file++) {
127 if (gamestate->board[3][file] & ENPASSANT_THREAT) {
128 str[0] = filechr(file);
129 str[1] = rowchr(2);
130 }
131 if (gamestate->board[4][file] & ENPASSANT_THREAT) {
132 str[0] = filechr(file);
133 str[1] = rowchr(5);
134 }
135 }
136
137 return str[0] == '-' ? 1 : 2;
138 }
139
140 static size_t fen_halfmove(char *str, GameState *gamestate) {
141 unsigned int hm = 0;
142 for (unsigned int i = 0; i < gamestate->movecount; i++) {
143 if (gamestate->moves[i].capture
144 || (gamestate->moves[i].piece & PIECE_MASK) == PAWN) {
145 hm = 0;
146 } else {
147 hm++;
148 }
149 }
150
151 return sprintf(str, "%u", hm);
152 }
153
154 static size_t fen_movenr(char *str, GameState *gamestate) {
155 return sprintf(str, "%u", gamestate->movecount);
156 }
157
158 void fen_compute(char *str, GameState *gamestate) {
159 str += fen_pieces(str, gamestate);
160 *str = ' '; str++;
161 str += fen_color(str, gamestate);
162 *str = ' '; str++;
163 str += fen_castling(str, gamestate);
164 *str = ' '; str++;
165 str += fen_enpassant(str, gamestate);
166 *str = ' '; str++;
167 str += fen_halfmove(str, gamestate);
168 *str = ' '; str++;
169 str += fen_movenr(str, gamestate);
170 str[0] = '\0';
171 }

mercurial