File: decode.c

package info (click to toggle)
pgn-extract 16.7-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 936 kB
  • ctags: 722
  • sloc: ansic: 9,243; makefile: 63
file content (650 lines) | stat: -rw-r--r-- 19,683 bytes parent folder | download | duplicates (2)
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
/*
 *  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/
 *
 */

            /* This file contains functions concerned with decoding
             * the original text of a move in order to determine
             *                 which MoveClass it is in,
             *                any start and end square information.
             * It extracts this information purely from the move text
             * rather than analysing the move within the context of
             * a board position.
             * This information is later refined by the semantic analysis
             * phase of the program as part of the checking of a game score.
             */

#include <stdio.h>
#include <string.h>
#include "bool.h"
#include "mymalloc.h"
#include "defs.h"
#include "typedef.h"
#include "decode.h"
#include "tokens.h"
#include "taglist.h"
#include "lex.h"

        /* Does the character represent a column of the board? */
Boolean
is_col(char c)
{
    return (FIRSTCOL <= c) && (c <= LASTCOL);
}

        /* Does the character represent a rank of the board? */
Boolean
is_rank(char c)
{
    return (FIRSTRANK <= c) && (c <= LASTRANK);
}

        /* What kind of piece is *move likely to represent?
         * Note, the provision for double-character pieces,
         * like a Russian King, means we need access to a
         * string rather than a single char.
         */
Piece
is_piece(const unsigned char *move)
{   Piece piece = EMPTY;

    switch(*move){
        case 'K': case 'k':
            piece = KING;
            break;
        case 'Q': case 'q':
        case 'D': /* Dutch/German. */
        case RUSSIAN_QUEEN:
            piece = QUEEN;
            break;
        case 'R': case 'r':
        case 'T': /* Dutch/German. */
        case RUSSIAN_ROOK:
            piece = ROOK;
            break;
        case 'N': case 'n':
        case 'P': /* Dutch. */
        case 'S': /* German. */
            piece = KNIGHT;
            break;
        case 'B': 
        case 'L': /* Dutch/German. */
        case RUSSIAN_BISHOP:
            /* Lower case 'b' is most likely to be a pawn reference. */
            piece = BISHOP;
            break;
        case RUSSIAN_KNIGHT_OR_KING:
            if(RUSSIAN_PIECE_CHECK(*(move+1)) == RUSSIAN_KING_SECOND_LETTER){
                piece = KING;
            }
            else{
                piece = KNIGHT;
            }
            break;
    }
    return piece;
}

        /* Is the symbol a capturing one?
         * In fact, this is used to recognise any general separator
         * between two parts of a move, e.g.:
         *        Nxc3, e2-e4, etc.
         */

static Boolean
is_capture(char c)
{
    return (c == 'x') || (c == 'X') || (c == ':') || (c == '-');
}

static Boolean
is_castling_character(char c)
{
    return (c == 'O') || (c == '0') || (c == 'o');
}

Boolean
is_check(char c)
{
    return (c == '+') || (c == '#');
}

        /* Allocate space in which to return the information that
         * has been gleaned from the move.
         */
Move *
new_move_structure(void)
{   Move *move = (Move *)MallocOrDie(sizeof(Move));

    move->terminating_result = NULL;
    move->epd = NULL;
    move->Nags = NULL;
    move->Comment = NULL;
    move->Variants = NULL;
    move->next = NULL;
    return move;
}

        /* Work out whatever can be gleaned from move_string of
         * the starting and ending points of the given move.
         * The move may be any legal string.
         * The scanning here is libertarian, so it relies heavily on
         * illegal moves having already been filtered out by the process
         * of lexical analysis.
         */
