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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
|
/**
Copyright (c) Scott Gasch
Module Name:
gamelist.c
Abstract:
Maintain a list of game moves. This list is used to take back
official moves. It's also used to produce PGN at the end of a
game and to detect draws by repetition. This code also maintains
the official "current position" of the game in progress.
Author:
Scott Gasch ([email protected]) 14 May 2004
Revision History:
$Id: gamelist.c 355 2008-07-01 15:46:43Z scott $
**/
#include "chess.h"
GAME_DATA g_GameData;
ULONG g_uMoveNumber[2] = { 1, 1 };
ULONG g_uToMove;
ALIGN64 POSITION g_RootPosition;
#ifdef DEBUG
ULONG g_uAllocs = 0;
#endif
#define MOVE_POOL_SIZE (256)
GAME_MOVE g_MovePool[MOVE_POOL_SIZE];
static GAME_MOVE *
_GetMoveFromPool(void)
/**
Routine description:
Fast GAME_MOVE allocator that uses a preallocated pool.
Parameters:
void
Return value:
static GAME_MOVE *
**/
{
ULONG x = (rand() % (MOVE_POOL_SIZE - 15));
ULONG y;
for (y = x; y < x + 15; y++)
{
if (g_MovePool[y].fInUse == FALSE)
{
LockIncrement(&(g_MovePool[y].fInUse));
return(&(g_MovePool[y]));
}
}
#ifdef DEBUG
g_uAllocs++;
#endif
return(SystemAllocateMemory(sizeof(GAME_MOVE)));
}
static void
_ReturnMoveToPool(GAME_MOVE *p)
/**
Routine description:
Return a GAME_MOVE to the pool.
Parameters:
GAME_MOVE *p
Return value:
static void
**/
{
ULONG x = (ULONG)((BYTE *)p - (BYTE *)&g_MovePool);
x /= sizeof(GAME_MOVE);
if (x < MOVE_POOL_SIZE)
{
g_MovePool[x].fInUse = FALSE;
}
else
{
#ifdef DEBUG
g_uAllocs--;
#endif
SystemFreeMemory(p);
}
}
static void
_FreeIfNotNull(void *p)
/**
Routine description:
Free a pointer if it's not null.
Parameters:
void *p
Return value:
static void
**/
{
if (NULL != p)
{
SystemFreeMemory(p);
#ifdef DEBUG
g_uAllocs--;
#endif
}
}
static void
_FreeGameMove(GAME_MOVE *p)
/**
Routine description:
Free memory allocated to store a game move.
Parameters:
GAME_MOVE *p
Return value:
static void
**/
{
ASSERT(NULL != p);
_FreeIfNotNull(p->szComment);
_FreeIfNotNull(p->szDecoration);
_FreeIfNotNull(p->szMoveInSan);
_FreeIfNotNull(p->szMoveInIcs);
_FreeIfNotNull(p->szUndoPositionFen);
_ReturnMoveToPool(p);
}
static void
_FreeGameList(void)
/**
Routine description:
Free the whole game list.
Parameters:
void
Return value:
static void
**/
{
DLIST_ENTRY *p;
GAME_MOVE *q;
if (NULL != g_GameData.sMoveList.pFlink)
{
while(FALSE == IsListEmpty(&(g_GameData.sMoveList)))
{
p = RemoveHeadList(&(g_GameData.sMoveList));
q = CONTAINING_STRUCT(p, GAME_MOVE, links);
ASSERT(NULL != q);
_FreeGameMove(q);
}
}
_FreeIfNotNull(g_GameData.sHeader.szGameDescription);
_FreeIfNotNull(g_GameData.sHeader.szLocation);
_FreeIfNotNull(g_GameData.sHeader.sPlayer[WHITE].szName);
_FreeIfNotNull(g_GameData.sHeader.sPlayer[WHITE].szDescription);
_FreeIfNotNull(g_GameData.sHeader.sPlayer[BLACK].szName);
_FreeIfNotNull(g_GameData.sHeader.sPlayer[BLACK].szDescription);
_FreeIfNotNull(g_GameData.sHeader.szInitialFen);
memset(&g_GameData, 0, sizeof(g_GameData));
}
void
ResetGameList(void)
/**
Routine description:
Reset the game list (between games etc...)
Parameters:
void
Return value:
void
**/
{
_FreeGameList();
ASSERT(g_uAllocs == 0);
memset(g_MovePool, 0, sizeof(g_MovePool));
g_uMoveNumber[BLACK] = g_uMoveNumber[WHITE] = 1;
g_uToMove = WHITE;
InitializeListHead(&(g_GameData.sMoveList));
g_GameData.sHeader.result.eResult = RESULT_IN_PROGRESS;
}
FLAG
SetRootPosition(CHAR *szFen)
/**
Routine description:
Set the root position.
Parameters:
CHAR *szFen
Return value:
FLAG
**/
{
if (TRUE == FenToPosition(&g_RootPosition, szFen))
{
ResetGameList();
g_uToMove = g_RootPosition.uToMove;
TellGamelistThatIPlayColor(FLIP(g_RootPosition.uToMove));
if (0 != strcmp(szFen, STARTING_POSITION_IN_FEN))
{
g_GameData.sHeader.szInitialFen = STRDUP(szFen);
#ifdef DEBUG
if (g_GameData.sHeader.szInitialFen)
{
g_uAllocs++;
}
#endif
}
if (g_Options.fShouldPost)
{
DumpPosition(&g_RootPosition);
}
return(TRUE);
}
return(FALSE);
}
POSITION *
GetRootPosition(void)
/**
Routine description:
Get the root position.
Parameters:
void
Return value:
POSITION
**/
{
#ifdef DEBUG
static POSITION p;
memcpy(&p, &g_RootPosition, sizeof(POSITION));
return(&p);
#else
return(&g_RootPosition);
#endif
}
void
SetGameResultAndDescription(GAME_RESULT result)
/**
Routine description:
Set the game result and a description (both for PGN).
Parameters:
GAME_RESULT result
Return value:
void
**/
{
g_GameData.sHeader.result = result;
}
GAME_RESULT GetGameResult(void)
/**
Routine description:
Get the game result.
Parameters:
void
Return value:
INT
**/
{
return g_GameData.sHeader.result;
}
GAME_MOVE *
GetNthOfficialMoveRecord(ULONG n)
/**
Routine description:
Get the official move record for move N.
Parameters:
ULONG n
Return value:
GAME_MOVE
**/
{
DLIST_ENTRY *p = g_GameData.sMoveList.pFlink;
GAME_MOVE *q;
while((p != &(g_GameData.sMoveList)) &&
(n != 0))
{
p = p->pFlink;
n--;
}
if (n == 0)
{
q = CONTAINING_STRUCT(p, GAME_MOVE, links);
return(q);
}
return(NULL);
}
FLAG
DoesSigAppearInOfficialGameList(UINT64 u64Sig)
/**
Routine description:
Does sig X appear in the official move list?
Parameters:
UINT64 u64Sig
Return value:
FLAG
**/
{
DLIST_ENTRY *p = g_GameData.sMoveList.pBlink;
GAME_MOVE *q;
while(p != &(g_GameData.sMoveList))
{
q = CONTAINING_STRUCT(p, GAME_MOVE, links);
if (q->u64PositionSigAfterMove == u64Sig)
{
return(TRUE);
}
if ((q->mv.pCaptured) || (IS_PAWN(q->mv.pMoved)))
{
break;
}
p = p->pBlink;
}
return(FALSE);
}
ULONG CountOccurrancesOfSigInOfficialGameList(UINT64 u64Sig)
/**
Routine description:
How many times does sig X appear in the official move list?
Parameters:
UINT64 u64Sig
Return value:
ULONG
**/
{
DLIST_ENTRY *p = g_GameData.sMoveList.pBlink;
GAME_MOVE *q;
ULONG uCount = 0;
while(p != &(g_GameData.sMoveList))
{
q = CONTAINING_STRUCT(p, GAME_MOVE, links);
if (q->u64PositionSigAfterMove == u64Sig)
{
uCount++;
}
if ((q->mv.pCaptured) || (IS_PAWN(q->mv.pMoved)))
{
break;
}
p = p->pBlink;
}
return(uCount);
}
FLAG
IsLegalDrawByRepetition(void)
/**
Routine description:
Have we just drawn the game by repetition?
Parameters:
void
Return value:
FLAG
**/
{
POSITION *pos = GetRootPosition();
UINT64 u64Sig = (pos->u64PawnSig ^ pos->u64NonPawnSig);
if (3 == CountOccurrancesOfSigInOfficialGameList(u64Sig))
{
return(TRUE);
}
return(FALSE);
}
void
DumpGameList(void)
/**
Routine description:
Dump the move list to stdout.
Parameters:
void
Return value:
void
**/
{
GAME_MOVE *q;
ULONG u = 0;
Trace("\twhite\tblack\n"
"\t-----\t-----\n");
q = GetNthOfficialMoveRecord(u);
while((q != NULL) && (q->mv.uMove != 0))
{
if (GET_COLOR(q->mv.pMoved) == WHITE)
{
Trace("%2u.\t%s\t", q->uNumber, q->szMoveInSan);
}
else
{
Trace("%s\n", q->szMoveInSan);
}
u++;
q = GetNthOfficialMoveRecord(u);
}
Trace("\n");
}
void
DumpPgn(void)
/**
Routine description:
Dump the PGN of the current game to stdout.
Parameters:
void
Return value:
void
**/
{
DLIST_ENTRY *p = g_GameData.sMoveList.pFlink;
GAME_MOVE *q;
ULONG u = 0;
Trace("[Event \"Computer Chess Game\"]\n");
if (g_GameData.sHeader.szLocation != NULL)
{
Trace("[Site \"%s\"]\n", g_GameData.sHeader.szLocation);
}
Trace("[Date \"%s\"]\n", SystemGetDateString());
Trace("[Round 0]\n");
Trace("[White \"%s\"]\n", g_GameData.sHeader.sPlayer[WHITE].szName);
Trace("[Black \"%s\"]\n", g_GameData.sHeader.sPlayer[BLACK].szName);
switch(g_GameData.sHeader.result.eResult)
{
case RESULT_BLACK_WON:
Trace("[Result \"0-1\"]\n");
break;
case RESULT_WHITE_WON:
Trace("[Result \"1-0\"]\n");
break;
case RESULT_DRAW:
Trace("[Result \"1/2-1/2\"]\n");
break;
default:
Trace("[Result \"*\"]\n");
break;
}
if (g_GameData.sHeader.sPlayer[WHITE].uRating != 0)
{
Trace("[WhiteElo \"%u\"]\n", g_GameData.sHeader.sPlayer[WHITE].uRating);
}
if (g_GameData.sHeader.sPlayer[BLACK].uRating != 0)
{
Trace("[BlackElo \"%u\"]\n", g_GameData.sHeader.sPlayer[BLACK].uRating);
}
if (NULL != g_GameData.sHeader.szInitialFen)
{
Trace("[InitialFEN \"%s\"]\n", g_GameData.sHeader.szInitialFen);
}
// [ECO: XX]
Trace("[Time \"%s\"]\n", SystemGetTimeString());
/*
9.6.1: Tag: TimeControl
This uses a list of one or more time control fields. Each field contains a
descriptor for each time control period; if more than one descriptor is present
then they are separated by the colon character (":"). The descriptors appear
in the order in which they are used in the game. The last field appearing is
considered to be implicitly repeated for further control periods as needed.
There are six kinds of TimeControl fields.
The first kind is a single question mark ("?") which means that the time
control mode is unknown. When used, it is usually the only descriptor present.
The second kind is a single hyphen ("-") which means that there was no time
control mode in use. When used, it is usually the only descriptor present.
The third Time control field kind is formed as two positive integers separated
by a solidus ("/") character. The first integer is the number of moves in the
period and the second is the number of seconds in the period. Thus, a time
control period of 40 moves in 2 1/2 hours would be represented as "40/9000".
The fourth TimeControl field kind is used for a "sudden death" control period.
It should only be used for the last descriptor in a TimeControl tag value. It
is sometimes the only descriptor present. The format consists of a single
integer that gives the number of seconds in the period. Thus, a blitz game
would be represented with a TimeControl tag value of "300".
The fifth TimeControl field kind is used for an "incremental" control period.
It should only be used for the last descriptor in a TimeControl tag value and
is usually the only descriptor in the value. The format consists of two
positive integers separated by a plus sign ("+") character. The first integer
gives the minimum number of seconds allocated for the period and the second
integer gives the number of extra seconds added after each move is made. So,
an incremental time control of 90 minutes plus one extra minute per move would
be given by "4500+60" in the TimeControl tag value.
The sixth TimeControl field kind is used for a "sandclock" or "hourglass"
control period. It should only be used for the last descriptor in a
TimeControl tag value and is usually the only descriptor in the value. The
format consists of an asterisk ("*") immediately followed by a positive
integer. The integer gives the total number of seconds in the sandclock
period. The time control is implemented as if a sandclock were set at the
start of the period with an equal amount of sand in each of the two chambers
and the players invert the sandclock after each move with a time forfeit
indicated by an empty upper chamber. Electronic implementation of a physical
sandclock may be used. An example sandclock specification for a common three
minute egg timer sandclock would have a tag value of "*180".
Additional TimeControl field kinds will be defined as necessary.
*/
Trace("\n");
while(p != &(g_GameData.sMoveList))
{
q = CONTAINING_STRUCT(p, GAME_MOVE, links);
if (GET_COLOR(q->mv.pMoved) == WHITE)
{
Trace("%2u. %s ", q->uNumber, q->szMoveInSan);
}
else
{
Trace("%s ", q->szMoveInSan);
}
u++;
if (u > 10)
{
Trace("\n");
u = 0;
}
p = p->pFlink;
}
switch(g_GameData.sHeader.result.eResult)
{
case RESULT_BLACK_WON:
Trace("0-1\n");
break;
case RESULT_WHITE_WON:
Trace("1-0\n");
break;
case RESULT_DRAW:
Trace("1/2-1/2\n");
break;
default:
Trace("*\n");
break;
}
Trace("\n");
if (strlen(g_GameData.sHeader.result.szDescription)) {
Trace("%s\n", g_GameData.sHeader.result.szDescription);
}
}
FLAG
OfficiallyTakebackMove(void)
/**
Routine description:
Take back a move (i.e. unmake it and delete it from the move
list).
Parameters:
void
Return value:
FLAG
**/
{
DLIST_ENTRY *p;
GAME_MOVE *q;
POSITION *pos = &g_RootPosition;
if (FALSE == IsListEmpty(&(g_GameData.sMoveList)))
{
p = RemoveTailList(&(g_GameData.sMoveList));
ASSERT(p);
q = CONTAINING_STRUCT(p, GAME_MOVE, links);
if (FALSE == FenToPosition(pos, q->szUndoPositionFen))
{
UtilPanic(INCONSISTENT_STATE,
pos,
q->szUndoPositionFen,
NULL,
NULL,
__FILE__, __LINE__);
}
_FreeGameMove(q);
VerifyPositionConsistency(pos, FALSE);
ASSERT(g_uMoveNumber[g_uToMove] > 0);
g_uMoveNumber[g_uToMove] -= 1;
g_uToMove = FLIP(g_uToMove);
return(TRUE);
}
return(FALSE);
}
FLAG
OfficiallyMakeMove(MOVE mv,
SCORE iMoveScore,
FLAG fFast)
/**
Routine description:
Officially make a move (i.e. add it to the game list)
Parameters:
MOVE mv,
SCORE iMoveScore
Return value:
FLAG
**/
{
static SEARCHER_THREAD_CONTEXT ctx;
FLAG fRet = FALSE;
GAME_MOVE *q;
POSITION *pos = &g_RootPosition;
UINT64 u64SigBefore = pos->u64PawnSig ^ pos->u64NonPawnSig;
ReInitializeSearcherContext(pos, &ctx);
ASSERT(mv.uMove);
ASSERT(GET_COLOR(mv.pMoved) == g_uToMove);
if (FALSE == MakeUserMove(&ctx, mv))
{
goto end;
}
q = _GetMoveFromPool();
if (NULL == q)
{
goto end;
}
q->uNumber = g_uMoveNumber[g_uToMove];
ASSERT(q->uNumber > 0);
g_uMoveNumber[g_uToMove] += 1;
ASSERT(g_uMoveNumber[g_uToMove] != 0);
q->mv.uMove = mv.uMove;
q->szComment = NULL;
q->szDecoration = NULL;
q->iMoveScore = iMoveScore;
q->szMoveInSan = STRDUP(MoveToSan(mv, pos));
#ifdef DEBUG
if (q->szMoveInSan)
{
g_uAllocs++;
}
#endif
q->szMoveInIcs = STRDUP(MoveToIcs(mv));
#ifdef DEBUG
if (q->szMoveInIcs)
{
g_uAllocs++;
}
#endif
q->szUndoPositionFen = PositionToFen(pos);
#ifdef DEBUG
g_uAllocs++;
#endif
InsertTailList(&(g_GameData.sMoveList), &(q->links));
q->u64PositionSigAfterMove = (ctx.sPosition.u64NonPawnSig ^
ctx.sPosition.u64PawnSig);
q->u64PositionSigBeforeMove = u64SigBefore;
g_uToMove = FLIP(g_uToMove);
//
// Update the root position
//
memcpy(pos, &(ctx.sPosition), sizeof(POSITION));
VerifyPositionConsistency(&(ctx.sPosition), FALSE);
fRet = TRUE;
end:
return(fRet);
}
ULONG
GetMyColor(void)
/**
Routine description:
What color am I playing?
Parameters:
void
Return value:
ULONG
**/
{
switch(g_Options.ePlayMode)
{
case I_PLAY_WHITE:
return(WHITE);
case I_PLAY_BLACK:
default:
return(BLACK);
}
}
ULONG
GetOpponentsColor(void)
/**
Routine description:
What color is the opponent playing?
Parameters:
void
Return value:
ULONG
**/
{
return(FLIP(GetMyColor()));
}
void
SetOpponentsName(CHAR *sz)
/**
Routine description:
Set my opponent's name for the PGN record. Hello, my name is
Inigo Montoya. You killed my father. Prepare to die.
Parameters:
CHAR *sz
Return value:
void
**/
{
_FreeIfNotNull(g_GameData.sHeader.sPlayer[GetOpponentsColor()].szName);
g_GameData.sHeader.sPlayer[GetOpponentsColor()].szName = STRDUP(sz);
#ifdef DEBUG
if (g_GameData.sHeader.sPlayer[GetOpponentsColor()].szName)
{
g_uAllocs++;
}
#endif
}
void SetMyName(void)
/**
Routine description:
Set my name for the PGN record. Some people call me... Tim?
Parameters:
void
Return value:
void
**/
{
ULONG u = GetMyColor();
_FreeIfNotNull(g_GameData.sHeader.sPlayer[u].szName);
g_GameData.sHeader.sPlayer[u].szName = STRDUP("typhoon");
#ifdef DEBUG
if (g_GameData.sHeader.sPlayer[u].szName)
{
g_uAllocs++;
}
#endif
_FreeIfNotNull(g_GameData.sHeader.sPlayer[u].szDescription);
g_GameData.sHeader.sPlayer[u].szDescription =
STRDUP("Ver: " VERSION " Build Time: " __TIME__ " " __DATE__);
#ifdef DEBUG
if (g_GameData.sHeader.sPlayer[u].szDescription)
{
g_uAllocs++;
}
#endif
g_GameData.sHeader.sPlayer[u].fIsComputer = TRUE;
}
void
SetMyRating(ULONG u)
/**
Routine description:
Set my rating for the PGN record.
Parameters:
ULONG u
Return value:
void
**/
{
g_GameData.sHeader.sPlayer[GetMyColor()].uRating = u;
}
void
SetOpponentsRating(ULONG u)
/**
Routine description:
Set the opponent's rating for the PGN record.
Parameters:
ULONG u
Return value:
void
**/
{
g_GameData.sHeader.sPlayer[GetOpponentsColor()].uRating = u;
}
void
TellGamelistThatIPlayColor(ULONG u)
/**
Routine description:
The gamelist needs to know what color the computer is playing.
This routine sets it.
Parameters:
ULONG u
Return value:
void
**/
{
ULONG uOldColor = GetMyColor();
GAME_PLAYER x;
ASSERT(IS_VALID_COLOR(u));
ASSERT(IS_VALID_COLOR(uOldColor));
if (u != uOldColor)
{
x = g_GameData.sHeader.sPlayer[uOldColor];
g_GameData.sHeader.sPlayer[uOldColor] =
g_GameData.sHeader.sPlayer[u];
g_GameData.sHeader.sPlayer[u] = x;
}
}
ULONG
GetMoveNumber(ULONG uColor)
/**
Routine description:
What move number is it?
Parameters:
void
Return value:
ULONG
**/
{
return(g_uMoveNumber[uColor]);
}
void
MakeStatusLine(void)
/**
Routine description:
Parameters:
void
Return value:
void
**/
{
char buf[SMALL_STRING_LEN_CHAR];
ASSERT(IS_VALID_COLOR(g_uToMove));
if (g_Options.fStatusLine)
{
strcpy(buf, "_\0");
if (!g_Options.fRunningUnderXboard)
{
if ((TRUE == g_Options.fPondering) &&
(g_Options.mvPonder.uMove != 0))
{
snprintf(buf, SMALL_STRING_LEN_CHAR - 1,
"_[pondering %s] ",
MoveToSan(g_Options.mvPonder, &g_RootPosition));
}
snprintf(buf, SMALL_STRING_LEN_CHAR - strlen(buf),
"%s%s(move %u)_",
buf,
(g_uToMove == WHITE) ? "white" : "black",
g_uMoveNumber[g_uToMove]);
Trace("%s\n", buf);
}
}
}
static void
_ParsePgnHeaderLine(CHAR *p, CHAR **ppVar, CHAR **ppVal)
/**
Routine description:
Parameters:
CHAR *p,
CHAR **ppVar,
CHAR **ppVal
Return value:
static void
**/
{
*ppVar = NULL;
*ppVal = NULL;
if (*p != '[') return;
p++;
*ppVar = p;
while(*p && !isspace(*p)) p++;
p++;
*ppVal = p;
while(*p && *p != ']') p++;
}
static void
_ParsePgnHeaderTag(CHAR *p)
/**
Routine description:
Parse PGN headers and extract people's names, ratings, game
result, etc...
Parameters:
CHAR *p
Return value:
static void
**/
{
CHAR *pVar, *pVal;
_ParsePgnHeaderLine(p, &pVar, &pVal);
if ((NULL == pVar) || (NULL == pVal))
{
return;
}
if (!STRNCMPI(pVar, "BLACK ", 6))
{
p = pVal;
while(*p && *p != ']') p++;
if (*p)
{
*p = '\0';
_FreeIfNotNull(g_GameData.sHeader.sPlayer[BLACK].szName);
g_GameData.sHeader.sPlayer[BLACK].szName = STRDUP(pVal);
#ifdef DEBUG
if (g_GameData.sHeader.sPlayer[BLACK].szName)
{
g_uAllocs++;
}
#endif
*p = ']';
p++;
}
}
else if (!STRNCMPI(pVar, "WHITE ", 6))
{
p = pVal;
while(*p && *p != ']') p++;
if (*p)
{
*p = '\0';
_FreeIfNotNull(g_GameData.sHeader.sPlayer[WHITE].szName);
g_GameData.sHeader.sPlayer[WHITE].szName = STRDUP(pVal);
#ifdef DEBUG
if (g_GameData.sHeader.sPlayer[WHITE].szName)
{
g_uAllocs++;
}
#endif
*p = ']';
p++;
}
}
else if (!STRNCMPI(pVar, "RESULT", 6))
{
p = pVal;
if (*p == '"') p++;
if (!STRNCMPI(p, "1-0", 3))
{
GAME_RESULT res;
res.eResult = RESULT_WHITE_WON;
strcpy(res.szDescription, "PGN game");
SetGameResultAndDescription(res);
}
else if (!STRNCMPI(p, "0-1", 3))
{
GAME_RESULT res;
res.eResult = RESULT_BLACK_WON;
strcpy(res.szDescription, "PGN game");
SetGameResultAndDescription(res);
}
else if (!STRNCMPI(p, "1/2", 3))
{
GAME_RESULT res;
res.eResult = RESULT_DRAW;
strcpy(res.szDescription, "PGN game");
SetGameResultAndDescription(res);
}
while(*p && *p != ']') p++;
if (*p) p++;
}
//
// TODO: add others
//
}
FLAG
LoadPgn(CHAR *szPgn)
/**
Routine description:
Load a PGN blob and create a move list from it.
Parameters:
CHAR *szPgn
Return value:
FLAG
**/
{
POSITION *pos;
CHAR szMove[16];
MOVE mv;
CHAR *p = szPgn;
ULONG uMoveCount = 0;
ULONG x;
FLAG fRet = FALSE;
FLAG fOldPost = g_Options.fShouldPost;
g_Options.fShouldPost = FALSE;
ResetGameList();
SetRootToInitialPosition();
pos = GetRootPosition();
//
// Get rid of newlines, non-space spaces and .'s
//
while(*p)
{
if (isspace(*p)) *p = ' ';
if (*p == '.') *p = ' ';
p++;
}
p = szPgn;
while(*p)
{
while(*p && isspace(*p)) p++;
while(*p && (isdigit(*p) || (*p == '.'))) p++;
if (*p == '[')
{
_ParsePgnHeaderTag(p);
while(*p && *p != ']') p++;
if (*p) p++;
}
else if (*p == '{')
{
while(*p && *p != '}') p++;
if (*p) p++;
}
else if (isalpha(*p))
{
//
// Copy the thing we think is a move.
//
memset(szMove, 0, sizeof(szMove));
x = 0;
while(*p && (!isspace(*p)))
{
if (x < (ARRAY_LENGTH(szMove) - 1))
{
szMove[x] = *p;
}
x++;
p++;
}
switch (LooksLikeMove(szMove))
{
case MOVE_SAN:
mv = ParseMoveSan(szMove, pos);
break;
case MOVE_ICS:
mv = ParseMoveIcs(szMove, pos);
break;
default:
mv.uMove = 0;
break;
}
//if (FALSE == SanityCheckMove(pos, mv))
// {
// Trace("Error in PGN(?) Bad chunk \"%s\"\n", szMove);
// goto end;
// }
if (FALSE == OfficiallyMakeMove(mv, 0, TRUE))
{
Trace("Error in PGN(?) Can't play move \"%s\"\n", szMove);
goto end;
}
pos = GetRootPosition();
uMoveCount++;
}
else
{
while(*p && !isspace(*p)) p++;
}
}
fRet = TRUE;
end:
g_Options.fShouldPost = fOldPost;
return(fRet);
}
char *
CleanupString(char *pIn)
/**
Routine description:
Ckeans up a PGN tag containing part of the opening line
name... gets rid of the quotes around the name and the newline.
Parameters:
char *pIn
Return value:
char
**/
{
static char buf[256];
char *p = pIn;
char *q = buf;
//
// Skip leading whitespace in input
//
buf[0] = '\0';
while(isspace(*p)) p++;
if (*p == '\0') return(buf);
do
{
//
// Copy A-Z, 0-9, space, -, ., (, or )
//
// Ignore others
//
if ((*p == ' ') ||
(isalpha(*p)) ||
(isdigit(*p)) ||
(*p == '-') ||
(*p == '.') ||
(*p == ',') ||
(*p == '(') ||
(*p == ')'))
{
*q = *p;
q++;
if ((q - buf) > (sizeof(buf) - 1)) break;
}
p++;
}
while(*p);
*q = '\0';
buf[255] = 0;
return(buf);
}
|