Sun, 01 Oct 2023 13:56:42 +0200
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 | 234 | |
235 | static size_t fen_pieces(char *str, GameState *gamestate) { | |
236 | size_t i = 0; | |
237 | for (int row = 7 ; row >= 0 ; row--) { | |
238 | unsigned int skip = 0; | |
239 | for (int file = 0 ; file < 8 ; file++) { | |
240 | if (gamestate->board[row][file]) { | |
241 | if (skip > 0) { | |
242 | str[i++] = '0'+skip; | |
243 | skip = 0; | |
244 | } | |
245 | switch (gamestate->board[row][file] & ~ENPASSANT_THREAT) { | |
246 | case WHITE|KING: str[i++] = 'K'; break; | |
247 | case WHITE|QUEEN: str[i++] = 'Q'; break; | |
248 | case WHITE|BISHOP: str[i++] = 'B'; break; | |
249 | case WHITE|KNIGHT: str[i++] = 'N'; break; | |
250 | case WHITE|ROOK: str[i++] = 'R'; break; | |
251 | case WHITE|PAWN: str[i++] = 'P'; break; | |
252 | case BLACK|KING: str[i++] = 'k'; break; | |
253 | case BLACK|QUEEN: str[i++] = 'q'; break; | |
254 | case BLACK|BISHOP: str[i++] = 'b'; break; | |
255 | case BLACK|KNIGHT: str[i++] = 'n'; break; | |
256 | case BLACK|ROOK: str[i++] = 'r'; break; | |
257 | case BLACK|PAWN: str[i++] = 'p'; break; | |
258 | } | |
259 | } else { | |
260 | skip++; | |
261 | } | |
262 | } | |
263 | if (skip > 0) { | |
264 | str[i++] = '0'+skip; | |
265 | } | |
266 | if (row > 0) { | |
267 | str[i++] = '/'; | |
268 | } | |
269 | } | |
270 | ||
271 | return i; | |
272 | } | |
273 | ||
274 | static size_t fen_color(char *str, GameState *gamestate) { | |
275 | uint8_t color = opponent_color(gamestate->lastmove ? | |
276 | (gamestate->lastmove->move.piece & COLOR_MASK) : BLACK); | |
277 | ||
278 | str[0] = color == WHITE ? 'w' : 'b'; | |
279 | ||
280 | return 1; | |
281 | } | |
282 | ||
283 | static _Bool fen_castling_chkmoved(GameState *gamestate, | |
284 | uint8_t row, uint8_t file) { | |
285 | ||
286 | MoveList *ml = gamestate->movelist; | |
287 | while (ml) { | |
288 | if (ml->move.fromfile == file && ml->move.fromrow == row) { | |
289 | return 1; | |
290 | } | |
291 | ml = ml->next; | |
292 | } | |
293 | ||
294 | return 0; | |
295 | } | |
296 | ||
297 | static size_t fen_castling(char *str, GameState *gamestate) { | |
298 | _Bool K, Q, k, q; | |
299 | ||
300 | if (fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('e'))) { | |
301 | K = Q = 0; | |
302 | } else { | |
303 | K = !fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('h')); | |
304 | Q = !fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('a')); | |
305 | } | |
306 | if (fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('e'))) { | |
307 | k = q = 0; | |
308 | } else { | |
309 | k = !fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('h')); | |
310 | q = !fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('a')); | |
311 | } | |
312 | ||
313 | size_t i = 0; | |
314 | if (K) str[i++] = 'K'; | |
315 | if (Q) str[i++] = 'Q'; | |
316 | if (k) str[i++] = 'k'; | |
317 | if (q) str[i++] = 'q'; | |
318 | if (!i) str[i++] = '-'; | |
319 | ||
320 | return i; | |
321 | } | |
322 | ||
323 | static size_t fen_enpassant(char *str, GameState *gamestate) { | |
324 | ||
325 | str[0] = '-'; str[1] = '\0'; | |
326 | ||
327 | for (int file = 0 ; file < 8 ; file++) { | |
328 | if (gamestate->board[3][file] & ENPASSANT_THREAT) { | |
329 | str[0] = filechr(file); | |
330 | str[1] = rowchr(2); | |
331 | } | |
332 | if (gamestate->board[4][file] & ENPASSANT_THREAT) { | |
333 | str[0] = filechr(file); | |
334 | str[1] = rowchr(5); | |
335 | } | |
336 | } | |
337 | ||
338 | return str[0] == '-' ? 1 : 2; | |
339 | } | |
340 | ||
341 | static size_t fen_halfmove(char *str, GameState *gamestate) { | |
342 | ||
343 | unsigned int i = 0; | |
344 | for (MoveList *move = gamestate->movelist ; move ; move = move->next) { | |
345 | if (move->move.capture || (move->move.piece & PIECE_MASK) == PAWN) { | |
346 | i = 0; | |
347 | } else { | |
348 | i++; | |
349 | } | |
350 | } | |
351 | ||
352 | char m[8]; | |
353 | size_t len = sprintf(m, "%u", i); | |
354 | memcpy(str, m, len); | |
355 | ||
356 | return len; | |
357 | } | |
358 | ||
359 | static size_t fen_movenr(char *str, GameState *gamestate) { | |
360 | ||
361 | MoveList *move = gamestate->movelist; | |
362 | unsigned int i = 1; | |
363 | while (move) { | |
364 | i++; | |
365 | move = move->next; | |
366 | } | |
367 | ||
368 | char m[8]; | |
369 | size_t len = sprintf(m, "%u", i); | |
370 | memcpy(str, m, len); | |
371 | ||
372 | return len; | |
373 | } | |
374 | ||
375 | void compute_fen(char *str, GameState *gamestate) { | |
376 | str += fen_pieces(str, gamestate); | |
377 | *str = ' '; str++; | |
378 | str += fen_color(str, gamestate); | |
379 | *str = ' '; str++; | |
380 | str += fen_castling(str, gamestate); | |
381 | *str = ' '; str++; | |
382 | str += fen_enpassant(str, gamestate); | |
383 | *str = ' '; str++; | |
384 | str += fen_halfmove(str, gamestate); | |
385 | *str = ' '; str++; | |
386 | str += fen_movenr(str, gamestate); | |
387 | str[0] = '\0'; | |
388 | } |