File: moves.c

package info (click to toggle)
pgn-extract 16.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 912 kB
  • ctags: 737
  • sloc: ansic: 8,857; makefile: 103
file content (704 lines) | stat: -rw-r--r-- 21,196 bytes parent folder | download
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
/*
 *  Program: pgn-extract: a Portable Game Notation (PGN) extractor.
 *  Copyright (C) 1994-2005 David Barnes
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 1, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  David Barnes may be contacted as D.J.Barnes@kent.ac.uk
 *  http://www.cs.kent.ac.uk/people/staff/djb/
 *
 */

	/***
	 * These routines are concerned with gathering moves of
	 * the various sorts of variations specified by the -v
	 * and -x flags.  In the former case, there are also functions
	 * for checking the moves of a game against the variation
	 * lists that are wanted.  Checking of the variations specified
	 * by the -x flag is handled elsewhere by apply_move_list().
	 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "bool.h"
#include "mymalloc.h"
#include "lines.h"
#include "defs.h"
#include "typedef.h"
#include "map.h"
#include "lists.h"
#include "tokens.h"
#include "taglist.h"
#include "lex.h"
#include "moves.h"
#include "apply.h"
#include "decode.h"

/* Define a character that can be used in the variations file to
 * mean that we don't mind what move was played at this point.
 * So: 
 *	* b6
 * means look for all games in which Black playes 1...b6, regardless
 * of White's first move.
 */
#define ANY_MOVE '*'
/* Define a character that can be used in the variations file to
 * mean that we do not wish to match a particular move.
 * So:
 *	e4 c5 !Nf3
 * means look for games in which Black does not play 2. Nf3 against
 * the Sicilian defence.
 */
#define DISALLOWED_MOVE '!'

    /* Hold details of a single move within a variation. */
typedef struct{
    /* Characters of the move.
     * Alternative notations for the same move are separated by
     * a non-move character, e.g.:
     *	cxd|cxd4|c5xd4
     * could all be alternatives for the same basic pawn capture.
     */
    char *move;
    /* If we are interested in matching the moves in any order,
     * then we need to record whether or not the current move has
     * been matched or not.
     */
    Boolean matched;
} variant_move;

    /* Hold details of a single variation, with a pointer to
     * an alternative variation.
     */
typedef struct variation_list {
    /* The list of moves. */
    variant_move *moves;
    /* Keep a count of how many ANY_MOVE moves there are in the move
     * list for each colour.  If these are non-zero then one is used
     * when a match fails when looking for permutations.
     */
    unsigned num_white_any_moves;
    unsigned num_black_any_moves;
    /* Keep a count of how many DISALLOWED_MOVE moves there are in the move
     * list for each colour.  If these are non-zero then the game
     * must be searched for them when looking for permutations before
     * the full search is made.
     */
    unsigned num_white_disallowed_moves;
    unsigned num_black_disallowed_moves;
    /* How many half-moves in the variation? */
    unsigned length;
    struct variation_list *next;
} variation_list;

    /* The head of the variations-of-interest list. */
static variation_list *games_to_keep = NULL;

static Boolean textual_variation_match(const char *variation_move,
					const unsigned char *actual_move);

	/*** Functions concerned with reading details of the variations
	 *** of interest.
	 ***/

	/* Remove any move number prefix from str.
	 * Return NULL if there is no move (only number)
	 * otherwise return the head of the move portion.
	 */
static char *
strip_move_number(char *str)
{
    while(isdigit((int) *str)){
      str++;
    }
    while(*str == '.'){
      str++;
    }
    if(*str != '\0'){
	return str;
    }
    else{
	return (char *)NULL;
    }
}



/* Define values for malloc/realloc allocation. */
#define INIT_MOVE_NUMBER 10
#define MOVE_INCREMENT 5

	/* Break up a single line of moves into a list of moves
	 * comprising a variation.
	 */
