src/chess/pgn.c

Sun, 01 Oct 2023 13:56:42 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 01 Oct 2023 13:56:42 +0200
changeset 72
7e58e0f74e50
parent 70
5427beba96d1
permissions
-rw-r--r--

improve Makefiles

50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
55
54ea19938d57 updated copyright and version info
Mike Becker <universe@uap-core.de>
parents: 54
diff changeset
4 * Copyright 2016 Mike Becker. All rights reserved.
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
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
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 *
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 */
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 #include "pgn.h"
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 #include <ctype.h>
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 #include <stdlib.h>
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 #include <string.h>
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34
60
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
35 enum {
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
36 pgn_error_missing_quote = 1,
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
37 pgn_error_missing_bracket,
70
5427beba96d1 pgn parser can now handle comments (although it ignores them for now)
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
38 pgn_error_missing_brace,
60
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
39 pgn_error_missing_dot,
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
40 pgn_error_move_syntax,
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
41 pgn_error_move_semantics
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
42 };
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
43
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
44 static const char* pgn_error_strings[] = {
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
45 "No Error.",
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
46 "Tag values must be enclosed in double-quotes.",
70
5427beba96d1 pgn parser can now handle comments (although it ignores them for now)
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
47 "Missing closing brace '}' for comment.",
60
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
48 "Tags must be enclosed in square brackets: '[Key \"Value\"]'.",
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
49 "Move numbers must be terminated with a dot (e.g. '13.' - not '13').",
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
50 "Move is syntactically incorrect.",
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
51 "Move is not valid according to chess rules."
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
52 };
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
53
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
54 const char* pgn_error_str(int code) {
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
55 return pgn_error_strings[code];
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
56 }
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
57
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 int read_pgn(FILE* stream, GameState *gamestate, GameInfo *gameinfo) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 int c, i;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 char result[8];
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 char tagkey[32];
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 char tagvalue[128];
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65
64
4eda5df55f86 fixes castling not printed correctly to PGN
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
66 /* read tag pairs */
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 _Bool readmoves = 0;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 while (!readmoves) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 while (isspace(c = fgetc(stream)));
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 if (c == '1') {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 readmoves = 1;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 break;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 if (c != '[') {
60
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
75 return pgn_error_missing_bracket;
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 while (isspace(c = fgetc(stream)));
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 i = 0;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 do {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 tagkey[i++] = c;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 } while (!isspace(c = fgetc(stream)));
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 tagkey[i] = '\0';
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 while (isspace(c = fgetc(stream)));
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 if (c != '"') {
60
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
85 return pgn_error_missing_quote;
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 i = 0;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 while ((c = fgetc(stream)) != '"') {
60
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
89 if (c == '\n' || c == EOF) {
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
90 return pgn_error_missing_quote;
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 tagvalue[i++] = c;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 tagvalue[i] = '\0';
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 if (fgetc(stream) != ']') {
60
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
96 return pgn_error_missing_bracket;
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 if (strcmp("Result", tagkey) == 0) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 memcpy(result, tagvalue, 8);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103
64
4eda5df55f86 fixes castling not printed correctly to PGN
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
104 /* read moves */
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 if (fgetc(stream) != '.') {
60
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
106 return pgn_error_missing_dot;
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 char movestr[10];
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 Move move;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 uint8_t curcol = WHITE;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 while (readmoves) {
64
4eda5df55f86 fixes castling not printed correctly to PGN
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
114 /* move */
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 while (isspace(c = fgetc(stream)));
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 i = 0;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 do {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 movestr[i++] = c;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 if (i >= 10) {
59
3fa1de896666 fixes inappropriate use of EXIT_ macros + adds a sample PGN file
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
120 return 1;
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 } while (!isspace(c = fgetc(stream)));
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 movestr[i] = '\0';
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 if (eval_move(gamestate, movestr, &move, curcol)
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 != VALID_MOVE_SYNTAX) {
60
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
126 return pgn_error_move_syntax;
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 if (validate_move(gamestate, &move) != VALID_MOVE_SEMANTICS) {
60
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
129 return pgn_error_move_semantics;
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 apply_move(gamestate, &move);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132
70
5427beba96d1 pgn parser can now handle comments (although it ignores them for now)
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
133 /* skip spaces */
5427beba96d1 pgn parser can now handle comments (although it ignores them for now)
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
134 while (isspace(c = fgetc(stream)));
5427beba96d1 pgn parser can now handle comments (although it ignores them for now)
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
135
5427beba96d1 pgn parser can now handle comments (although it ignores them for now)
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
136 /* parse possible comment */
5427beba96d1 pgn parser can now handle comments (although it ignores them for now)
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
137 if (c == '{') {
5427beba96d1 pgn parser can now handle comments (although it ignores them for now)
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
138 // TODO: interpret comment (may contain clock info)
5427beba96d1 pgn parser can now handle comments (although it ignores them for now)
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
139 do {
5427beba96d1 pgn parser can now handle comments (although it ignores them for now)
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
140 c = fgetc(stream);
5427beba96d1 pgn parser can now handle comments (although it ignores them for now)
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
141 } while (c != '}' && c != EOF);
5427beba96d1 pgn parser can now handle comments (although it ignores them for now)
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
142 if (c == EOF) {
5427beba96d1 pgn parser can now handle comments (although it ignores them for now)
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
143 return pgn_error_missing_brace;
5427beba96d1 pgn parser can now handle comments (although it ignores them for now)
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
144 }
5427beba96d1 pgn parser can now handle comments (although it ignores them for now)
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
145 }
5427beba96d1 pgn parser can now handle comments (although it ignores them for now)
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
146
5427beba96d1 pgn parser can now handle comments (although it ignores them for now)
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
147 /* skip spaces */
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 while (isspace(c = fgetc(stream)));
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149
64
4eda5df55f86 fixes castling not printed correctly to PGN
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
150 /* end of game data encountered */
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 if (c == EOF) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 break;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154 if (c == '1' || c == '0') {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 c = fgetc(stream);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 if (c == '-') {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 gamestate->resign = !gamestate->checkmate;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 break;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159 } else if (c == '/') {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 gamestate->remis = !gamestate->stalemate;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 break;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 } else {
64
4eda5df55f86 fixes castling not printed correctly to PGN
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
163 /* oops, it was a move number, go back! */
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164 fseek(stream, -1, SEEK_CUR);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167
64
4eda5df55f86 fixes castling not printed correctly to PGN
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
168 /* we have eaten the next valuable byte, so go back */
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 fseek(stream, -1, SEEK_CUR);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170
64
4eda5df55f86 fixes castling not printed correctly to PGN
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
171 /* skip move number after black move */
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 if (curcol == BLACK) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 while (isdigit(c = fgetc(stream)));
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 if (c != '.') {
60
0c50aac49e55 adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents: 59
diff changeset
175 return pgn_error_missing_dot;
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178 curcol = opponent_color(curcol);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180
59
3fa1de896666 fixes inappropriate use of EXIT_ macros + adds a sample PGN file
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
181 return 0;
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 size_t write_pgn(FILE* stream, GameState *gamestate, GameInfo *gameinfo) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185 // TODO: tag pairs
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
186 size_t bytes = 0;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187
64
4eda5df55f86 fixes castling not printed correctly to PGN
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
188 /* Result */
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 char *result;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 if (gamestate->stalemate || gamestate->remis) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 result = "1/2-1/2";
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192 } else if (gamestate->checkmate || gamestate->resign) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 if (gamestate->lastmove) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 result = (gamestate->lastmove->move.piece & COLOR_MASK) == WHITE ?
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195 "1-0" : "0-1";
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 } else {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 result = "0-1";
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 } else {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200 result = "*";
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
201 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
202 fprintf(stream, "[Result \"%s\"]\n\n", result);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203
64
4eda5df55f86 fixes castling not printed correctly to PGN
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
204 /* moves */
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
205 int i = 1;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206 for (MoveList *movelist = gamestate->movelist ;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207 movelist ; movelist = movelist->next) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
209 if (++i % 2 == 0) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
210 fprintf(stream, "%d. %s", i/2, movelist->move.string);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
211 } else {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
212 fprintf(stream, " %s", movelist->move.string);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
213 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
214
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
215 // TODO: move time and maybe other comments
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
216
64
4eda5df55f86 fixes castling not printed correctly to PGN
Mike Becker <universe@uap-core.de>
parents: 60
diff changeset
217 /* line break every 10 moves */
65
dcc5bd2c56c0 PGN output now correctly breaks after black has also moved
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
218 if ((i-1) % 20) {
50
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
219 fputc(' ', stream);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
220 } else {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
221 fputc('\n', stream);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
222 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
223 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
224
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
225 if (result[0] == '*') {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
226 fputc('\n', stream);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
227 } else {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
228 fprintf(stream, "%s\n", result);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
229 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
230
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
231
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
232 return bytes;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
233 }
54
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
234
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
235 static size_t fen_pieces(char *str, GameState *gamestate) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
236 size_t i = 0;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
237 for (int row = 7 ; row >= 0 ; row--) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
238 unsigned int skip = 0;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
239 for (int file = 0 ; file < 8 ; file++) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
240 if (gamestate->board[row][file]) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
241 if (skip > 0) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
242 str[i++] = '0'+skip;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
243 skip = 0;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
244 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
245 switch (gamestate->board[row][file] & ~ENPASSANT_THREAT) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
246 case WHITE|KING: str[i++] = 'K'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
247 case WHITE|QUEEN: str[i++] = 'Q'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
248 case WHITE|BISHOP: str[i++] = 'B'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
249 case WHITE|KNIGHT: str[i++] = 'N'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
250 case WHITE|ROOK: str[i++] = 'R'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
251 case WHITE|PAWN: str[i++] = 'P'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
252 case BLACK|KING: str[i++] = 'k'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
253 case BLACK|QUEEN: str[i++] = 'q'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
254 case BLACK|BISHOP: str[i++] = 'b'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
255 case BLACK|KNIGHT: str[i++] = 'n'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
256 case BLACK|ROOK: str[i++] = 'r'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
257 case BLACK|PAWN: str[i++] = 'p'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
258 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
259 } else {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
260 skip++;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
261 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
262 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
263 if (skip > 0) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
264 str[i++] = '0'+skip;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
265 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
266 if (row > 0) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
267 str[i++] = '/';
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
268 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
269 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
270
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
271 return i;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
272 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
273
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
274 static size_t fen_color(char *str, GameState *gamestate) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
275 uint8_t color = opponent_color(gamestate->lastmove ?
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
276 (gamestate->lastmove->move.piece & COLOR_MASK) : BLACK);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
277
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
278 str[0] = color == WHITE ? 'w' : 'b';
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
279
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
280 return 1;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
281 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
282
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
283 static _Bool fen_castling_chkmoved(GameState *gamestate,
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
284 uint8_t row, uint8_t file) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
285
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
286 MoveList *ml = gamestate->movelist;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
287 while (ml) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
288 if (ml->move.fromfile == file && ml->move.fromrow == row) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
289 return 1;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
290 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
291 ml = ml->next;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
292 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
293
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
294 return 0;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
295 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
296
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
297 static size_t fen_castling(char *str, GameState *gamestate) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
298 _Bool K, Q, k, q;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
299
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
300 if (fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('e'))) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
301 K = Q = 0;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
302 } else {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
303 K = !fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('h'));
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
304 Q = !fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('a'));
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
305 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
306 if (fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('e'))) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
307 k = q = 0;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
308 } else {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
309 k = !fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('h'));
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
310 q = !fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('a'));
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
311 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
312
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
313 size_t i = 0;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
314 if (K) str[i++] = 'K';
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
315 if (Q) str[i++] = 'Q';
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
316 if (k) str[i++] = 'k';
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
317 if (q) str[i++] = 'q';
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
318 if (!i) str[i++] = '-';
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
319
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
320 return i;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
321 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
322
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
323 static size_t fen_enpassant(char *str, GameState *gamestate) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
324
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
325 str[0] = '-'; str[1] = '\0';
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
326
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
327 for (int file = 0 ; file < 8 ; file++) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
328 if (gamestate->board[3][file] & ENPASSANT_THREAT) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
329 str[0] = filechr(file);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
330 str[1] = rowchr(2);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
331 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
332 if (gamestate->board[4][file] & ENPASSANT_THREAT) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
333 str[0] = filechr(file);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
334 str[1] = rowchr(5);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
335 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
336 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
337
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
338 return str[0] == '-' ? 1 : 2;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
339 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
340
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
341 static size_t fen_halfmove(char *str, GameState *gamestate) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
342
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
343 unsigned int i = 0;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
344 for (MoveList *move = gamestate->movelist ; move ; move = move->next) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
345 if (move->move.capture || (move->move.piece & PIECE_MASK) == PAWN) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
346 i = 0;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
347 } else {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
348 i++;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
349 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
350 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
351
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
352 char m[8];
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
353 size_t len = sprintf(m, "%u", i);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
354 memcpy(str, m, len);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
355
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
356 return len;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
357 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
358
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
359 static size_t fen_movenr(char *str, GameState *gamestate) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
360
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
361 MoveList *move = gamestate->movelist;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
362 unsigned int i = 1;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
363 while (move) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
364 i++;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
365 move = move->next;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
366 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
367
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
368 char m[8];
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
369 size_t len = sprintf(m, "%u", i);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
370 memcpy(str, m, len);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
371
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
372 return len;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
373 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
374
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
375 void compute_fen(char *str, GameState *gamestate) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
376 str += fen_pieces(str, gamestate);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
377 *str = ' '; str++;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
378 str += fen_color(str, gamestate);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
379 *str = ' '; str++;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
380 str += fen_castling(str, gamestate);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
381 *str = ' '; str++;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
382 str += fen_enpassant(str, gamestate);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
383 *str = ' '; str++;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
384 str += fen_halfmove(str, gamestate);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
385 *str = ' '; str++;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
386 str += fen_movenr(str, gamestate);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
387 str[0] = '\0';
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
388 }

mercurial