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
203
|
/**
Copyright (c) Scott Gasch
Module Name:
testfen.c
Abstract:
Test the fen parser.
Author:
Scott Gasch ([email protected]) 11 May 2004
Revision History:
$Id: testfen.c 345 2007-12-02 22:56:42Z scott $
**/
#include "chess.h"
char *_szPieceAbbrevs = "pPnNbBrRqQkK";
#ifdef TEST
CHAR *
GenerateRandomLegalFenString(void)
/**
Routine description:
Parameters:
void
Return value:
CHAR
**/
{
static CHAR buf[MEDIUM_STRING_LEN_CHAR];
CHAR *pch = buf;
static ULONG uPieceLimits[14] =
// - - p P n N b B r R q Q k K
{0, 0, 8, 8, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1};
ULONG uPieceCounts[14] =
// - - p P n N b B r R q Q k K
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1};
ULONG uNumPiecesOnBoard = 2;
COOR c;
COOR cKingLoc[2];
PIECE p;
memset(buf, 0, sizeof(buf));
//
// Do the board. Note: this can result in a position where one or
// both kings are en-prise, pawns on the 1st/8th etc. The purpose
// is just to test PositionToFen and FenToPosition, not to make
// legal chess position.
//
cKingLoc[WHITE] = RANDOM_COOR;
do
{
cKingLoc[BLACK] = RANDOM_COOR;
}
while(cKingLoc[WHITE] == cKingLoc[BLACK]);
ASSERT(IS_ON_BOARD(cKingLoc[WHITE]));
ASSERT(IS_ON_BOARD(cKingLoc[BLACK]));
FOREACH_SQUARE(c)
{
if (!IS_ON_BOARD(c))
{
if (c % 8 == 0) *pch++ = '/';
continue;
}
else
{
if (c == cKingLoc[WHITE])
{
*pch = 'K';
}
else if (c == cKingLoc[BLACK])
{
*pch = 'k';
}
else
{
if ((rand() & 1) &&
(uNumPiecesOnBoard < 32))
{
do
{
p = RANDOM_PIECE;
ASSERT(IS_VALID_PIECE(p));
}
while(uPieceCounts[p] >= uPieceLimits[p]);
uPieceCounts[p]++;
uNumPiecesOnBoard++;
*pch = *(_szPieceAbbrevs + (p - 2));
}
else
{
*pch = '-';
}
}
}
pch++;
}
*pch++ = ' ';
//
// Do side on move
//
*pch = 'w';
if (rand() & 1)
{
*pch = 'b';
}
pch++;
*pch++ = ' ';
//
// Do castling permissions
//
*pch++ = '-';
*pch++ = ' ';
//
// Do the rest
//
strcat(buf, "- 0 1");
return(buf);
}
void
TestFenCode(void)
{
char *p;
char *q;
UINT u = 0;
POSITION pos1, pos2;
Trace("Testing FEN translation code...\n");
do
{
//
// Generate a random legal FEN string and parse it into pos1.
//
p = GenerateRandomLegalFenString();
if (FALSE == FenToPosition(&pos1, p))
{
UtilPanic(TESTCASE_FAILURE,
NULL, p, NULL, NULL,
__FILE__, __LINE__);
}
//
// Set q to be the FEN code of the position at pos1.
//
q = PositionToFen(&pos1);
if (NULL == q)
{
UtilPanic(TESTCASE_FAILURE,
&pos1, "q <- PositionToFen", NULL, NULL,
__FILE__, __LINE__);
}
//
// Set pos2 to be the position represented in q.
//
if (FALSE == FenToPosition(&pos2, q))
{
UtilPanic(TESTCASE_FAILURE,
NULL, q, NULL, NULL,
__FILE__, __LINE__);
}
//
// Pos1 and pos2 should be identical
//
if (FALSE == PositionsAreEquivalent(&pos1, &pos2))
{
UtilPanic(TESTCASE_FAILURE,
NULL,
"One initial FEN string has generated two "
"different positions",
&pos1,
&pos2,
__FILE__, __LINE__);
}
SystemFreeMemory(q);
u++;
}
while(u < 10000);
}
#endif
|