diff options
Diffstat (limited to 'mbbsd/gomo.c')
-rw-r--r-- | mbbsd/gomo.c | 119 |
1 files changed, 58 insertions, 61 deletions
diff --git a/mbbsd/gomo.c b/mbbsd/gomo.c index d2196bab..c7e14923 100644 --- a/mbbsd/gomo.c +++ b/mbbsd/gomo.c @@ -445,6 +445,61 @@ gomo_genlog(ChessInfo* info, FILE* fp, ChessGameResult result) fputs("</gomokulog>\n", fp); } +static int gomo_loadlog(FILE *fp, ChessInfo *info) +{ + char buf[256]; + +#define INVALID_ROW(R) ((R) < 0 || (R) >= BRDSIZ) +#define INVALID_COL(C) ((C) < 0 || (C) >= BRDSIZ) + while (fgets(buf, sizeof(buf), fp)) { + if (strcmp("</gomokulog>\n", buf) == 0) + return 1; + else if (strncmp("black:", buf, 6) == 0 || + strncmp("white:", buf, 6) == 0) { + /* /(black|white):([a-zA-Z0-9]+)/; $2 */ + userec_t rec; + ChessUser *user = (buf[0] == 'b' ? &info->user1 : &info->user2); + + chomp(buf); + if (getuser(buf + 6, &rec)) + gomo_init_user_userec(&rec, user); + } else if (buf[0] == '[') { + /* "[ 1]¡´ ==> H8 [ 2]¡³ ==> H9" */ + gomo_step_t step = { CHESS_STEP_NORMAL }; + int c, r; + const char *p; + int i; + + for(i=0; i<2; i++) { + p = strchr(buf, '>'); + + if (p == NULL) break; + + ++p; /* skip '>' */ + while (*p && isspace(*p)) ++p; + if (!*p) break; + + /* i=0, p -> "H8 ..." */ + /* i=1, p -> "H9\n" */ + c = p[0] - 'A'; + r = BRDSIZ - atoi(p + 1); + + if (INVALID_COL(c) || INVALID_ROW(r)) + break; + + step.color = i==0? BLK : WHT; + step.loc.r = r; + step.loc.c = c; + ChessHistoryAppend(info, &step); + } + } + } +#undef INVALID_ROW +#undef INVALID_COL + return 0; +} + + void gomoku(int s, ChessGameMode mode) { @@ -503,71 +558,13 @@ ChessInfo* gomoku_replay(FILE* fp) { ChessInfo *info; - char buf[256]; info = NewChessInfo(&gomo_actions, &gomo_constants, 0, CHESS_MODE_REPLAY); - while (fgets(buf, sizeof(buf), fp)) { - if (strcmp("</gomokulog>\n", buf) == 0) - break; - else if (strncmp("black:", buf, 6) == 0 || - strncmp("white:", buf, 6) == 0) { - /* /(black|white):([a-zA-Z0-9]+)/; $2 */ - userec_t rec; - ChessUser *user = (buf[0] == 'b' ? &info->user1 : &info->user2); - - chomp(buf); - if (getuser(buf + 6, &rec)) - gomo_init_user_userec(&rec, user); - } else if (buf[0] == '[') { - /* "[ 1]¡´ ==> H8 [ 2]¡³ ==> H9" */ - gomo_step_t step = { CHESS_STEP_NORMAL, BLK }; - int c, r; - const char *p = strchr(buf, '>'); - - if (p == NULL) continue; - - ++p; /* skip '>' */ - while (*p && isspace(*p)) ++p; - if (!*p) continue; - - /* p -> "H8 ..." */ - c = p[0] - 'A'; - r = BRDSIZ - atoi(p + 1); - -#define INVALID_ROW(R) ((R) < 0 || (R) >= BRDSIZ) -#define INVALID_COL(C) ((C) < 0 || (C) >= BRDSIZ) - if (INVALID_COL(c) || INVALID_ROW(r)) - continue; - - step.loc.r = r; - step.loc.c = c; - ChessHistoryAppend(info, &step); - - p = strchr(p, '>'); - - if (p == NULL) continue; - - ++p; /* skip '>' */ - while (*p && isspace(*p)) ++p; - if (!*p) continue; - - /* p -> "H9\n" */ - c = p[0] - 'A'; - r = BRDSIZ - atoi(p + 1); - - if (INVALID_COL(c) || INVALID_ROW(r)) - continue; - - step.color = WHT; - step.loc.r = r; - step.loc.c = c; - ChessHistoryAppend(info, &step); - -#undef INVALID_ROW -#undef INVALID_COL - } + if(!gomo_loadlog(fp, info)) { + DeleteChessInfo(info); + return NULL; } info->board = malloc(sizeof(board_t)); |