diff options
author | scw <scw@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2005-09-11 17:06:58 +0800 |
---|---|---|
committer | scw <scw@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2005-09-11 17:06:58 +0800 |
commit | 915be02b2a9538045ac94c4cac2fdfd49fd2a1e6 (patch) | |
tree | 84a4e6360baf964aac5e3e2443400a3a1847f32f | |
parent | f1c53390c4817e5f9552588f298808764ef1bc65 (diff) | |
download | pttbbs-915be02b2a9538045ac94c4cac2fdfd49fd2a1e6.tar pttbbs-915be02b2a9538045ac94c4cac2fdfd49fd2a1e6.tar.gz pttbbs-915be02b2a9538045ac94c4cac2fdfd49fd2a1e6.tar.bz2 pttbbs-915be02b2a9538045ac94c4cac2fdfd49fd2a1e6.tar.lz pttbbs-915be02b2a9538045ac94c4cac2fdfd49fd2a1e6.tar.xz pttbbs-915be02b2a9538045ac94c4cac2fdfd49fd2a1e6.tar.zst pttbbs-915be02b2a9538045ac94c4cac2fdfd49fd2a1e6.zip |
GO chess replay implemented
git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@3155 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rw-r--r-- | mbbsd/chc.c | 3 | ||||
-rw-r--r-- | mbbsd/chess.c | 15 | ||||
-rw-r--r-- | mbbsd/go.c | 167 |
3 files changed, 177 insertions, 8 deletions
diff --git a/mbbsd/chc.c b/mbbsd/chc.c index 77b12c41..e201bc91 100644 --- a/mbbsd/chc.c +++ b/mbbsd/chc.c @@ -901,6 +901,9 @@ chc_gameend(ChessInfo* info, ChessGameResult result) currutmp->chess_elo_rating = user1->rating; chcusr_put(&cuser, user1); passwd_update(usernum, &cuser); + } else if (info->mode == CHESS_MODE_REPLAY) { + free(info->board); + free(info->tag); } } diff --git a/mbbsd/chess.c b/mbbsd/chess.c index 1c5b44d1..56124c42 100644 --- a/mbbsd/chess.c +++ b/mbbsd/chess.c @@ -655,10 +655,9 @@ ChessPlayFuncHis(ChessInfo* info) game_result = CHESS_RESULT_WIN; endturn = 1; } else if (result == CHESS_STEP_PASS) { - ChessStepType type = CHESS_STEP_PASS; strcpy(info->last_movestr, "µê¤â"); - info->pass[info->turn] = 1; + info->pass[(int) info->turn] = 1; endturn = 1; } else if (result == CHESS_STEP_TIE) { if (ChessAnswerRequest(info, "©M´Ñ")) { @@ -684,7 +683,7 @@ ChessPlayFuncHis(ChessInfo* info) game_result = CHESS_RESULT_CONTINUE; } endturn = 1; - info->pass[info->turn] = 0; + info->pass[(int) info->turn] = 0; ChessStepMade(info, 1); info->actions->drawstep(info, &info->step_tmp); } else if (result == CHESS_STEP_UNDO_ACC) { @@ -1255,9 +1254,13 @@ ChessReplayGame(const char* fname) info = ChessReplayMap[found].func(fp); fclose(fp); - screen_backup(&oldscreen); - ChessPlay(info); - screen_restore(&oldscreen); + if (info) { + screen_backup(&oldscreen); + ChessPlay(info); + screen_restore(&oldscreen); + + DeleteChessInfo(info); + } return 0; } @@ -717,12 +717,16 @@ go_prepare_step(ChessInfo* info, const go_step_t* step) tag->need_redraw = 1; ((go_tag_t*)info->tag)->feed_back = 0.0; } - } + } else if (step->type == CHESS_STEP_PASS) + strcpy(info->last_movestr, "µê¤â"); } static ChessGameResult go_apply_step(board_t board, const go_step_t* step) { + if (step->type != CHESS_STEP_NORMAL) + return CHESS_RESULT_CONTINUE; + switch (step->color) { case BWHITE: case BBLACK: @@ -790,6 +794,10 @@ static void go_gameend(ChessInfo* info, ChessGameResult result) { /* TODO: implement */ + if (info->mode == CHESS_MODE_REPLAY) { + free(info->board); + free(info->tag); + } } static void @@ -915,8 +923,163 @@ gochess_watch(void) return ChessWatchGame(&gochess, GO, "³ò´Ñ"); } +static int +mygetc(FILE* fp, char* buf, int* idx, int len) +{ + for (;;) { + while (buf[*idx] && isspace(buf[*idx])) ++*idx; + + if (buf[*idx]) { + ++*idx; + return buf[*idx - 1]; + } + + if (fgets(buf, len, fp) == NULL) + return EOF; + + if (strcmp(buf, "<golog>\n") == 0) + return EOF; + + *idx = 0; + } +} + ChessInfo* gochess_replay(FILE* fp) { - return NULL; + ChessInfo *info; + int ch; + char userid[2][IDLEN + 1] = { "", "" }; + char sethand_str[4] = ""; + char *recording = NULL; + char *record_end = NULL; + go_step_t step; + + /* for mygetc */ + char buf[512] = ""; + int idx = 0; + +#define GETC() mygetc(fp, buf, &idx, sizeof(buf)) + + /* sgf file started with "(;" */ + if (GETC() != '(' || GETC() != ';') + return NULL; + + /* header info */ + while ((ch = GETC()) != EOF && ch != ';') { + if (ch == '[') { + if (recording) { + while ((ch = GETC()) != EOF && ch != ']') + if (recording < record_end) + *recording++ = ch; + *recording = 0; + recording = NULL; + } else + while ((ch = GETC()) != EOF && ch != ']') + continue; + + if (ch == EOF) + break; + } else if (ch == ';') /* next stage */ + break; + else { + int ch2 = GETC(); + + if (ch2 == EOF) { + ch = EOF; + break; + } + + if (ch == 'P') { + if (ch2 == 'B') { + recording = userid[BBLACK]; + record_end = userid[BBLACK] + IDLEN; + } else if (ch2 == 'W') { + recording = userid[BWHITE]; + record_end = userid[BWHITE] + IDLEN; + } + } else if (ch == 'H') { + if (ch2 == 'A') { + recording = sethand_str; + record_end = sethand_str + sizeof(sethand_str) - 1; + } + } + } + } + + if (ch == EOF) + return NULL; + + info = NewChessInfo(&go_actions, &go_constants, + 0, CHESS_MODE_REPLAY); + + /* filling header information to info */ + if (userid[BBLANK][0]) { + userec_t rec; + if (getuser(userid[BBLANK], &rec)) + go_init_user_userec(&rec, &info->user1); + } + + if (userid[BWHITE][0]) { + userec_t rec; + if (getuser(userid[BWHITE], &rec)) + go_init_user_userec(&rec, &info->user2); + } + + if (sethand_str[0]) { + int sethand = atoi(sethand_str); + if (sethand >= 2 && sethand <= 9) { + step.type = CHESS_STEP_NORMAL; + step.color = SETHAND; + step.loc.r = sethand; + ChessHistoryAppend(info, &step); + } + } + + /* steps, ends with ")" */ + while ((ch = GETC()) != EOF && ch != ')') { + if (ch == ';') + ChessHistoryAppend(info, &step); + else if (ch == 'B') + step.color = BBLACK; + else if (ch == 'W') + step.color = BWHITE; + else if (ch == '[') { + ch = GETC(); + if (ch == EOF) + break; + else if (ch == ']') { + step.type = CHESS_STEP_PASS; + continue; + } else + step.loc.c = ch - 'a'; + + ch = GETC(); + if (ch == EOF) + break; + else if (ch == ']') { + step.type = CHESS_STEP_PASS; + continue; + } else + step.loc.r = ch - 'a'; + + while ((ch = GETC()) != EOF && ch != ']'); + + if (step.loc.r < 0 || step.loc.r >= BRDSIZ || + step.loc.c < 0 || step.loc.c >= BRDSIZ) + step.type = CHESS_STEP_PASS; + else + step.type = CHESS_STEP_NORMAL; + } + } + + info->board = malloc(sizeof(board_t)); + info->tag = malloc(sizeof(go_tag_t)); + + go_init_board(info->board); + go_init_tag(info->tag); + + return info; + +#undef GETC } |