static variation_list *
compose_variation(char *line)
{   variation_list *variation;
    variant_move *move_list;
    /* Keep track of the number of moves extracted from line. */
    unsigned num_moves = 0;
    unsigned max_moves = 0;
    char *move;

    variation = (variation_list *)MallocOrDie(sizeof(variation_list));
    /* We don't yet know how many ANY_MOVEs or DISALLOWED_MOVES there
     * will be in this variation.
     */
    variation->num_white_any_moves = 0;
    variation->num_black_any_moves = 0;
    variation->num_white_disallowed_moves = 0;
    variation->num_black_disallowed_moves = 0;
    /* Allocate an initial number of pointers for the moves of the variation. */
    move_list = (variant_move *) MallocOrDie(INIT_MOVE_NUMBER *
					     sizeof(*move_list));
    max_moves = INIT_MOVE_NUMBER;
    /* Find the first move. */
    move = strtok(line," ");
    while(move != NULL){
	if((move = strip_move_number(move)) == NULL){
	    /* Only a move number. */
	}
	else{
	    /* See if we need some more space. */
	    if(num_moves == max_moves){
		 move_list = (variant_move *)realloc((void *)move_list,
			    (max_moves+MOVE_INCREMENT) * sizeof(*move_list));
		 if(move_list == NULL){
		     /* Catastrophic failure. */
		     free((void *)variation);
		     return NULL;
		 }
		 else{
		     max_moves += MOVE_INCREMENT;
		 }
	    }
	    /* Keep the move and initialise the matched field for
	     * when we start matching games.
	     */
	    move_list[num_moves].move = move;
	    move_list[num_moves].matched = FALSE;
	    /* Keep track of moves that will match anything. */
	    if(*move == ANY_MOVE){
		/* Odd numbered half-moves in the variant list are Black. */
		if(num_moves & 0x01){
		    variation->num_black_any_moves++;
		}
		else{
		    variation->num_white_any_moves++;
		}
		/* Beware of the potential for false matches. */
		if(strlen(move) > 1){
		    fprintf(GlobalState.logfile,
			"Warning: %c in %s should not be followed by additional move text.\n",
			    *move,move);
		    fprintf(GlobalState.logfile,"It could give false matches.\n");
		}
	    }
	    else if(*move == DISALLOWED_MOVE){
		/* Odd numbered half-moves in the variant list are Black. */
		if(num_moves & 0x01){
		    variation->num_black_disallowed_moves++;
		}
		else{
		    variation->num_white_disallowed_moves++;
		}
	    }
	    else{
		/* Unadorned move. */
	    }
	    num_moves++;
	}
	move = strtok((char *)NULL," ");
    }
    variation->moves = move_list;
    variation->length = num_moves;
    variation->next = NULL;
    return variation;
}

	/* Read each line of input and decompose it into a variation
	 * to be placed in the games_to_keep list.
	 */
void
add_textual_variations_from_file(FILE *fpin)
{   char *line;

    while((line = read_line(fpin)) != NULL){
	add_textual_variation_from_line(line);
    }
}

	/* Add the text of the given line to the list of games_to_keep.
	 */
void
add_textual_variation_from_line(char *line)
{
    if(non_blank_line(line)){
	variation_list *next_variation = compose_variation(line);
	if(next_variation != NULL){
	    next_variation->next = games_to_keep;
	    games_to_keep = next_variation;
	}
    }
}

	/*** Functions concerned with reading details of the positional
	 *** variations of interest.
	 ***/

	/* Break up a single line of moves into a list of moves
	 * comprising a positional variation.
	 * In doing so, set GlobalState.depth_of_positional_search
	 * if this variation is longer than the default.
	 */
