-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcldir2.c
More file actions
515 lines (411 loc) · 12.8 KB
/
cldir2.c
File metadata and controls
515 lines (411 loc) · 12.8 KB
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
/*
* cldir2.c
* (c) 2020, Brian Stephenson
* brian@bstephen.me.uk
*
* A program to test orthodox chess problems of the types:
*
* directmates
* selfmates
* relfexmates
* helpmates
*
* Input is taken from the program options and output is xml on stdout.
*
* This is the module for classifying directmates in two.
*/
#include "sengine.h"
#include "utarray.h"
#include "utstring.h"
void setup_id_board(BOARD*, ID_BOARD*);
void start_class_2_xml(void);
void end_class_2_xml(void);
void start_static_class_xml(void);
void end_static_clas_xml(void);
void start_set_class_xml(void);
void end_set_class_xml(void);
void start_virtual_class_xml(void);
void end_virtual_class_xml(void);
void start_actual_class_xml(void);
void end_actual_class_xml(void);
void add_static_type(char*);
void add_added(int);
void add_removed(int);
void add_changed(int);
void add_up_flights(int);
void add_up_checks(int);
void add_up_fgivers(int);
void add_up_caps(int);
void add_tot_up(int);
void add_var(char*);
void start_try();
void end_try();
void add_key(char*);
void add_threat(char*);
void add_refut(char*);
char get_piece_type(enum COLOUR, BOARD*, unsigned char);
void update_id_board(enum COLOUR, BOARD*, ID_BOARD*, ID_BOARD*);
UT_string* get_mate_class(BOARD*, BOARD*, ID_BOARD*);
void classify_white_move(BOARD*, BOARD*, ID_BOARD*, ID_BOARD*);
void classify_vars(BOARDLIST*, BOARDLIST*, BOARD*, ID_BOARD*);
static void do_statics(DIR_SOL*, BOARD*);
static void do_sets(DIR_SOL*, BOARD*, ID_BOARD*);
static void do_virtual(DIR_SOL*, BOARD*, ID_BOARD*);
static void do_actual(DIR_SOL*, BOARD*, ID_BOARD*);
static int get_set_size(DIR_SOL*);
static void get_changed_and_removed_mates(DIR_SOL*);
static void get_added_mates(DIR_SOL*);
static bool mate_equals(BOARD*, BOARD*, BOARD*);
static bool black_equals(BOARD*, BOARD*);
static bool is_flight_giver(BOARD*, unsigned int);
static bool is_provided(BOARD*, BOARDLIST*);
static void classify_threats(BOARD*, BOARDLIST*, ID_BOARD*);
static int ChangedMates = 0;
static int AddedMates = 0;
static int RemovedMates = 0;
static int UpFlights = 0;
static int UpChecks = 0;
static int UpFgivers = 0;
static int UpCaps = 0;
static int TotUp = 0;
char pieces[] = "0PSBRQK";
char* lab_check = "CHECK";
char* lab_kcast = "0-0";
char* lab_qcast = "0-0-0";
char* lab_p_flight = "P-FLIGHT";
char* lab_s_flight = "S_FLIGHT";
int featsort(const void* a, const void* b)
{
char** aa = (char**) a;
char** bb = (char**) b;
#ifdef MOVESTAT
fprintf(stderr, "%s\n%s\n\n", *aa, *bb);
#endif
return strcmp(*aa, *bb);
}
void class_direct_2(DIR_SOL* insol, BOARD* inBrd)
{
ID_BOARD* p_init_idbd;
start_class_2_xml();
do_statics(insol, inBrd);
p_init_idbd = getIdBoard();
setup_id_board(inBrd, p_init_idbd);
#ifdef MOVESTAT
fprintf(stderr, "WHITE_ID => %s\n", p_init_idbd->white_ids);
fprintf(stderr, "BLACK_ID => %s\n", p_init_idbd->black_ids);
#endif
if (inBrd->check == false) {
do_sets(insol, inBrd, p_init_idbd);
}
do_virtual(insol, inBrd, p_init_idbd);
do_actual(insol, inBrd, p_init_idbd);
freeIdBoard(p_init_idbd);
end_class_2_xml();
return;
}
void do_statics(DIR_SOL* insol, BOARD* inBrd)
{
#ifndef NDEBUG
fputs("do_statics()\n", stderr);
#endif
BOARDLIST* bList;
unsigned int flights;
int set_count;
int set_full_count;
bool set_complete = false;
start_static_class_xml();
if (inBrd->check == false) {
bList = generateBlackBoardlist(inBrd, 1, &flights);
if (insol->set->vektor != NULL) {
BOARD* elt;
if (bList->vektor != NULL) {
LL_COUNT(insol->set->vektor, elt, set_count);
LL_COUNT(bList->vektor, elt, set_full_count);
set_complete = (set_count == set_full_count) ? true : false;
}
}
}
/*
* Classify type
*/
if (inBrd->check == false) {
get_changed_and_removed_mates(insol);
get_added_mates(insol);
int SetSize = get_set_size(insol);
add_changed(ChangedMates);
add_added(AddedMates);
add_removed(RemovedMates);
if (insol->keys->vektor->check == true) {
add_static_type("CHECKING_KEY");
} else if (insol->keys->vektor->threat == NULL) {
if ((SetSize > 0) && (set_complete == true)) {
if (ChangedMates > 0) {
add_static_type("MUTATE");
} else {
add_static_type("WAITER");
}
} else {
add_static_type("INCOMPLETE_BLOCK");
}
} else {
if ((SetSize > 0) && (set_complete == true)) {
add_static_type("BLOCK_THREAT");
} else {
add_static_type("THREAT");
}
}
/*
* Count strong unprovided moves.
*/
{
BOARD* elt;
BOARDLIST* sets = insol->set;
LL_FOREACH(bList->vektor, elt) {
if (elt->mover == KING) {
if (is_provided(elt, sets) == false) {
UpFlights++;
TotUp++;
}
} else if (elt->check == true) {
if (is_provided(elt, sets) == false) {
UpChecks++;
TotUp++;
}
} else if (elt->captured == true) {
if (is_provided(elt, sets) == false) {
UpCaps++;
TotUp++;
}
} else if (is_flight_giver(elt, flights) == true) {
if (is_provided(elt, sets) == false) {
UpFgivers++;
TotUp++;
}
}
}
}
freeBoardlist(bList);
} else {
add_static_type("WHITE_IN_CHECK");
}
add_up_flights(UpFlights);
add_up_checks(UpChecks);
add_up_fgivers(UpFgivers);
add_up_caps(UpCaps);
add_tot_up(TotUp);
end_static_clas_xml();
return;
}
bool is_provided(BOARD* inBrd, BOARDLIST* sets)
{
BOARD* elt;
bool rc = false;
LL_FOREACH(sets->vektor, elt) {
if ((inBrd->from == elt->from) && (inBrd->to == elt->to)) {
rc = true;
break;
}
}
return rc;
}
bool is_flight_giver(BOARD* inBrd, unsigned int inFlights)
{
unsigned int flights;
BOARDLIST* bList = generateBlackBoardlist(inBrd, 1, &flights);
freeBoardlist(bList);
return (flights > inFlights) ? true : false;
}
int get_set_size(DIR_SOL* insol)
{
BOARD* elt;
int rc = 0;
if (insol->set->vektor != NULL) {
LL_COUNT(insol->set->vektor, elt, rc);
}
return rc;
}
void get_changed_and_removed_mates(DIR_SOL* insol)
{
// Iterate round set variations counting changed and removed.
// Only inspect variations that are dual-free in set and actual.
// A mate by the key-piece going to the same square as in the set play is not a change.
if (insol->set->vektor != NULL) {
BOARDLIST* defences = insol->keys->vektor->nextply;
BOARD* setDefence;
LL_FOREACH(insol->set->vektor, setDefence) {
int mateCount;
BOARD* tp;
BOARDLIST* setMates = setDefence->nextply;
LL_COUNT(setMates->vektor, tp, mateCount);
if (mateCount == 1) {
bool found = false;
BOARD* setMate = setMates->vektor;
BOARD* defence;
LL_FOREACH(defences->vektor, defence) {
if (black_equals(setDefence, defence) == true) {
int aMateCount;
BOARDLIST* actualMates = defence->nextply;
BOARD* tp1;
LL_COUNT(actualMates->vektor, tp1, aMateCount);
if (aMateCount == 1) {
found = true;
if (mate_equals(insol->keys->vektor, setMate, actualMates->vektor) == false) {
ChangedMates++;
}
break;
}
}
}
if (found == false) {
RemovedMates++;
}
}
}
}
return;
}
void get_added_mates(DIR_SOL* insol)
{
// Iterate round actual variations counting added mates.
// Inspect dual-free variations only.
BOARDLIST* defences = insol->keys->vektor->nextply;
BOARDLIST* setVars = insol->set;
BOARD* defence;
LL_FOREACH(defences->vektor, defence) {
int MateCount;
BOARD* tp1;
BOARDLIST* mates = defence->nextply;
LL_COUNT(mates->vektor, tp1, MateCount);
if (MateCount == 1) {
bool found = false;
BOARD* setVar;
LL_FOREACH(setVars->vektor, setVar) {
if (black_equals(defence, setVar) == true) {
int ccount;
BOARD* tp2;
BOARDLIST* setMates = setVar->nextply;
LL_COUNT(setMates->vektor, tp2, ccount);
if (ccount == 1) {
found = true;
break;
}
}
}
if (found == false) {
AddedMates++;
}
}
}
return;
}
void do_sets(DIR_SOL* insol, BOARD* inBrd, ID_BOARD* in_Idb)
{
BOARDLIST* wlist;
#ifndef NDEBUG
fputs("do_sets()\n", stderr);
#endif
start_set_class_xml();
wlist = generateWhiteBoardlist(inBrd, 1);
classify_vars(wlist, insol->set, inBrd, in_Idb);
freeBoardlist(wlist);
end_set_class_xml();
return;
}
void do_virtual(DIR_SOL* insol, BOARD* inBrd, ID_BOARD* in_Idb)
{
BOARDLIST* wlist;
#ifndef NDEBUG
fputs("do_virtual()\n", stderr);
#endif
BOARDLIST* tries = insol->tries;
BOARD* elt;
start_virtual_class_xml();
LL_FOREACH(tries->vektor, elt) {
start_try();
ID_BOARD* newIB = cloneIdBoard(in_Idb);
update_id_board(WHITE, elt, in_Idb, newIB);
classify_white_move(inBrd, elt, in_Idb, newIB);
if (elt->threat != NULL) {
classify_threats(elt, elt->threat, newIB);
}
wlist = generateWhiteBoardlist(elt, 1);
classify_vars(wlist, elt->nextply, elt, newIB);
freeBoardlist(wlist);
freeIdBoard(newIB);
end_try();
}
end_virtual_class_xml();
return;
}
void do_actual(DIR_SOL* insol, BOARD* inBrd, ID_BOARD* in_Idb)
{
BOARDLIST* wlist;
#ifndef NDEBUG
fputs("do_actual()\n", stderr);
#endif
BOARD* wkey = insol->keys->vektor;
start_actual_class_xml();
ID_BOARD* newIB = cloneIdBoard(in_Idb);
update_id_board(WHITE, wkey, in_Idb, newIB);
classify_white_move(inBrd, wkey, in_Idb, newIB);
if (wkey->threat != NULL) {
classify_threats(wkey, wkey->threat, newIB);
}
wlist = generateWhiteBoardlist(wkey, 1);
classify_vars(wlist, wkey->nextply, wkey, newIB);
freeBoardlist(wlist);
freeIdBoard(newIB);
end_actual_class_xml();
return;
}
void classify_threats(BOARD* initBrd, BOARDLIST* threats, ID_BOARD* in_Idb)
{
#ifndef NDEBUG
fputs("classify_threats()\n", stderr);
#endif
unsigned int count;
BOARD* elt;
LL_COUNT(threats->vektor, elt, count);
if (count == 2) {
// Treat promotion to Q/R or Q/B as non dual.
BOARD* m1 = threats->vektor;
BOARD* m2 = threats->vektor->next;
if ((m1->mover == PAWN) && (m2->mover == PAWN)) {
if ((m1->from == m2->from) && (m1->to == m2->to)) {
if ((m1->promotion == QUEEN) && ((m2->promotion == ROOK) || (m2->promotion == BISHOP))) {
// Allowable dual - delete underpromotion.
LL_DELETE(m1, m2);
count--;
}
}
}
}
if (count == 1) {
UT_string* s = get_mate_class(initBrd, threats->vektor, in_Idb);
add_threat(utstring_body(s));
utstring_free(s);
} else {
UT_string* d;
utstring_new(d);
utstring_printf(d, "DUALS(%u)", count);
add_threat(utstring_body(d));
utstring_free(d);
}
return;
}
bool mate_equals(BOARD* key, BOARD* setmate, BOARD* actualmate)
{
if (setmate->mover != actualmate->mover) return false;
if (setmate->to != actualmate->to) return false;
if ((setmate->from != actualmate->from) && (key->to != actualmate->from)) return false;
if (setmate->promotion != actualmate->promotion) return false;
return true;
}
bool black_equals(BOARD* brda, BOARD* brdb)
{
if (brda->mover != brdb->mover) return false;
if (brda->from != brdb->from) return false;
if (brda->to != brdb->to) return false;
if (brda->promotion != brdb->promotion) return false;
return true;
}