Move *
decode_move(const unsigned char *move_string)
{   /* The four components of the co-ordinates when known. */
    Rank from_rank = 0, to_rank = 0;
    Col from_col = 0, to_col = 0;
    MoveClass class;
    Boolean Ok = TRUE;
    /* Temporary locations until known whether they are from_ or to_. */
    Col col = 0;
    Rank rank = 0;
    /* A pointer to move along the move string. */
    const unsigned char *move = move_string;
    /* A pointer to the structure containing the details to be returned. */
    Move *move_details;
    Piece piece_to_move = EMPTY;

    /* Make an initial distinction between pawn moves and piece moves. */
    if(is_col(*move)){
        /* Pawn move. */
        class = PAWN_MOVE;
        piece_to_move = PAWN;
        col = *move;
        move++;
        if(is_rank(*move)){
            /* e4, e2e4 */
            rank = *move;
            move++;
            if(is_capture(*move)){
                move++;
            }
            if(is_col(*move)){
                from_col = col;
                from_rank = rank;
                to_col = *move;
                move++;
                if(is_rank(*move)){
                    to_rank = *move;
                    move++;
                }
            }
            else{
                to_col = col;
                to_rank = rank;
            }
        }
        else{
            if(is_capture(*move)){
                /* axb */
                move++;
            }
            if(is_col(*move)){
                /* ab, or bg8 for liberal bishop moves. */
                from_col = col;
                to_col = *move;
                move++;
                if(is_rank(*move)){
                    to_rank = *move;
                    move++;
                    /* Check the sanity of this. */
                    if((from_col != 'b') &&
                            (from_col != (to_col+1)) && (from_col != (to_col-1))){
                        Ok = FALSE;
                    }
                }
                else{
                    /* Check the sanity of this. */
                    if((from_col != (to_col+1)) && (from_col != (to_col-1))){
                        Ok = FALSE;
                    }
                }
            }
            else{
                print_error_context(GlobalState.logfile);
                fprintf(GlobalState.logfile,"Unknown pawn move %s.\n",move_string);
                Ok = FALSE;
            }
        }
        if(Ok){
            /* Look for promotions. */
            if(*move == '='){
                move++;
            }
            if(is_piece(move) != EMPTY){
                class = PAWN_MOVE_WITH_PROMOTION;
                /* @@@ Strictly speaking, if the piece is a RUSSIAN_KING
                 * then we should skip two chars.
                 */
                move++;
            }
        }
    }
    else if((piece_to_move = is_piece(move)) != EMPTY){
        class = PIECE_MOVE;
        /* Check for a two-character piece. */
        if((RUSSIAN_PIECE_CHECK(*move) == RUSSIAN_KNIGHT_OR_KING) &&
                                (piece_to_move == KING)){
            move++;
        }
        move++;
        if(is_rank(*move)){
            /* A disambiguating rank.
             * R1e1, R1xe3.
             */
            from_rank = *move;
            move++;
            if(is_capture(*move)){
                move++;
            }
            if(is_col(*move)){
                to_col = *move;
                move++;
                if(is_rank(*move)){
                    to_rank = *move;
                    move++;
                }
            }
            else{
                Ok = FALSE;
                print_error_context(GlobalState.logfile);
                fprintf(GlobalState.logfile,"Unknown piece move %s.\n",move_string);
            }
        }
        else{
            if(is_capture(*move)){
                /* Rxe1 */
                move++;
                if(is_col(*move)){
                    to_col = *move;
                    move++;
                    if(is_rank(*move)){
                        to_rank = *move;
                        move++;
                    }
                    else{
                        Ok = FALSE;
                        print_error_context(GlobalState.logfile);
                        fprintf(GlobalState.logfile,
                                        "Unknown piece move %s.\n",move_string);
                    }
                }
                else{
                    Ok = FALSE;
                    print_error_context(GlobalState.logfile);
                    fprintf(GlobalState.logfile,"Unknown piece move %s.\n",move_string);
                }
            }
            else if(is_col(*move)){
                col = *move;
                move++;
                if(is_capture(*move)){
                    move++;
                }
                if(is_rank(*move)){
                    /* Re1, Re1d1, Re1xd1 */
                    rank = *move;
                    move++;
                    if(is_capture(*move)){
                        move++;
                    }
                    if(is_col(*move)){
                        /* Re1d1 */
                        from_col = col;
                        from_rank = rank;
                        to_col = *move;
                        move++;
                        if(is_rank(*move)){
                            to_rank = *move;
                            move++;
                        }
                        else{
                            Ok = FALSE;
                            print_error_context(GlobalState.logfile);
                            fprintf(GlobalState.logfile,
                                        "Unknown piece move %s.\n",move_string);
                        }
                    }
                    else{
                        to_col = col;
                        to_rank = rank;
                    }
                }
                else if(is_col(*move)){
                    /* Rae1 */
                    from_col = col;
                    to_col = *move;
                    move++;
                    if(is_rank(*move)){
                        to_rank = *move;
                        move++;
                    }
                }
                else{
                    Ok = FALSE;
                    print_error_context(GlobalState.logfile);
                    fprintf(GlobalState.logfile,"Unknown piece move %s.\n",move_string);
                }
            }
            else{
                Ok = FALSE;
                print_error_context(GlobalState.logfile);
                fprintf(GlobalState.logfile,"Unknown piece move %s.\n",move_string);
            }
        }
    }
    else if(is_castling_character(*move)){
         /* Some form of castling. */
         move++;
         /* Allow separators to be optional. */
         if(*move == '-'){
             move++;
         }
         if(is_castling_character(*move)){
             move++;
             if(*move == '-'){
                 move++;
             }
             if(is_castling_character(*move)){
                 class = QUEENSIDE_CASTLE;
                 move++;
             }
             else{
                 class = KINGSIDE_CASTLE;
             }
         }
         else{
             print_error_context(GlobalState.logfile);
             fprintf(GlobalState.logfile,"Unknown castling move %s.\n",move_string);
             Ok = FALSE;
         }
    }
    else{
        print_error_context(GlobalState.logfile);
        fprintf(GlobalState.logfile,"Unknown move %s.\n",move_string);
        Ok = FALSE;
    }
    if(Ok){
        /* Allow trailing checks. */
        while(is_check(*move)){
            move++;
        }
        if(*move == '\0'){
            /* Nothing more to check. */
        }
        else if(((strcmp((const char *) move,"ep") == 0) ||
                            (strcmp((const char *) move,"e.p.") == 0)) &&
                (class == PAWN_MOVE)){
            /* These are ok. */
            class = ENPASSANT_PAWN_MOVE;
        }
        else{
            Ok = FALSE;
            print_error_context(GlobalState.logfile);
            fprintf(GlobalState.logfile,
                "Unknown text trailing move %s <%s>.\n",move_string,move);
        }
    }
    /* Store all of the details gathered, even if the move is illegal. */
    if(!Ok){
        class = UNKNOWN_MOVE;
    }
    move_details = new_move_structure();
    strcpy((char *) move_details->move,(const char *) move_string);
    move_details->class = class;
    move_details->piece_to_move = piece_to_move;
    move_details->from_col = from_col;
    move_details->from_rank = from_rank;
    move_details->to_col = to_col;
    move_details->to_rank = to_rank;
    move_details->captured_piece = EMPTY;
    move_details->check_status = NOCHECK;
    return move_details;
}