static Move *
compose_positional_variation(char *line)
{   char *move;
    /* Build a linked list of the moves of the variation. */
    Move *head = NULL, *tail = NULL;
    Boolean Ok = TRUE;
    /* Keep track of the depth. */
    unsigned depth = 0;

    move = strtok(line," ");
    while(Ok && (move != NULL) && (*move != '*')){
	if((move = strip_move_number(move)) == NULL){
	    /* Only a move number. */
	}
	else{
	    Move *next = decode_move((unsigned char *) move);

	    if(next == NULL){
		fprintf(GlobalState.logfile,"Failed to identify %s\n",move);
		Ok = FALSE;
	    }
	    else{
		/* Chain it on to the list. */
		if(tail == NULL){
		    head = next;
		    tail = next;
		}
		else{
		    tail->next = next;
		    tail = next;
		}
		next->next = NULL;
	    }
	    depth++;
	}
	/* Pick up the next likely move. */
	move = strtok(NULL," ");
    }
    if(Ok){
	/* Determine whether the depth of this variation exceeds
	 * the current default.
	 * Depth is counted in move pairs.
	 * Add some extras, in order to be surer of catching transpositions.
	 */
	depth = ((depth+1) / 2) + 4;
	if(depth > GlobalState.depth_of_positional_search){
	    GlobalState.depth_of_positional_search = depth;
	}
    }
    else{
	if(head != NULL){
	    free_move_list(head);
	}
	head = NULL;
    }
    return head;
}

	/* Read each line of input and decompose it into a positional variation
	 * to be placed in the list of required hash values.
	 */
void
add_positional_variations_from_file(FILE *fpin)
{   char *line;

    while((line = read_line(fpin)) != NULL){
	add_positional_variation_from_line(line);
    }
}

void
add_positional_variation_from_line(char *line)
{
    if(non_blank_line(line)){
	Move *next_variation = compose_positional_variation(line);
	if(next_variation != NULL){
	    /* We need a NULL fen string, because this is from
	     * the initial position.
	     */
	    store_hash_value(next_variation,(const char *)NULL);
	    free_move_list(next_variation);
	    /* We need to know globally that positional variations
	     * are of interest.
	     */
	    GlobalState.positional_variations = TRUE;
	}
    }
}

	/* Treat fen_string as being a position to be matched.
	 */
void
add_fen_positional_match(const char *fen_string)
{
    store_hash_value((Move *)NULL,fen_string);
    GlobalState.positional_variations = TRUE;
}

	/* Roughly define a move character for the purposes of textual
	 * matching.
	 */
static Boolean
move_char(char c)
{
    return (Boolean) isalpha((int) c) || isdigit((int) c) || (c == '-');
}

	/* Return TRUE if there is a match for actual_move in variation_move.
	 * A match means that the string in actual_move is found surrounded
	 * by non-move characters in variation_move. For instance,
	 *    variation_move == "Nc6|Nf3|f3" would match
	 *    actual_move == "f3" but not actual_move == "c6".
	 */
static Boolean
textual_variation_match(const char *variation_move,const unsigned char *actual_move)
{   const char *match_point;
    Boolean found = FALSE;

    for(match_point = variation_move; !found && (match_point != NULL); ){
	/* Try for a match from where we are. */
	match_point = strstr(match_point,(const char *) actual_move);
	if(match_point != NULL){
	    /* A possible match. Make sure that the match point
	     * is surrounded by non-move characters so as to be sure
	     * that we haven't picked up part way through a variation string.
	     * Assume success.
	     */
	    found = TRUE;
	    if(match_point != variation_move){
		if(move_char(match_point[-1])){
		    found = FALSE;
		}
	    }
	    if(move_char(match_point[strlen((const char *) actual_move)])){
		found = FALSE;
	    }
	    if(!found){
		/* Move on the match point and try again. */
		while(move_char(*match_point)){
		    match_point++;
		}
	    }
	}
    }
    return found;
			
}

	/*** Functions concerned with matching the moves of the current game
	 *** against the variations of interest.
	 ***/

	/* Do the moves of the current game match the given variation?
	 * Go for a straight 1-1 match in the ordering, without considering
	 * permutations.
	 * Return TRUE if so, FALSE otherwise.
	 */
