Wed, 29 Aug 2018 13:55:18 +0200
fixes castling not printed correctly to PGN
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, |
0c50aac49e55
adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents:
59
diff
changeset
|
38 | pgn_error_missing_dot, |
0c50aac49e55
adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents:
59
diff
changeset
|
39 | pgn_error_move_syntax, |
0c50aac49e55
adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents:
59
diff
changeset
|
40 | pgn_error_move_semantics |
0c50aac49e55
adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents:
59
diff
changeset
|
41 | }; |
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 | static const char* pgn_error_strings[] = { |
0c50aac49e55
adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents:
59
diff
changeset
|
44 | "No Error.", |
0c50aac49e55
adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents:
59
diff
changeset
|
45 | "Tag values must be enclosed in double-quotes.", |
0c50aac49e55
adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents:
59
diff
changeset
|
46 | "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
|
47 | "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
|
48 | "Move is syntactically incorrect.", |
0c50aac49e55
adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents:
59
diff
changeset
|
49 | "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
|
50 | }; |
0c50aac49e55
adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents:
59
diff
changeset
|
51 | |
0c50aac49e55
adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents:
59
diff
changeset
|
52 | const char* pgn_error_str(int code) { |
0c50aac49e55
adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents:
59
diff
changeset
|
53 | return pgn_error_strings[code]; |
0c50aac49e55
adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents:
59
diff
changeset
|
54 | } |
0c50aac49e55
adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents:
59
diff
changeset
|
55 | |
50
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
56 | 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
|
57 | int c, i; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
58 | |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
59 | char result[8]; |
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 tagkey[32]; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
62 | char tagvalue[128]; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
63 | |
64
4eda5df55f86
fixes castling not printed correctly to PGN
Mike Becker <universe@uap-core.de>
parents:
60
diff
changeset
|
64 | /* 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
|
65 | _Bool readmoves = 0; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
66 | while (!readmoves) { |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
67 | 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
|
68 | if (c == '1') { |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
69 | readmoves = 1; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
70 | break; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
71 | } |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
72 | if (c != '[') { |
60
0c50aac49e55
adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents:
59
diff
changeset
|
73 | 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
|
74 | } |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
75 | 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
|
76 | i = 0; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
77 | do { |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
78 | tagkey[i++] = c; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
79 | } 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
|
80 | tagkey[i] = '\0'; |
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 | if (c != '"') { |
60
0c50aac49e55
adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents:
59
diff
changeset
|
83 | 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
|
84 | } |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
85 | i = 0; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
86 | while ((c = fgetc(stream)) != '"') { |
60
0c50aac49e55
adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents:
59
diff
changeset
|
87 | if (c == '\n' || c == EOF) { |
0c50aac49e55
adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents:
59
diff
changeset
|
88 | 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
|
89 | } |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
90 | tagvalue[i++] = c; |
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] = '\0'; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
93 | if (fgetc(stream) != ']') { |
60
0c50aac49e55
adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents:
59
diff
changeset
|
94 | 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
|
95 | } |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
96 | |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
97 | 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
|
98 | 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
|
99 | } |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
100 | } |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
101 | |
64
4eda5df55f86
fixes castling not printed correctly to PGN
Mike Becker <universe@uap-core.de>
parents:
60
diff
changeset
|
102 | /* read moves */ |
50
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
103 | if (fgetc(stream) != '.') { |
60
0c50aac49e55
adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents:
59
diff
changeset
|
104 | 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
|
105 | } |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
106 | |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
107 | char movestr[10]; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
108 | Move move; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
109 | 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
|
110 | |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
111 | while (readmoves) { |
64
4eda5df55f86
fixes castling not printed correctly to PGN
Mike Becker <universe@uap-core.de>
parents:
60
diff
changeset
|
112 | /* move */ |
50
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
113 | 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
|
114 | i = 0; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
115 | do { |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
116 | movestr[i++] = c; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
117 | 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
|
118 | return 1; |
50
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
119 | } |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
120 | } 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
|
121 | movestr[i] = '\0'; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
122 | 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
|
123 | != VALID_MOVE_SYNTAX) { |
60
0c50aac49e55
adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents:
59
diff
changeset
|
124 | 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
|
125 | } |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
126 | 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
|
127 | 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
|
128 | } |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
129 | 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
|
130 | |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
131 | // TODO: parse comments |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
132 | 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
|
133 | |
64
4eda5df55f86
fixes castling not printed correctly to PGN
Mike Becker <universe@uap-core.de>
parents:
60
diff
changeset
|
134 | /* 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
|
135 | if (c == EOF) { |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
136 | break; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
137 | } |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
138 | 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
|
139 | c = fgetc(stream); |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
140 | if (c == '-') { |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
141 | 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
|
142 | break; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
143 | } else if (c == '/') { |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
144 | 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
|
145 | break; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
146 | } else { |
64
4eda5df55f86
fixes castling not printed correctly to PGN
Mike Becker <universe@uap-core.de>
parents:
60
diff
changeset
|
147 | /* 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
|
148 | 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
|
149 | } |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
150 | } |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
151 | |
64
4eda5df55f86
fixes castling not printed correctly to PGN
Mike Becker <universe@uap-core.de>
parents:
60
diff
changeset
|
152 | /* 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
|
153 | 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
|
154 | |
64
4eda5df55f86
fixes castling not printed correctly to PGN
Mike Becker <universe@uap-core.de>
parents:
60
diff
changeset
|
155 | /* 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
|
156 | if (curcol == BLACK) { |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
157 | 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
|
158 | if (c != '.') { |
60
0c50aac49e55
adds error messages to PGN reader
Mike Becker <universe@uap-core.de>
parents:
59
diff
changeset
|
159 | 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
|
160 | } |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
161 | } |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
162 | 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
|
163 | } |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
164 | |
59
3fa1de896666
fixes inappropriate use of EXIT_ macros + adds a sample PGN file
Mike Becker <universe@uap-core.de>
parents:
55
diff
changeset
|
165 | return 0; |
50
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 | |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
168 | 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
|
169 | // TODO: tag pairs |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
170 | 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
|
171 | |
64
4eda5df55f86
fixes castling not printed correctly to PGN
Mike Becker <universe@uap-core.de>
parents:
60
diff
changeset
|
172 | /* Result */ |
50
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
173 | char *result; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
174 | 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
|
175 | 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
|
176 | } 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
|
177 | if (gamestate->lastmove) { |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
178 | 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
|
179 | "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
|
180 | } else { |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
181 | result = "0-1"; |
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 | } else { |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
184 | result = "*"; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
185 | } |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
186 | 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
|
187 | |
64
4eda5df55f86
fixes castling not printed correctly to PGN
Mike Becker <universe@uap-core.de>
parents:
60
diff
changeset
|
188 | /* moves */ |
50
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
189 | int i = 1; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
190 | 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
|
191 | 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
|
192 | |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
193 | 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
|
194 | 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
|
195 | } else { |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
196 | 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
|
197 | } |
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 | // 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
|
200 | |
64
4eda5df55f86
fixes castling not printed correctly to PGN
Mike Becker <universe@uap-core.de>
parents:
60
diff
changeset
|
201 | /* line break every 10 moves */ |
50
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
202 | if (i % 20) { |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
203 | fputc(' ', stream); |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
204 | } else { |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
205 | fputc('\n', stream); |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
206 | } |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
207 | } |
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 (result[0] == '*') { |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
210 | fputc('\n', stream); |
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\n", result); |
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 | |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
216 | return bytes; |
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
217 | } |
54 | 218 | |
219 | static size_t fen_pieces(char *str, GameState *gamestate) { | |
220 | size_t i = 0; | |
221 | for (int row = 7 ; row >= 0 ; row--) { | |
222 | unsigned int skip = 0; | |
223 | for (int file = 0 ; file < 8 ; file++) { | |
224 | if (gamestate->board[row][file]) { | |
225 | if (skip > 0) { | |
226 | str[i++] = '0'+skip; | |
227 | skip = 0; | |
228 | } | |
229 | switch (gamestate->board[row][file] & ~ENPASSANT_THREAT) { | |
230 | case WHITE|KING: str[i++] = 'K'; break; | |
231 | case WHITE|QUEEN: str[i++] = 'Q'; break; | |
232 | case WHITE|BISHOP: str[i++] = 'B'; break; | |
233 | case WHITE|KNIGHT: str[i++] = 'N'; break; | |
234 | case WHITE|ROOK: str[i++] = 'R'; break; | |
235 | case WHITE|PAWN: str[i++] = 'P'; break; | |
236 | case BLACK|KING: str[i++] = 'k'; break; | |
237 | case BLACK|QUEEN: str[i++] = 'q'; break; | |
238 | case BLACK|BISHOP: str[i++] = 'b'; break; | |
239 | case BLACK|KNIGHT: str[i++] = 'n'; break; | |
240 | case BLACK|ROOK: str[i++] = 'r'; break; | |
241 | case BLACK|PAWN: str[i++] = 'p'; break; | |
242 | } | |
243 | } else { | |
244 | skip++; | |
245 | } | |
246 | } | |
247 | if (skip > 0) { | |
248 | str[i++] = '0'+skip; | |
249 | } | |
250 | if (row > 0) { | |
251 | str[i++] = '/'; | |
252 | } | |
253 | } | |
254 | ||
255 | return i; | |
256 | } | |
257 | ||
258 | static size_t fen_color(char *str, GameState *gamestate) { | |
259 | uint8_t color = opponent_color(gamestate->lastmove ? | |
260 | (gamestate->lastmove->move.piece & COLOR_MASK) : BLACK); | |
261 | ||
262 | str[0] = color == WHITE ? 'w' : 'b'; | |
263 | ||
264 | return 1; | |
265 | } | |
266 | ||
267 | static _Bool fen_castling_chkmoved(GameState *gamestate, | |
268 | uint8_t row, uint8_t file) { | |
269 | ||
270 | MoveList *ml = gamestate->movelist; | |
271 | while (ml) { | |
272 | if (ml->move.fromfile == file && ml->move.fromrow == row) { | |
273 | return 1; | |
274 | } | |
275 | ml = ml->next; | |
276 | } | |
277 | ||
278 | return 0; | |
279 | } | |
280 | ||
281 | static size_t fen_castling(char *str, GameState *gamestate) { | |
282 | _Bool K, Q, k, q; | |
283 | ||
284 | if (fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('e'))) { | |
285 | K = Q = 0; | |
286 | } else { | |
287 | K = !fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('h')); | |
288 | Q = !fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('a')); | |
289 | } | |
290 | if (fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('e'))) { | |
291 | k = q = 0; | |
292 | } else { | |
293 | k = !fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('h')); | |
294 | q = !fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('a')); | |
295 | } | |
296 | ||
297 | size_t i = 0; | |
298 | if (K) str[i++] = 'K'; | |
299 | if (Q) str[i++] = 'Q'; | |
300 | if (k) str[i++] = 'k'; | |
301 | if (q) str[i++] = 'q'; | |
302 | if (!i) str[i++] = '-'; | |
303 | ||
304 | return i; | |
305 | } | |
306 | ||
307 | static size_t fen_enpassant(char *str, GameState *gamestate) { | |
308 | ||
309 | str[0] = '-'; str[1] = '\0'; | |
310 | ||
311 | for (int file = 0 ; file < 8 ; file++) { | |
312 | if (gamestate->board[3][file] & ENPASSANT_THREAT) { | |
313 | str[0] = filechr(file); | |
314 | str[1] = rowchr(2); | |
315 | } | |
316 | if (gamestate->board[4][file] & ENPASSANT_THREAT) { | |
317 | str[0] = filechr(file); | |
318 | str[1] = rowchr(5); | |
319 | } | |
320 | } | |
321 | ||
322 | return str[0] == '-' ? 1 : 2; | |
323 | } | |
324 | ||
325 | static size_t fen_halfmove(char *str, GameState *gamestate) { | |
326 | ||
327 | unsigned int i = 0; | |
328 | for (MoveList *move = gamestate->movelist ; move ; move = move->next) { | |
329 | if (move->move.capture || (move->move.piece & PIECE_MASK) == PAWN) { | |
330 | i = 0; | |
331 | } else { | |
332 | i++; | |
333 | } | |
334 | } | |
335 | ||
336 | char m[8]; | |
337 | size_t len = sprintf(m, "%u", i); | |
338 | memcpy(str, m, len); | |
339 | ||
340 | return len; | |
341 | } | |
342 | ||
343 | static size_t fen_movenr(char *str, GameState *gamestate) { | |
344 | ||
345 | MoveList *move = gamestate->movelist; | |
346 | unsigned int i = 1; | |
347 | while (move) { | |
348 | i++; | |
349 | move = move->next; | |
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 | void compute_fen(char *str, GameState *gamestate) { | |
360 | str += fen_pieces(str, gamestate); | |
361 | *str = ' '; str++; | |
362 | str += fen_color(str, gamestate); | |
363 | *str = ' '; str++; | |
364 | str += fen_castling(str, gamestate); | |
365 | *str = ' '; str++; | |
366 | str += fen_enpassant(str, gamestate); | |
367 | *str = ' '; str++; | |
368 | str += fen_halfmove(str, gamestate); | |
369 | *str = ' '; str++; | |
370 | str += fen_movenr(str, gamestate); | |
371 | str[0] = '\0'; | |
372 | } |