diff options
author | kcwu <kcwu@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2005-09-22 11:04:07 +0800 |
---|---|---|
committer | kcwu <kcwu@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2005-09-22 11:04:07 +0800 |
commit | 217974dc5f22b9f4ef4aab2e2840e8bbabcfb219 (patch) | |
tree | e39e4564cd1912c9d76f02ea433a16be38c4b8e7 /mbbsd | |
parent | 1531486196d05fc963a8723f0b9eb098a163296b (diff) | |
download | pttbbs-217974dc5f22b9f4ef4aab2e2840e8bbabcfb219.tar pttbbs-217974dc5f22b9f4ef4aab2e2840e8bbabcfb219.tar.gz pttbbs-217974dc5f22b9f4ef4aab2e2840e8bbabcfb219.tar.bz2 pttbbs-217974dc5f22b9f4ef4aab2e2840e8bbabcfb219.tar.lz pttbbs-217974dc5f22b9f4ef4aab2e2840e8bbabcfb219.tar.xz pttbbs-217974dc5f22b9f4ef4aab2e2840e8bbabcfb219.tar.zst pttbbs-217974dc5f22b9f4ef4aab2e2840e8bbabcfb219.zip |
code clean up.
git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@3201 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'mbbsd')
-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)); |