static Boolean
straight_match(Move *current_game_head, variation_list variation)
{
    variant_move *moves_of_the_variation;
    /* Which is the next move that we wish to match. */
    Move *next_move;
    unsigned move_index = 0;
    /* Assume that it matches. */
    Boolean matches = TRUE;

    /* Access the head of the current game. */
    next_move = current_game_head;
    /* Go for a straight move-by-move match in the order in which
     * the variation is listed.
     * Point at the head of the moves list.
     */
    moves_of_the_variation = variation.moves;
    move_index = 0;
    while(matches && (next_move != NULL) && (move_index < variation.length)){
	Boolean this_move_matches;

	if(*(moves_of_the_variation[move_index].move) == ANY_MOVE){
	    /* Still matching as we don't care what the actual move is. */
	}
	else{
	    this_move_matches =
		textual_variation_match(moves_of_the_variation[move_index].move,
					    next_move->move);
	    if(this_move_matches){
		/* We found a match, check that it isn't disallowed. */
		if(*moves_of_the_variation[move_index].move == DISALLOWED_MOVE){
		    /* This move is disallowed and implies failure. */
		    matches = FALSE;
		}
	    }
	    else{
		if(*moves_of_the_variation[move_index].move != DISALLOWED_MOVE){
		    /* No match found for this move. */
		    matches = FALSE;
		}
		else{
		    /* This is ok, because we didn't want a match. */
		}
	    }
		
	}
	/* If we are still matching, go on to the next move. */
	if(matches){
	    move_index++;
	    next_move = next_move->next;
	}
    }
    /* The game could be shorter than the variation, so don't rely
     * on the fact that matches is still true.
     */
    matches = (move_index == variation.length);
    return matches;
}

    /* Do the moves of the current game match the given variation?
     * Try all possible orderings for the moves, within the
     * constraint of proper WHITE/BLACK moves.
     * The parameter variation is passed as a copy because we modify
     * the num_ fields within it.
     * Note that there is a possibility of a false match in this
     * function if a variant move is specified in a form such as:
     *		*|c4
     * This is because the num_ field is set from this on the basis of the
     * ANY_MOVE character at the start.  However, this could also match a
     * normal move with its c4 component.  If it is used for the latter
     * purpose then it should not count as an any_ move.  There is a warning
     * issued about this when variations are read in.
     * Return TRUE if we match, FALSE otherwise.
     *
     * The DISALLOWED_MOVE presents some problems with permutation matches
     * because an ANY_MOVE could match an otherwise disallowed move. The
     * approach that has been taken is to cause matching of a single disallowed
     * move to result in complete failure of the current match.
     */
