Fri, 04 Sep 2026 13:49:48 +0200
add function to reconstruct a board from a FEN string
resolves #939 in principle, but there are many TODOs left
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1 | /* |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
3 | * |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
4 | * Copyright 2026 Mike Becker. All rights reserved. |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
5 | * |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
8 | * |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
11 | * |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
15 | * |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
27 | * |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
28 | */ |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
29 | |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
30 | #include "fen.h" |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
31 | |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
32 | #include <stdlib.h> |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
33 | #include <stdio.h> |
|
218
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
34 | #include <string.h> |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
35 | |
|
218
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
36 | static size_t fen_pieces(char *str, const GameState *gamestate) { |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
37 | size_t i = 0; |
| 194 | 38 | Rank rank = 7; |
| 39 | do { | |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
40 | unsigned int skip = 0; |
| 194 | 41 | for (File file = 0 ; file < 8 ; file++) { |
| 42 | if (gamestate->board[rank][file]) { | |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
43 | if (skip > 0) { |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
44 | str[i++] = '0'+skip; |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
45 | skip = 0; |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
46 | } |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
47 | switch (piece_at(gamestate, file, rank)) { |
| 215 | 48 | case WKING: str[i++] = 'K'; break; |
| 49 | case WQUEEN: str[i++] = 'Q'; break; | |
| 50 | case WBISHOP: str[i++] = 'B'; break; | |
| 51 | case WKNIGHT: str[i++] = 'N'; break; | |
| 52 | case WROOK: str[i++] = 'R'; break; | |
| 53 | case WPAWN: str[i++] = 'P'; break; | |
| 54 | case BKING: str[i++] = 'k'; break; | |
| 55 | case BQUEEN: str[i++] = 'q'; break; | |
| 56 | case BBISHOP: str[i++] = 'b'; break; | |
| 57 | case BKNIGHT: str[i++] = 'n'; break; | |
| 58 | case BROOK: str[i++] = 'r'; break; | |
| 59 | case BPAWN: str[i++] = 'p'; break; | |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
60 | } |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
61 | } else { |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
62 | skip++; |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
63 | } |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
64 | } |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | if (skip > 0) { |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
66 | str[i++] = '0'+skip; |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
67 | } |
| 194 | 68 | if (rank > 0) { |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
69 | str[i++] = '/'; |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
70 | } |
| 194 | 71 | } while (rank-- > 0); |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
72 | |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
73 | return i; |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
74 | } |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
75 | |
|
218
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
76 | static size_t fen_color(char *str, const GameState *gamestate) { |
|
133
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
132
diff
changeset
|
77 | str[0] = current_color(gamestate) == WHITE ? 'w' : 'b'; |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
78 | return 1; |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
79 | } |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
80 | |
|
218
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
81 | static size_t fen_castling(char *str, const GameState *gamestate) { |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
82 | size_t i = 0; |
|
216
d0c3d3016650
make castling rights part of the game state
Mike Becker <universe@uap-core.de>
parents:
215
diff
changeset
|
83 | if (!gamestate->castling.K) str[i++] = 'K'; |
|
d0c3d3016650
make castling rights part of the game state
Mike Becker <universe@uap-core.de>
parents:
215
diff
changeset
|
84 | if (!gamestate->castling.Q) str[i++] = 'Q'; |
|
d0c3d3016650
make castling rights part of the game state
Mike Becker <universe@uap-core.de>
parents:
215
diff
changeset
|
85 | if (!gamestate->castling.k) str[i++] = 'k'; |
|
d0c3d3016650
make castling rights part of the game state
Mike Becker <universe@uap-core.de>
parents:
215
diff
changeset
|
86 | if (!gamestate->castling.q) str[i++] = 'q'; |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
87 | if (!i) str[i++] = '-'; |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
88 | |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
89 | return i; |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
90 | } |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
91 | |
|
218
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
92 | static size_t fen_enpassant(char *str, const GameState *gamestate) { |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
93 | |
|
133
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
132
diff
changeset
|
94 | str[0] = '-'; |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
95 | |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
96 | for (int file = 0 ; file < 8 ; file++) { |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
97 | if (enpassant_threat_exists(gamestate, file, 3)) { |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
98 | str[0] = filechr(file); |
| 194 | 99 | str[1] = rankchr(2); |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
100 | } |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
101 | if (enpassant_threat_exists(gamestate, file, 4)) { |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
102 | str[0] = filechr(file); |
| 194 | 103 | str[1] = rankchr(5); |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
104 | } |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
105 | } |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
106 | |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
107 | return str[0] == '-' ? 1 : 2; |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
108 | } |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
109 | |
|
218
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
110 | static size_t fen_halfmove(char *str, const GameState *gamestate) { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
111 | // TODO: respect a possible fifty_ctr_start |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
112 | unsigned int hm = 0; |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
113 | for (unsigned int i = 0; i < gamestate->movecount; i++) { |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
114 | if (gamestate->moves[i].capture |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
134
diff
changeset
|
115 | || piece_type(gamestate->moves[i].piece) == PAWN) { |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
116 | hm = 0; |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
117 | } else { |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
118 | hm++; |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
119 | } |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
120 | } |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
121 | |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
122 | return sprintf(str, "%u", hm); |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
123 | } |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
124 | |
|
218
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
125 | static size_t fen_movenr(char *str, const GameState *gamestate) { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
126 | unsigned mc = gamestate->movecount + gamestate->move_start; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
127 | return sprintf(str, "%u", 1 + mc / 2); |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
128 | } |
|
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
129 | |
|
133
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
132
diff
changeset
|
130 | static size_t fen_space(char *str) { |
|
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
132
diff
changeset
|
131 | *str = ' '; |
|
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
132
diff
changeset
|
132 | return 1; |
|
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
132
diff
changeset
|
133 | } |
|
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
132
diff
changeset
|
134 | |
|
218
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
135 | void fen_compute(char *str, const GameState *gamestate) { |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
136 | str += fen_pieces(str, gamestate); |
|
133
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
132
diff
changeset
|
137 | str += fen_space(str); |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
138 | str += fen_color(str, gamestate); |
|
133
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
132
diff
changeset
|
139 | str += fen_space(str); |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
140 | str += fen_castling(str, gamestate); |
|
133
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
132
diff
changeset
|
141 | str += fen_space(str); |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
142 | str += fen_enpassant(str, gamestate); |
|
133
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
132
diff
changeset
|
143 | str += fen_space(str); |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
144 | str += fen_halfmove(str, gamestate); |
|
133
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
132
diff
changeset
|
145 | str += fen_space(str); |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
146 | str += fen_movenr(str, gamestate); |
|
133
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
132
diff
changeset
|
147 | *str = '\0'; |
|
132
5762f2b5f87a
extracts FEN code into a separate compilation unit
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
148 | } |
|
218
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
149 | |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
150 | static unsigned fen_parse_number(const char *str, unsigned *target) { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
151 | unsigned l = 0; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
152 | *target = 0; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
153 | while (str[l] >= '0' && str[l] <= '9') { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
154 | unsigned n = str[l] - '0'; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
155 | *target *= 10; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
156 | *target += n; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
157 | l++; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
158 | } |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
159 | /* safety precaution - reject unreasonable high numbers */ |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
160 | if (l > 5) return 0; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
161 | return l; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
162 | } |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
163 | |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
164 | int fen_parse(const char *str, GameState *gamestate) { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
165 | const char * const fen_start = str; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
166 | // TODO: think about error reporting that is as good as for PGNs |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
167 | |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
168 | if (str == NULL) return 1; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
169 | |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
170 | /* zero-initialize the game state */ |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
171 | memset(gamestate, 0, sizeof(GameState)); |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
172 | |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
173 | /* parse the board (FEN starts top-left at "a8") */ |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
174 | Rank r = 7; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
175 | File f = 0; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
176 | while (true) { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
177 | switch (*str) { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
178 | case 'K': gamestate->board[r][f] = WKING; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
179 | case 'Q': gamestate->board[r][f] = WQUEEN; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
180 | case 'B': gamestate->board[r][f] = WBISHOP; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
181 | case 'N': gamestate->board[r][f] = WKNIGHT; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
182 | case 'R': gamestate->board[r][f] = WROOK; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
183 | case 'P': gamestate->board[r][f] = WPAWN; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
184 | case 'k': gamestate->board[r][f] = BKING; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
185 | case 'q': gamestate->board[r][f] = BQUEEN; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
186 | case 'b': gamestate->board[r][f] = BBISHOP; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
187 | case 'n': gamestate->board[r][f] = BKNIGHT; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
188 | case 'r': gamestate->board[r][f] = BROOK; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
189 | case 'p': gamestate->board[r][f] = BPAWN; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
190 | case '1': break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
191 | case '2': f += 1; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
192 | case '3': f += 2; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
193 | case '4': f += 3; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
194 | case '5': f += 4; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
195 | case '6': f += 5; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
196 | case '7': f += 6; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
197 | case '8': f += 7; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
198 | default: return 1; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
199 | } |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
200 | f++; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
201 | str++; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
202 | if (f == 8) { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
203 | /* rank complete - test for separator or ending space */ |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
204 | if (r > 0) { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
205 | if (*str != '/') return 1; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
206 | str++; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
207 | f = 0; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
208 | r--; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
209 | } else { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
210 | if (*str != ' ') return 1; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
211 | str++; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
212 | break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
213 | } |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
214 | } |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
215 | } |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
216 | |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
217 | /* whose turn is it? */ |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
218 | bool white_to_move; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
219 | if (str[0] == 'w') { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
220 | white_to_move = true; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
221 | } else if (str[0] == 'b') { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
222 | white_to_move = false; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
223 | } else { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
224 | return 1; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
225 | } |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
226 | if (str[1] != ' ') return 1; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
227 | str += 2; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
228 | |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
229 | /* castling rights */ |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
230 | gamestate->castling.K = gamestate->castling.Q = true; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
231 | gamestate->castling.k = gamestate->castling.q = true; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
232 | if (*str == '-') { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
233 | str++; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
234 | } else { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
235 | char cstl[5] = "KQkq"; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
236 | bool found = false; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
237 | for (unsigned i = 0 ; i < 4 ; i++) { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
238 | if (*str == cstl[i]) { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
239 | found = true; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
240 | switch (i) { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
241 | case 0: gamestate->castling.K = false; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
242 | case 1: gamestate->castling.Q = false; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
243 | case 2: gamestate->castling.k = false; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
244 | case 3: gamestate->castling.q = false; break; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
245 | } |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
246 | str++; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
247 | } |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
248 | } |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
249 | if (!found) return 1; /* no castling info found */ |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
250 | } |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
251 | if (*str != ' ') return 1; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
252 | str++; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
253 | |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
254 | /* is there an en-passant threat? */ |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
255 | if (*str == '-') { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
256 | str++; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
257 | } else { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
258 | if (isfile(str[0]) && isrank(str[1])) { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
259 | f = fileidx(str[0]); |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
260 | r = rankidx(str[1]); |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
261 | if (r == 2) { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
262 | r = 3; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
263 | } else if (r == 5) { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
264 | r = 4; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
265 | } else { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
266 | return 1; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
267 | } |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
268 | /* the threat is applied to the pawn, not the field it passed */ |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
269 | enpassant_threat_add(gamestate, f, r); |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
270 | } else { |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
271 | return 1; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
272 | } |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
273 | } |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
274 | if (*str != ' ') return 1; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
275 | str++; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
276 | |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
277 | /* fifty-moves counter */ |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
278 | unsigned mnr; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
279 | unsigned mlen; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
280 | mlen = fen_parse_number(str, &mnr); |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
281 | if (mlen == 0) return 1; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
282 | if (str[mlen] != ' ') return 1; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
283 | str += mlen+1; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
284 | gamestate->fifty_cntr_start = mnr; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
285 | |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
286 | /* move number */ |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
287 | mlen = fen_parse_number(str, &mnr); |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
288 | if (mlen == 0) return 1; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
289 | if (mnr == 0) return 1; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
290 | if (str[mlen] != '\0') return 1; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
291 | str += mlen+1; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
292 | gamestate->move_start = 2*mnr - 1; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
293 | if (white_to_move) gamestate->move_start--; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
294 | |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
295 | /* only copy the fen string if everything is a success */ |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
296 | gamestate->fen_start = strdup(fen_start); |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
297 | return 0; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
216
diff
changeset
|
298 | } |