src/chess/pgn.c

changeset 50
41017d0a72c5
child 54
eef745ba3774
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/chess/pgn.c	Mon Jun 16 13:45:31 2014 +0200
     1.3 @@ -0,0 +1,196 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright 2014 Mike Becker. All rights reserved.
     1.8 + *
     1.9 + * Redistribution and use in source and binary forms, with or without
    1.10 + * modification, are permitted provided that the following conditions are met:
    1.11 + *
    1.12 + *   1. Redistributions of source code must retain the above copyright
    1.13 + *      notice, this list of conditions and the following disclaimer.
    1.14 + *
    1.15 + *   2. Redistributions in binary form must reproduce the above copyright
    1.16 + *      notice, this list of conditions and the following disclaimer in the
    1.17 + *      documentation and/or other materials provided with the distribution.
    1.18 + *
    1.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.29 + * POSSIBILITY OF SUCH DAMAGE.
    1.30 + *
    1.31 + */
    1.32 +
    1.33 +#include "pgn.h"
    1.34 +#include <ctype.h>
    1.35 +#include <stdlib.h>
    1.36 +#include <string.h>
    1.37 +
    1.38 +int read_pgn(FILE* stream, GameState *gamestate, GameInfo *gameinfo) {
    1.39 +    int c, i;
    1.40 +    
    1.41 +    char result[8];
    1.42 +    
    1.43 +    char tagkey[32];
    1.44 +    char tagvalue[128];
    1.45 +    
    1.46 +    // read tag pairs
    1.47 +    _Bool readmoves = 0;
    1.48 +    while (!readmoves) {
    1.49 +        while (isspace(c = fgetc(stream)));
    1.50 +        if (c == '1') {
    1.51 +            readmoves = 1;
    1.52 +            break;
    1.53 +        }
    1.54 +        if (c != '[') {
    1.55 +            return EXIT_FAILURE;
    1.56 +        }
    1.57 +        while (isspace(c = fgetc(stream)));
    1.58 +        i = 0;
    1.59 +        do {
    1.60 +            tagkey[i++] = c;
    1.61 +        } while (!isspace(c = fgetc(stream)));
    1.62 +        tagkey[i] = '\0';
    1.63 +        while (isspace(c = fgetc(stream)));
    1.64 +        if (c != '"') {
    1.65 +            return EXIT_FAILURE;
    1.66 +        }
    1.67 +        i = 0;
    1.68 +        while ((c = fgetc(stream)) != '"') {
    1.69 +            if (c == '\n') {
    1.70 +                return EXIT_FAILURE;
    1.71 +            }
    1.72 +            tagvalue[i++] = c;
    1.73 +        }
    1.74 +        tagvalue[i] = '\0';
    1.75 +        if (fgetc(stream) != ']') {
    1.76 +            return EXIT_FAILURE;
    1.77 +        }
    1.78 +
    1.79 +        if (strcmp("Result", tagkey) == 0) {
    1.80 +            memcpy(result, tagvalue, 8);
    1.81 +        }
    1.82 +    }
    1.83 +    
    1.84 +    // read moves
    1.85 +    if (fgetc(stream) != '.') {
    1.86 +        return EXIT_FAILURE;
    1.87 +    }
    1.88 +    
    1.89 +    char movestr[10];
    1.90 +    Move move;
    1.91 +    uint8_t curcol = WHITE;
    1.92 +    
    1.93 +    while (readmoves) {
    1.94 +        // move
    1.95 +        while (isspace(c = fgetc(stream)));
    1.96 +        i = 0;
    1.97 +        do {
    1.98 +            movestr[i++] = c;
    1.99 +            if (i >= 10) {
   1.100 +                return EXIT_FAILURE;
   1.101 +            }
   1.102 +        } while (!isspace(c = fgetc(stream)));
   1.103 +        movestr[i] = '\0';
   1.104 +        if (eval_move(gamestate, movestr, &move, curcol)
   1.105 +                != VALID_MOVE_SYNTAX) {
   1.106 +            return EXIT_FAILURE;
   1.107 +        }
   1.108 +        if (validate_move(gamestate, &move) != VALID_MOVE_SEMANTICS) {
   1.109 +            return EXIT_FAILURE;
   1.110 +        }
   1.111 +        apply_move(gamestate, &move);
   1.112 +        
   1.113 +        // TODO: parse comments
   1.114 +        while (isspace(c = fgetc(stream)));
   1.115 +        
   1.116 +        // end of game data encountered
   1.117 +        if (c == EOF) {
   1.118 +            break;
   1.119 +        }
   1.120 +        if (c == '1' || c == '0') {
   1.121 +            c = fgetc(stream);
   1.122 +            if (c == '-') {
   1.123 +                gamestate->resign = !gamestate->checkmate;
   1.124 +                break;
   1.125 +            } else if (c == '/') {
   1.126 +                gamestate->remis = !gamestate->stalemate;
   1.127 +                break;
   1.128 +            } else {
   1.129 +                // oops, it was a move number, go back!
   1.130 +                fseek(stream, -1, SEEK_CUR);
   1.131 +            }
   1.132 +        }
   1.133 +        
   1.134 +        // we have eaten the next valuable byte, so go back
   1.135 +        fseek(stream, -1, SEEK_CUR);
   1.136 +        
   1.137 +        // skip move number after black move
   1.138 +        if (curcol == BLACK) {
   1.139 +            while (isdigit(c = fgetc(stream)));
   1.140 +            if (c != '.') {
   1.141 +                return EXIT_FAILURE;
   1.142 +            }
   1.143 +        }
   1.144 +        curcol = opponent_color(curcol);
   1.145 +    }
   1.146 +    
   1.147 +    return EXIT_SUCCESS;
   1.148 +}
   1.149 +
   1.150 +size_t write_pgn(FILE* stream, GameState *gamestate, GameInfo *gameinfo) {
   1.151 +    // TODO: tag pairs
   1.152 +    size_t bytes = 0;
   1.153 +    
   1.154 +    // Result
   1.155 +    char *result;
   1.156 +    if (gamestate->stalemate || gamestate->remis) {
   1.157 +        result = "1/2-1/2";
   1.158 +    } else if (gamestate->checkmate || gamestate->resign) {
   1.159 +        if (gamestate->lastmove) {
   1.160 +            result = (gamestate->lastmove->move.piece & COLOR_MASK) == WHITE ?
   1.161 +                "1-0" : "0-1";
   1.162 +        } else {
   1.163 +            result = "0-1";
   1.164 +        }
   1.165 +    } else {
   1.166 +        result = "*";
   1.167 +    }
   1.168 +    fprintf(stream, "[Result \"%s\"]\n\n", result);
   1.169 +    
   1.170 +    // moves
   1.171 +    int i = 1;
   1.172 +    for (MoveList *movelist = gamestate->movelist ;
   1.173 +        movelist ; movelist = movelist->next) {
   1.174 +        
   1.175 +        if (++i % 2 == 0) {
   1.176 +            fprintf(stream, "%d. %s", i/2, movelist->move.string);
   1.177 +        } else {
   1.178 +            fprintf(stream, " %s", movelist->move.string);
   1.179 +        }
   1.180 +        
   1.181 +        // TODO: move time and maybe other comments
   1.182 +        
   1.183 +        // line break every 10 moves
   1.184 +        if (i % 20)  {
   1.185 +            fputc(' ', stream);
   1.186 +        } else {
   1.187 +            fputc('\n', stream);
   1.188 +        }
   1.189 +    }
   1.190 +    
   1.191 +    if (result[0] == '*') {
   1.192 +        fputc('\n', stream);
   1.193 +    } else {
   1.194 +        fprintf(stream, "%s\n", result);
   1.195 +    }
   1.196 +    
   1.197 +    
   1.198 +    return bytes;
   1.199 +}

mercurial