static Boolean
permutation_match(Move *current_game_head, variation_list variation)
{   variant_move *moves_of_the_variation;
    /* Which is the next move that we wish to match? */
    Move *next_move;
    unsigned variant_index = 0;
    /* Assume that it matches. */
    Boolean matches = TRUE;
    /* How many moves have we matched?
     * When this reaches variation.length we have a full match.
     */
    unsigned matched_moves = 0;
    Boolean white_to_move = TRUE;

    moves_of_the_variation = variation.moves;
    /* Clear all of the matched fields of the variation. */
    for(variant_index = 0; variant_index < variation.length; variant_index++){
	moves_of_the_variation[variant_index].matched = FALSE;
    }
    /* Access the head of the current game. */
    next_move = current_game_head;

    /*** Stage One.
     * The first task is to ensure that there are no DISALLOWED_MOVEs in
     * the current game.
     */
    if((variation.num_white_disallowed_moves > 0) ||
	    (variation.num_black_disallowed_moves > 0)){
	unsigned tested_moves = 0;
	Boolean disallowed_move_found = FALSE;

	/* Keep going as long as we still have not found a diallowed move,
	 * we haven't matched the whole variation, and we haven't reached the end of
	 * the game.
	 */
	while((!disallowed_move_found) && (tested_moves < variation.length) &&
					(next_move != NULL)){
	    /* We want to see if next_move is a disallowed move of the variation. */
	    if(white_to_move){
		/* White; start with the first move. */
		variant_index = 0;
	    }
	    else{
		/* For a Black move, start at the second half-move in the list, if any. */
		variant_index = 1;
	    }
	    /* Try each move of the variation in turn, until a match is found. */
	    while((!disallowed_move_found) && (variant_index < variation.length)){
		if((*moves_of_the_variation[variant_index].move ==
			    DISALLOWED_MOVE) &&
		   (textual_variation_match(
		      moves_of_the_variation[variant_index].move,next_move->move))){
		    /* Found one. */
		    disallowed_move_found = TRUE;
		}
		if(!disallowed_move_found){
		    /* Move on to the next available move -- 2 half moves along. */
		    variant_index += 2;
		}
	    }
	    if(!disallowed_move_found){
		/* Ok so far, so move on. */
		tested_moves++;
		white_to_move = !white_to_move;
		next_move = next_move->next;
	    }
	}
	if(disallowed_move_found){
	    /* This rules out the whole match. */
	    matches = FALSE;
	}
	else{
	    /* In effect, each DISALLOWED_MOVE now becomes an ANY_MOVE. */
	    for(variant_index = 0; variant_index < variation.length; variant_index++){
		if(*moves_of_the_variation[variant_index].move == DISALLOWED_MOVE){
		    moves_of_the_variation[variant_index].matched = TRUE;
		    if((variant_index & 1) == 0){
			variation.num_white_any_moves++;
		    }
		    else{
			variation.num_black_any_moves++;
		    }
		}
	    }
	}
    }

    /*** Stage Two.
     * Having eliminated moves which have been disallowed, try permutations
     * of the variation against the moves of the current game.
     */
    /* Access the head of the current game. */
    next_move = current_game_head;
    white_to_move = TRUE;
    /* Keep going as long as we still have matches, we haven't
     * matched the whole variation, and we haven't reached the end of
     * the game.
     */
    while(matches && (matched_moves < variation.length) && (next_move != NULL)){
	/* Assume failure. */
	matches = FALSE;
	/* We want to find next_move in an unmatched move of the variation. */
	if(white_to_move){
	    /* Start with the first move. */
	    variant_index = 0;
	}
	else{
	    /* For a Black move, start at the second half-move in the list, if any. */
	    variant_index = 1;
	}
	/* Try each move of the variation in turn, until a match is found. */
	while((!matches) && (variant_index < variation.length)){
	    if(moves_of_the_variation[variant_index].matched){
		/* We can't try this. */
	    }
	    else{
		Boolean this_move_matches = textual_variation_match(
			    moves_of_the_variation[variant_index].move,
					next_move->move);
		if(this_move_matches){
		    /* Found it. */
		    moves_of_the_variation[variant_index].matched = TRUE;
		    matches = TRUE;
		}
	    }
	    if(!matches){
		/* Move on to the next available move -- 2 half moves along. */
		variant_index += 2;
	    }
	}
	/* See if we made a straight match. */
	if(!matches){
	    /* See if we have some ANY_MOVEs available. */
	    if(white_to_move && (variation.num_white_any_moves > 0)){
		matches = TRUE;
		variation.num_white_any_moves--;
	    }
	    else if(!white_to_move && (variation.num_black_any_moves > 0)){
		matches = TRUE;
		variation.num_black_any_moves--;
	    }
	    else{
		/* No slack. */
	    }
	}
	/* We have tried everything, did we succeed? */
	if(matches){
	    /* Yes, so move on. */
	    matched_moves++;
	    next_move = next_move->next;
	    white_to_move = !white_to_move;
	}
    }
    if(matches){
	/* Ensure that we completed the variation. */
	matches = matched_moves == (variation.length);
    }
    return matches;
}

	/* Determine whether or not the current game is wanted.
	 * It will be if it matches one of the current variations
	 * and its tag details match those that we are interested in.
	 */
Boolean
check_textual_variations(Game game_details)
{   Boolean wanted = FALSE;
    variation_list *variation;

    if(games_to_keep != NULL){
	for(variation = games_to_keep; (variation != NULL) && !wanted;
		    variation = variation->next){
	    if(GlobalState.match_permutations){
		wanted = permutation_match(game_details.moves,*variation);
	    }
	    else{
		wanted = straight_match(game_details.moves,*variation);
	    }
	}
    }
    else{
        /* There are no variations, assume that selection is done
         * on the basis of the Details.
         */
        wanted = TRUE;
    }
    return wanted;
}

	/* Determine whether the number of moves in this game
	 * is within the bounds of what we want.
	 */
Boolean
check_move_bounds(unsigned number_of_moves)
{   Boolean in_bounds = TRUE;

    if(GlobalState.check_move_bounds){
         in_bounds = (GlobalState.lower_move_bound <= number_of_moves) &&
	 		(number_of_moves <= GlobalState.upper_move_bound);
    }
    return in_bounds;
}