summaryrefslogtreecommitdiffstats
path: root/mbbsd/chess.c
diff options
context:
space:
mode:
authorscw <scw@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2005-08-28 22:36:54 +0800
committerscw <scw@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2005-08-28 22:36:54 +0800
commit544b6475f029f5396bea9ebca0994111e74d1200 (patch)
treea118f5effae4482def8d881794bf7ea7f676c381 /mbbsd/chess.c
parentbd3cb69722802ca63edbc0d60a8e07f19ab31359 (diff)
downloadpttbbs-544b6475f029f5396bea9ebca0994111e74d1200.tar
pttbbs-544b6475f029f5396bea9ebca0994111e74d1200.tar.gz
pttbbs-544b6475f029f5396bea9ebca0994111e74d1200.tar.bz2
pttbbs-544b6475f029f5396bea9ebca0994111e74d1200.tar.lz
pttbbs-544b6475f029f5396bea9ebca0994111e74d1200.tar.xz
pttbbs-544b6475f029f5396bea9ebca0994111e74d1200.tar.zst
pttbbs-544b6475f029f5396bea9ebca0994111e74d1200.zip
Chess functions:
* chc machine readable log - http://www.elephantbase.net/protocol/cchess_pgn.htm - http://www.elephantbase.net/protocol/cchess_move.htm * chc and gomoku replay work * key binding 'z' in pmore() for chess replay * avoid crash when watching (the watchee's mateid is empty) git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@3105 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'mbbsd/chess.c')
-rw-r--r--mbbsd/chess.c100
1 files changed, 79 insertions, 21 deletions
diff --git a/mbbsd/chess.c b/mbbsd/chess.c
index e131459d..b35d386f 100644
--- a/mbbsd/chess.c
+++ b/mbbsd/chess.c
@@ -32,6 +32,16 @@ static const char * const ChessHintStr[] = {
"Enter ¿ï¾Ü/²¾°Ê"
};
+static const struct {
+ const char* name;
+ int name_len;
+ ChessInfo* (*func)(FILE* fp);
+} ChessReplayMap[] = {
+ { "gomoku", 6, &gomoku_replay },
+ { "chc", 3, &chc_replay },
+ { NULL }
+};
+
static ChessInfo * CurrentPlayingGameInfo;
/* XXX: This is a BAD way to pass information.
@@ -625,6 +635,7 @@ ChessPlayFuncWatch(ChessInfo* info)
sizeof(info->warnmsg));
ChessDrawLine(info, CHESS_DRAWING_WARN_ROW);
+ ChessDrawLine(info, CHESS_DRAWING_STEP_ROW);
move(1, 0);
switch (igetch()) {
@@ -954,7 +965,9 @@ ChessPlay(ChessInfo* info)
}
info->actions->gameend(info, game_result);
- ChessGenLog(info, game_result);
+
+ if (info->mode != CHESS_MODE_REPLAY)
+ ChessGenLog(info, game_result);
currutmp->sig = -1;
Signal(SIGUSR1, old_handler);
@@ -1083,31 +1096,80 @@ ChessWatchGame(void (*play)(int, ChessGameMode), int game, const char* title)
return 0;
}
+int
+ChessReplayGame(const char* fname)
+{
+ ChessInfo *info;
+ FILE *fp = fopen(fname, "r");
+ int found = -1;
+ char buf[256];
+ screen_backup_t oldscreen;
+
+ while (found == -1 && fgets(buf, sizeof(buf), fp)) {
+ if (buf[0] == '<') {
+ const int line_len = strlen(buf);
+ if (strcmp(buf + line_len - 5, "log>\n") == 0) {
+ int i;
+ for (i = 0; ChessReplayMap[i].name; ++i)
+ if (ChessReplayMap[i].name_len == line_len - 6 &&
+ strncmp(buf + 1, ChessReplayMap[i].name,
+ ChessReplayMap[i].name_len) == 0) {
+ found = i;
+ break;
+ }
+ }
+ }
+ }
+
+ if (found == -1) {
+ fclose(fp);
+ return -1;
+ }
+
+ info = ChessReplayMap[found].func(fp);
+ fclose(fp);
+
+ screen_backup(&oldscreen);
+ ChessPlay(info);
+ screen_restore(&oldscreen);
+
+ return 0;
+}
+
static void
ChessInitUser(ChessInfo* info)
{
char userid[2][IDLEN + 1];
const userinfo_t* uinfo;
- if (info->mode == CHESS_MODE_PERSONAL) {
- strlcpy(userid[0], cuser.userid, sizeof(userid[0]));
- strlcpy(userid[1], cuser.userid, sizeof(userid[1]));
- }
- else if (info->mode == CHESS_MODE_WATCH) {
- userinfo_t *uinfo = search_ulist_userid(currutmp->mateid);
- strlcpy(userid[0], uinfo->userid, sizeof(userid[0]));
- strlcpy(userid[1], uinfo->mateid, sizeof(userid[1]));
+ switch (info->mode) {
+ case CHESS_MODE_PERSONAL:
+ strlcpy(userid[0], cuser.userid, sizeof(userid[0]));
+ strlcpy(userid[1], cuser.userid, sizeof(userid[1]));
+ break;
+
+ case CHESS_MODE_WATCH:
+ uinfo = search_ulist_userid(currutmp->mateid);
+ strlcpy(userid[0], uinfo->userid, sizeof(userid[0]));
+ strlcpy(userid[1], uinfo->mateid, sizeof(userid[1]));
+ break;
+
+ case CHESS_MODE_VERSUS:
+ strlcpy(userid[0], cuser.userid, sizeof(userid[0]));
+ strlcpy(userid[1], currutmp->mateid, sizeof(userid[1]));
+ break;
+
+ case CHESS_MODE_REPLAY:
+ return;
}
- else if (info->mode == CHESS_MODE_VERSUS) {
- strlcpy(userid[0], cuser.userid, sizeof(userid[0]));
- strlcpy(userid[1], currutmp->mateid, sizeof(userid[1]));
- } else
- assert_not_reached();
uinfo = search_ulist_userid(userid[0]);
- info->actions->init_user(uinfo, &info->user1);
+ if (uinfo)
+ info->actions->init_user(uinfo, &info->user1);
+
uinfo = search_ulist_userid(userid[1]);
- info->actions->init_user(uinfo, &info->user2);
+ if (uinfo)
+ info->actions->init_user(uinfo, &info->user2);
}
#ifdef CHESSCOUNTRY
@@ -1269,17 +1331,13 @@ ChessInitPlayFunc(ChessInfo* info)
break;
case CHESS_MODE_WATCH:
+ case CHESS_MODE_REPLAY:
info->play_func[0] = info->play_func[1] = &ChessPlayFuncWatch;
break;
case CHESS_MODE_PERSONAL:
info->play_func[0] = info->play_func[1] = &ChessPlayFuncMy;
break;
-
- case CHESS_MODE_REPLAY:
- /* TODO: not implemented yet */
- assert_not_reached();
- break;
}
}