Move *
decode_algebraic(Move *move_details, Board *board)
{   short from_r = RankConvert(move_details->from_rank);
    short from_c = ColConvert(move_details->from_col);
    Piece piece_to_move = EXTRACT_PIECE(board->board[from_r][from_c]);

    if(piece_to_move != EMPTY){
        /* Check for the special case of castling. */
        if((piece_to_move == KING) && (move_details->from_col == 'e')){
           if(move_details->to_col == 'g'){
                move_details->class = KINGSIDE_CASTLE;
            }
            else if(move_details->to_col == 'c'){
                move_details->class = QUEENSIDE_CASTLE;
            }
            else{
                move_details->class = PIECE_MOVE;
                move_details->piece_to_move = piece_to_move;
            }
        }
        else{
            if(piece_to_move == PAWN){
                move_details->class = PAWN_MOVE;
            }
            else{
                move_details->class = PIECE_MOVE;
            }
            move_details->piece_to_move = piece_to_move;
        }
        move_details->captured_piece = EMPTY;
        move_details->check_status = NOCHECK;
    }
    return move_details;
}

        /* See if move_string seems to represent the text of a valid move.
         * Don't print any error messages, just return TRUE or FALSE.
         */
Boolean
move_seems_valid(const unsigned char *move_string)
{   MoveClass class;
    Boolean Ok = TRUE;
    /* A pointer to move along the move string. */
    unsigned const char *move = move_string;

    /* Make an initial distinction between pawn moves and piece moves. */
    if(is_col(*move)){
        /* Pawn move. */
        class = PAWN_MOVE;
        move++;
        if(is_rank(*move)){
            /* e4, e2e4 */
            move++;
            if(is_capture(*move)){
                move++;
            }
            if(is_col(*move)){
                move++;
                if(is_rank(*move)){
                    move++;
                }
            }
            else{
            }
        }
        else{
            if(is_capture(*move)){
                /* axb */
                move++;
            }
            if(is_col(*move)){
                /* ab */
                move++;
                if(is_rank(*move)){
                    move++;
                }
            }
            else{
                Ok = FALSE;
            }
        }
        if(Ok){
            /* Look for promotions. */
            if(*move == '='){
                move++;
            }
            if(is_piece(move) != EMPTY){
                class = PAWN_MOVE_WITH_PROMOTION;
                /* @@@ Strictly speaking, if the piece is a RUSSIAN_KING
                 * then we should skip two chars.
                 */
                move++;
            }
        }
    }
    else if(is_piece(move) != EMPTY){
        class = PIECE_MOVE;
        /* Check for a two-character piece. */
        if((RUSSIAN_PIECE_CHECK(*move) == RUSSIAN_KNIGHT_OR_KING) &&
                        (is_piece(move) == KING)){
            move++;
        }
        move++;
        if(is_rank(*move)){
            /* A disambiguating rank.
             * R1e1, R1xe3.
             */
            move++;
            if(is_capture(*move)){
                move++;
            }
            if(is_col(*move)){
                move++;
                if(is_rank(*move)){
                    move++;
                }
            }
            else{
                Ok = FALSE;
            }
        }
        else{
            if(is_capture(*move)){
                /* Rxe1 */
                move++;
                if(is_col(*move)){
                    move++;
                    if(is_rank(*move)){
                        move++;
                    }
                    else{
                        Ok = FALSE;
                    }
                }
                else{
                    Ok = FALSE;
                }
            }
            else if(is_col(*move)){
                move++;
                if(is_capture(*move)){
                    move++;
                }
                if(is_rank(*move)){
                    /* Re1, Re1d1, Re1xd1 */
                    move++;
                    if(is_capture(*move)){
                        move++;
                    }
                    if(is_col(*move)){
                        /* Re1d1 */
                        move++;
                        if(is_rank(*move)){
                            move++;
                        }
                        else{
                            Ok = FALSE;
                        }
                    }
                }
                else if(is_col(*move)){
                    /* Rae1 */
                    move++;
                    if(is_rank(*move)){
                        move++;
                    }
                    else{
                        Ok = FALSE;
                    }
                }
                else{
                    Ok = FALSE;
                }
            }
            else{
                Ok = FALSE;
            }
        }
    }
    else if(is_castling_character(*move)){
         /* Some form of castling. */
         move++;
         /* Allow separators to be optional. */
         if(*move == '-'){
             move++;
         }
         if(is_castling_character(*move)){
             move++;
             if(*move == '-'){
                 move++;
             }
             if(is_castling_character(*move)){
                 class = QUEENSIDE_CASTLE;
                 move++;
             }
             else{
                 class = KINGSIDE_CASTLE;
             }
         }
         else{
             Ok = FALSE;
         }
    }
    else{
        Ok = FALSE;
    }
    if(Ok){
        /* Allow trailing checks. */
        while(is_check(*move)){
            move++;
        }
        if(*move == '\0'){
            /* Nothing more to check. */
        }
        else if(((strcmp((const char *) move,"ep") == 0) ||
                        (strcmp((const char *) move,"e.p.") == 0)) &&
                (class == PAWN_MOVE)){
            /* These are ok. */
            class = ENPASSANT_PAWN_MOVE;
        }
        else{
            Ok = FALSE;
        }
    }
    return Ok;
}