forked from ulthiel/polyglot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
line.c
203 lines (129 loc) · 3.49 KB
/
line.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// line.c
// includes
#include <string.h>
#include "board.h"
#include "line.h"
#include "move.h"
#include "move_do.h"
#include "move_legal.h"
#include "san.h"
#include "util.h"
// constants
static const bool Strict = FALSE; // FALSE
static const bool UseDebug = FALSE; // FALSE
// functions
// line_is_ok()
bool line_is_ok(const move_t line[]) {
int move;
if (line == NULL) return FALSE;
while ((move = *line++) != MoveNone) {
if (!move_is_ok(move)) return FALSE;
}
return TRUE;
}
// line_clear()
void line_clear(move_t line[]) {
ASSERT(line!=NULL);
*line = MoveNone;
}
// line_copy()
void line_copy(move_t dst[], const move_t src[]) {
ASSERT(dst!=NULL);
ASSERT(src!=NULL);
ASSERT(dst!=src);
while ((*dst++ = *src++) != MoveNone)
;
}
// line_from_can()
bool line_from_can (move_t line[], const board_t * board, const char string[], int size) {
int pos;
char new_string[StringSize], *p;
int move;
board_t new_board[1];
ASSERT(line!=NULL);
ASSERT(board_is_ok(board));
ASSERT(string!=NULL);
ASSERT(size>=LineSize);
// init
pos = 0;
board_copy(new_board,board);
// loop
strcpy(new_string,string); // HACK
for (p = strtok(new_string," "); p != NULL; p = strtok(NULL," ")) {
move = move_from_can(p,new_board);
ASSERT(move!=MoveNone);
ASSERT(move_is_legal(move,new_board));
if (move == MoveNone || !move_is_legal(move,new_board)) break; // HACK: ignore illegal moves
if (pos >= size) return FALSE;
line[pos++] = move;
move_do(new_board,move);
}
if (pos >= size) return FALSE;
line[pos] = MoveNone;
return TRUE;
}
// line_to_can()
bool line_to_can(const move_t line[], const board_t * board, char string[], int size) {
board_t new_board[1];
int pos;
int move;
ASSERT(line_is_ok(line));
ASSERT(board_is_ok(board));
ASSERT(string!=NULL);
ASSERT(size>=StringSize);
// init
if (size < StringSize) return FALSE;
board_copy(new_board,board);
pos = 0;
// loop
while ((move = *line++) != MoveNone) {
if (pos != 0) {
if (pos >= size) return FALSE;
string[pos++] = ' ';
}
if (!move_to_can(move,new_board,&string[pos],size-pos)) return FALSE;
pos += strlen(&string[pos]);
move_do(new_board,move);
}
if (pos >= size) return FALSE;
string[pos] = '\0';
return TRUE;
}
// line_to_san()
bool line_to_san(const move_t line[], const board_t * board, char string[], int size) {
board_t new_board[1];
int pos;
int move;
char move_string[256];
ASSERT(line_is_ok(line));
ASSERT(board_is_ok(board));
ASSERT(string!=NULL);
ASSERT(size>=StringSize);
// init
if (size < StringSize) return FALSE;
board_copy(new_board,board);
pos = 0;
// loop
while ((move = *line++) != MoveNone) {
if (pos != 0) {
if (pos >= size) return FALSE;
string[pos++] = ' ';
}
if (!move_is_legal(move,new_board)
|| !move_to_san(move,new_board,&string[pos],size-pos)) {
if (Strict || UseDebug) {
move_to_can(move,new_board,move_string,256);
my_log("POLYGLOT ILLEGAL MOVE IN LINE %s\n",move_string);
board_disp(new_board);
}
if (Strict) my_fatal("line_to_san(): illegal move\n");
break;
}
pos += strlen(&string[pos]);
move_do(new_board,move);
}
if (pos >= size) return FALSE;
string[pos] = '\0';
return TRUE;
}
// end of line.cpp