src/chess/pgn.c

Tue, 28 Aug 2018 14:03:09 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Aug 2018 14:03:09 +0200
changeset 58
7ba8a97a8b6e
parent 55
54ea19938d57
child 59
3fa1de896666
permissions
-rw-r--r--

simplifies 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
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 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
36 int c, i;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 char result[8];
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 char tagkey[32];
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 char tagvalue[128];
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 // read tag pairs
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 _Bool readmoves = 0;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 while (!readmoves) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 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
47 if (c == '1') {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 readmoves = 1;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 break;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 if (c != '[') {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 return EXIT_FAILURE;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 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
55 i = 0;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 do {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 tagkey[i++] = c;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 } 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
59 tagkey[i] = '\0';
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 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
61 if (c != '"') {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 return EXIT_FAILURE;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 i = 0;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 while ((c = fgetc(stream)) != '"') {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 if (c == '\n') {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 return EXIT_FAILURE;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 tagvalue[i++] = c;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 tagvalue[i] = '\0';
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 if (fgetc(stream) != ']') {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 return EXIT_FAILURE;
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
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 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
77 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
78 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 // read moves
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 if (fgetc(stream) != '.') {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 return EXIT_FAILURE;
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
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 char movestr[10];
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 Move move;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 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
89
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 while (readmoves) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 // move
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 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
93 i = 0;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 do {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 movestr[i++] = c;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 if (i >= 10) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 return EXIT_FAILURE;
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 } 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
100 movestr[i] = '\0';
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 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
102 != VALID_MOVE_SYNTAX) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 return EXIT_FAILURE;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 if (validate_move(gamestate, &move) != VALID_MOVE_SEMANTICS) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 return EXIT_FAILURE;
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 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
109
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 // TODO: parse comments
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 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
112
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 // end of game data encountered
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 if (c == EOF) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 break;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 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
118 c = fgetc(stream);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 if (c == '-') {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 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
121 break;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 } else if (c == '/') {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 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
124 break;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 } else {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 // oops, it was a move number, go back!
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 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
128 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 }
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 // we have eaten the next valuable byte, so go back
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 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
133
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 // skip move number after black move
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 if (curcol == BLACK) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 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
137 if (c != '.') {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 return EXIT_FAILURE;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 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
142 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144 return EXIT_SUCCESS;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 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
148 // TODO: tag pairs
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 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
150
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 // Result
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 char *result;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 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
154 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
155 } 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
156 if (gamestate->lastmove) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 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
158 "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
159 } else {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 result = "0-1";
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 } else {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163 result = "*";
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 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
166
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167 // moves
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168 int i = 1;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 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
170 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
171
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 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
173 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
174 } else {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 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
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 // 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
179
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 // line break every 10 moves
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181 if (i % 20) {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 fputc(' ', stream);
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 fputc('\n', stream);
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 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188 if (result[0] == '*') {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 fputc('\n', stream);
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 } else {
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 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
192 }
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195 return bytes;
41017d0a72c5 added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 }
54
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
197
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
198 static size_t fen_pieces(char *str, GameState *gamestate) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
199 size_t i = 0;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
200 for (int row = 7 ; row >= 0 ; row--) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
201 unsigned int skip = 0;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
202 for (int file = 0 ; file < 8 ; file++) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
203 if (gamestate->board[row][file]) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
204 if (skip > 0) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
205 str[i++] = '0'+skip;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
206 skip = 0;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
207 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
208 switch (gamestate->board[row][file] & ~ENPASSANT_THREAT) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
209 case WHITE|KING: str[i++] = 'K'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
210 case WHITE|QUEEN: str[i++] = 'Q'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
211 case WHITE|BISHOP: str[i++] = 'B'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
212 case WHITE|KNIGHT: str[i++] = 'N'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
213 case WHITE|ROOK: str[i++] = 'R'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
214 case WHITE|PAWN: str[i++] = 'P'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
215 case BLACK|KING: str[i++] = 'k'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
216 case BLACK|QUEEN: str[i++] = 'q'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
217 case BLACK|BISHOP: str[i++] = 'b'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
218 case BLACK|KNIGHT: str[i++] = 'n'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
219 case BLACK|ROOK: str[i++] = 'r'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
220 case BLACK|PAWN: str[i++] = 'p'; break;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
221 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
222 } else {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
223 skip++;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
224 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
225 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
226 if (skip > 0) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
227 str[i++] = '0'+skip;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
228 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
229 if (row > 0) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
230 str[i++] = '/';
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
231 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
232 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
233
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
234 return i;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
235 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
236
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
237 static size_t fen_color(char *str, GameState *gamestate) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
238 uint8_t color = opponent_color(gamestate->lastmove ?
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
239 (gamestate->lastmove->move.piece & COLOR_MASK) : BLACK);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
240
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
241 str[0] = color == WHITE ? 'w' : 'b';
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
242
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
243 return 1;
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
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
246 static _Bool fen_castling_chkmoved(GameState *gamestate,
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
247 uint8_t row, uint8_t file) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
248
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
249 MoveList *ml = gamestate->movelist;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
250 while (ml) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
251 if (ml->move.fromfile == file && ml->move.fromrow == row) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
252 return 1;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
253 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
254 ml = ml->next;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
255 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
256
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
257 return 0;
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
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
260 static size_t fen_castling(char *str, GameState *gamestate) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
261 _Bool K, Q, k, q;
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 (fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('e'))) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
264 K = Q = 0;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
265 } else {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
266 K = !fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('h'));
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
267 Q = !fen_castling_chkmoved(gamestate, rowidx('1'), fileidx('a'));
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 if (fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('e'))) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
270 k = q = 0;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
271 } else {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
272 k = !fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('h'));
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
273 q = !fen_castling_chkmoved(gamestate, rowidx('8'), fileidx('a'));
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
274 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
275
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
276 size_t i = 0;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
277 if (K) str[i++] = 'K';
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
278 if (Q) str[i++] = 'Q';
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
279 if (k) str[i++] = 'k';
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
280 if (q) str[i++] = 'q';
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
281 if (!i) str[i++] = '-';
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 return i;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
284 }
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 static size_t fen_enpassant(char *str, GameState *gamestate) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
287
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
288 str[0] = '-'; str[1] = '\0';
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
289
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
290 for (int file = 0 ; file < 8 ; file++) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
291 if (gamestate->board[3][file] & ENPASSANT_THREAT) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
292 str[0] = filechr(file);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
293 str[1] = rowchr(2);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
294 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
295 if (gamestate->board[4][file] & ENPASSANT_THREAT) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
296 str[0] = filechr(file);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
297 str[1] = rowchr(5);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
298 }
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
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
301 return str[0] == '-' ? 1 : 2;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
302 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
303
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
304 static size_t fen_halfmove(char *str, GameState *gamestate) {
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 unsigned int i = 0;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
307 for (MoveList *move = gamestate->movelist ; move ; move = move->next) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
308 if (move->move.capture || (move->move.piece & PIECE_MASK) == PAWN) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
309 i = 0;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
310 } else {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
311 i++;
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 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
314
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
315 char m[8];
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
316 size_t len = sprintf(m, "%u", i);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
317 memcpy(str, m, len);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
318
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
319 return len;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
320 }
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 static size_t fen_movenr(char *str, GameState *gamestate) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
323
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
324 MoveList *move = gamestate->movelist;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
325 unsigned int i = 1;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
326 while (move) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
327 i++;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
328 move = move->next;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
329 }
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
330
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
331 char m[8];
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
332 size_t len = sprintf(m, "%u", i);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
333 memcpy(str, m, len);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
334
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
335 return len;
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 void compute_fen(char *str, GameState *gamestate) {
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
339 str += fen_pieces(str, gamestate);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
340 *str = ' '; str++;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
341 str += fen_color(str, gamestate);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
342 *str = ' '; str++;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
343 str += fen_castling(str, gamestate);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
344 *str = ' '; str++;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
345 str += fen_enpassant(str, gamestate);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
346 *str = ' '; str++;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
347 str += fen_halfmove(str, gamestate);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
348 *str = ' '; str++;
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
349 str += fen_movenr(str, gamestate);
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
350 str[0] = '\0';
eef745ba3774 implemented FEN
Mike Becker <universe@uap-core.de>
parents: 50
diff changeset
351 }

mercurial