From f2ff0a76f97deb1b9a27ff4d9f38b894bbf82287 Mon Sep 17 00:00:00 2001 From: victor Date: Fri, 22 Aug 2003 08:20:54 +0000 Subject: merge from my branch git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@1124 63ad8ddf-47c3-0310-b6dd-a9e9d9715204 --- mbbsd/chc_draw.c | 18 +++---- mbbsd/chc_play.c | 159 +++++++++++++++++++++++++++++++++++-------------------- mbbsd/talk.c | 23 +++++--- mbbsd/var.c | 1 - 4 files changed, 126 insertions(+), 75 deletions(-) (limited to 'mbbsd') diff --git a/mbbsd/chc_draw.c b/mbbsd/chc_draw.c index a3cda935..2cca2f19 100644 --- a/mbbsd/chc_draw.c +++ b/mbbsd/chc_draw.c @@ -1,4 +1,4 @@ -/* $Id: chc_draw.c,v 1.5 2002/12/31 17:40:51 in2 Exp $ */ +/* $Id$ */ #include "bbs.h" #define SIDE_ROW 10 @@ -99,7 +99,7 @@ showstep(board_t board) } void -chc_drawline(board_t board, int line) +chc_drawline(board_t board, chcusr_t *user1, chcusr_t *user2, int line) { int i, j; @@ -107,7 +107,7 @@ chc_drawline(board_t board, int line) clrtoeol(); if (line == 0) { prints("\033[1;46m 象棋對戰 \033[45m%30s VS %-30s\033[m", - cuser.userid, chc_mateid); + user1->userid, user2->userid); } else if (line >= 3 && line <= 21) { outs(" "); for (i = 0; i < 9; i++) { @@ -152,15 +152,15 @@ chc_drawline(board_t board, int line) "\033[1;31m%2d\033[37m勝 " "\033[34m%2d\033[37m敗 " "\033[36m%2d\033[37m和\033[m", - cuser.userid, - cuser.chc_win, cuser.chc_lose - 1, cuser.chc_tie); + user1->userid, + user1->win, user1->lose - 1, user1->tie); } else if (line == HISWIN_ROW) { prints("\033[1;33m%12.12s " "\033[1;31m%2d\033[37m勝 " "\033[34m%2d\033[37m敗 " "\033[36m%2d\033[37m和\033[m", - chc_mateid, - chc_hiswin, chc_hislose - 1, chc_histie); + user2->userid, + user2->win, user2->lose - 1, user2->tie); } } else if (line == 2 || line == 22) { outs(" "); @@ -174,10 +174,10 @@ chc_drawline(board_t board, int line) } void -chc_redraw(board_t board) +chc_redraw(chcusr_t *user1, chcusr_t *user2, board_t board) { int i; for (i = 0; i <= 22; i++) - chc_drawline(board, i); + chc_drawline(board, user1, user2, i); } diff --git a/mbbsd/chc_play.c b/mbbsd/chc_play.c index 08eead20..d6a714cf 100644 --- a/mbbsd/chc_play.c +++ b/mbbsd/chc_play.c @@ -1,6 +1,6 @@ -/* $Id: chc_play.c,v 1.7 2002/11/16 13:41:07 kcwu Exp $ */ +/* $Id$ */ #include "bbs.h" -typedef int (*play_func_t) (int, board_t, board_t); +typedef int (*play_func_t) (int, chcusr_t *, chcusr_t *, board_t, board_t); static int chc_ipass = 0, chc_hepass = 0; @@ -13,8 +13,25 @@ static int chc_ipass = 0, chc_hepass = 0; #define MYWIN_ROW 17 #define HISWIN_ROW 18 +static void +chcusr_put(userec_t *userec, chcusr_t *user) +{ + userec->chc_win = user->win; + userec->chc_lose = user->lose; + userec->chc_tie = user->tie; +} + +static void +chcusr_get(userec_t *userec, chcusr_t *user) +{ + strlcpy(user->userid, userec->userid, sizeof(user->userid)); + user->win = userec->chc_win; + user->lose = userec->chc_lose; + user->tie = userec->chc_tie; +} + static int -hisplay(int s, board_t board, board_t tmpbrd) +hisplay(int s, chcusr_t *user1, chcusr_t *user2, board_t board, board_t tmpbrd) { int start_time; int endgame = 0, endturn = 0; @@ -29,7 +46,7 @@ hisplay(int s, board_t board, board_t tmpbrd) chc_from.r = -2; chc_sendmove(s); } - chc_drawline(board, TIME_ROW); + chc_drawline(board, user1, user2, TIME_ROW); move(1, 0); oflush(); switch (igetkey()) { @@ -53,7 +70,7 @@ hisplay(int s, board_t board, board_t tmpbrd) if (chc_from.r == -1) { chc_hepass = 1; strlcpy(chc_warnmsg, "\033[1;33m要求和局!\033[m", sizeof(chc_warnmsg)); - chc_drawline(board, WARN_ROW); + chc_drawline(board, user1, user2, WARN_ROW); } else { chc_from.r = 9 - chc_from.r, chc_from.c = 8 - chc_from.c; chc_to.r = 9 - chc_to.r, chc_to.c = 8 - chc_to.c; @@ -62,10 +79,10 @@ hisplay(int s, board_t board, board_t tmpbrd) endgame = 2; endturn = 1; chc_hepass = 0; - chc_drawline(board, STEP_ROW); + chc_drawline(board, user1, user2, STEP_ROW); chc_movechess(board); - chc_drawline(board, LTR(chc_from.r)); - chc_drawline(board, LTR(chc_to.r)); + chc_drawline(board, user1, user2, LTR(chc_from.r)); + chc_drawline(board, user1, user2, LTR(chc_to.r)); } } break; @@ -75,7 +92,7 @@ hisplay(int s, board_t board, board_t tmpbrd) } static int -myplay(int s, board_t board, board_t tmpbrd) +myplay(int s, chcusr_t *user1, chcusr_t *user2, board_t board, board_t tmpbrd) { int ch, start_time; int endgame = 0, endturn = 0; @@ -85,7 +102,7 @@ myplay(int s, board_t board, board_t tmpbrd) chc_lefttime = CHC_TIMEOUT - (now - start_time); bell(); while (!endturn) { - chc_drawline(board, TIME_ROW); + chc_drawline(board, user1, user2, TIME_ROW); chc_movecur(chc_cursor.r, chc_cursor.c); oflush(); ch = igetkey(); @@ -131,7 +148,7 @@ myplay(int s, board_t board, board_t tmpbrd) chc_from.r = -1; chc_sendmove(s); strlcpy(chc_warnmsg, "\033[1;33m要求和棋!\033[m", sizeof(chc_warnmsg)); - chc_drawline(board, WARN_ROW); + chc_drawline(board, user1, user2, WARN_ROW); bell(); break; case '\r': @@ -141,7 +158,7 @@ myplay(int s, board_t board, board_t tmpbrd) if (chc_cursor.r == chc_select.r && chc_cursor.c == chc_select.c) { chc_selected = 0; - chc_drawline(board, LTR(chc_cursor.r)); + chc_drawline(board, user1, user2, LTR(chc_cursor.r)); } else if (chc_canmove(board, chc_select, chc_cursor)) { if (CHE_P(board[chc_cursor.r][chc_cursor.c]) == 1) endgame = 1; @@ -152,24 +169,24 @@ myplay(int s, board_t board, board_t tmpbrd) chc_movechess(tmpbrd); } if (endgame || !chc_iskfk(tmpbrd)) { - chc_drawline(board, STEP_ROW); + chc_drawline(board, user1, user2, STEP_ROW); chc_movechess(board); chc_sendmove(s); chc_selected = 0; - chc_drawline(board, LTR(chc_from.r)); - chc_drawline(board, LTR(chc_to.r)); + chc_drawline(board, user1, user2, LTR(chc_from.r)); + chc_drawline(board, user1, user2, LTR(chc_to.r)); endturn = 1; } else { strlcpy(chc_warnmsg, "\033[1;33m不可以王見王\033[m", sizeof(chc_warnmsg)); bell(); - chc_drawline(board, WARN_ROW); + chc_drawline(board, user1, user2, WARN_ROW); } } } else if (board[chc_cursor.r][chc_cursor.c] && CHE_O(board[chc_cursor.r][chc_cursor.c]) == chc_turn) { chc_selected = 1; chc_select = chc_cursor; - chc_drawline(board, LTR(chc_cursor.r)); + chc_drawline(board, user1, user2, LTR(chc_cursor.r)); } break; } @@ -178,91 +195,117 @@ myplay(int s, board_t board, board_t tmpbrd) } static void -mainloop(int s, board_t board) +mainloop(int s, chcusr_t *user1, chcusr_t *user2, board_t board, play_func_t play_func[2]) { int endgame; board_t tmpbrd; - play_func_t play_func[2]; play_func[chc_my] = myplay; - play_func[chc_my ^ 1] = hisplay; + if(s != 0) + play_func[chc_my ^ 1] = hisplay; + else /* 跟自己下棋 */ + play_func[chc_my ^ 1] = myplay; + for (chc_turn = 1, endgame = 0; !endgame; chc_turn ^= 1) { chc_firststep = 0; - chc_drawline(board, TURN_ROW); + chc_drawline(board, user1, user2, TURN_ROW); if (chc_ischeck(board, chc_turn)) { strlcpy(chc_warnmsg, "\033[1;31m將軍!\033[m", sizeof(chc_warnmsg)); bell(); } else chc_warnmsg[0] = 0; - chc_drawline(board, WARN_ROW); - endgame = play_func[chc_turn] (s, board, tmpbrd); + chc_drawline(board, user1, user2, WARN_ROW); + endgame = play_func[chc_turn] (s, user1, user2, board, tmpbrd); } - if (endgame == 1) { - strlcpy(chc_warnmsg, "對方認輸了!", sizeof(chc_warnmsg)); - cuser.chc_win++; - currutmp->chc_win++; - } else if (endgame == 2) { - strlcpy(chc_warnmsg, "你認輸了!", sizeof(chc_warnmsg)); - cuser.chc_lose++; - currutmp->chc_lose++; - } else { - strlcpy(chc_warnmsg, "和棋", sizeof(chc_warnmsg)); - cuser.chc_tie++; - currutmp->chc_tie++; + if (s != 0) { + if (endgame == 1) { + strlcpy(chc_warnmsg, "對方認輸了!", sizeof(chc_warnmsg)); + user1->win++; + currutmp->chc_win++; + } else if (endgame == 2) { + strlcpy(chc_warnmsg, "你認輸了!", sizeof(chc_warnmsg)); + user1->lose++; + currutmp->chc_lose++; + } else { + strlcpy(chc_warnmsg, "和棋", sizeof(chc_warnmsg)); + user1->tie++; + currutmp->chc_tie++; + } + } + else { + strlcpy(chc_warnmsg, "結束打譜", sizeof(chc_warnmsg)); } - cuser.chc_lose--; + user1->lose--; + + // if not watching + chcusr_put(&cuser, user1); passwd_update(usernum, &cuser); - chc_drawline(board, WARN_ROW); + chc_drawline(board, user1, user2, WARN_ROW); bell(); oflush(); } static void -chc_init(int s, board_t board) +chc_init(int s, chcusr_t *user1, chcusr_t *user2, board_t board) { userinfo_t *my = currutmp; setutmpmode(CHC); clear(); chc_warnmsg[0] = 0; - chc_my = my->turn; - chc_mateid = my->mateid; + if(s != 0){ // XXX mateid -> user2 + chc_my = my->turn; + chc_mateid = my->mateid; + } + else{ + chc_my = 1; + chc_mateid = cuser.userid; + } chc_firststep = 1; chc_init_board(board); - chc_redraw(board); + chc_redraw(user1, user2, board); chc_cursor.r = 9, chc_cursor.c = 0; add_io(s, 0); - if (my->turn) + if (my->turn) chc_recvmove(s); - passwd_query(usernum, &xuser); - cuser.chc_win = xuser.chc_win; - cuser.chc_lose = xuser.chc_lose + 1; - cuser.chc_tie = xuser.chc_tie; - cuser.money = xuser.money; - passwd_update(usernum, &cuser); - getuser(chc_mateid); - chc_hiswin = xuser.chc_win; - chc_hislose = xuser.chc_lose; - chc_histie = xuser.chc_tie; + user1->lose++; + // if not watching + passwd_query(usernum, &xuser); + chcusr_put(&xuser, user1); + passwd_update(usernum, &xuser); if (!my->turn) { chc_sendmove(s); - chc_hislose++; + user2->lose++; } - chc_redraw(board); + chc_redraw(user1, user2, board); +} + +static void +chc_userinit(char *userid1, char *userid2, chcusr_t *user1, chcusr_t *user2, play_func_t play_func[2]) +{ + getuser(userid1); + chcusr_get(&xuser, user2); + + getuser(userid2); + chcusr_get(&xuser, user2); } void -chc(int s) +chc(int s, int type) { board_t board; + chcusr_t user1, user2; + play_func_t play_func[2]; - chc_init(s, board); - mainloop(s, board); - close(s); + chc_userinit(cuser.userid, currutmp->mateid, &user1, &user2, play_func); + chc_init(s, &user1, &user2, board); + mainloop(s, &user1, &user2, board, play_func); + if (type == CHC_VERSUS) + close(s); add_io(0, 0); if (chc_my) pressanykey(); diff --git a/mbbsd/talk.c b/mbbsd/talk.c index 8246801b..e94bd9f4 100644 --- a/mbbsd/talk.c +++ b/mbbsd/talk.c @@ -1,4 +1,4 @@ -/* $Id: talk.c,v 1.112 2003/07/19 01:26:33 in2 Exp $ */ +/* $Id$ */ #include "bbs.h" #define QCAST int (*)(const void *, const void *) @@ -1292,7 +1292,7 @@ my_talk(userinfo_t * uin, int fri_stat) gomoku(msgsock); break; case SIG_CHC: - chc(msgsock); + chc(msgsock, CHC_VERSUS); break; case SIG_TALK: default: @@ -1345,6 +1345,12 @@ my_talk(userinfo_t * uin, int fri_stat) pressanykey(); } +static void +self_play(userinfo_t * uin, int fri_stat) +{ + chc(0, CHC_PERSONAL); +} + /* 選單式聊天介面 */ #define US_PICKUP 1234 #define US_RESORT 1233 @@ -2195,14 +2201,17 @@ userlist(void) case 't': if (HAS_PERM(PERM_LOGINOK)) { + move(1, 0); + clrtobot(); + move(3, 0); if (uentp->pid != currpid && strcmp(uentp->userid, cuser.userid) != 0) { - move(1, 0); - clrtobot(); - move(3, 0); my_talk(uentp, fri_stat); - redrawall = redraw = 1; } + else{ + self_play(uentp, fri_stat); + } + redrawall = redraw = 1; } break; case 'K': @@ -2605,7 +2614,7 @@ talkreply(void) gomoku(a); break; case SIG_CHC: - chc(a); + chc(a, CHC_VERSUS); break; case SIG_TALK: default: diff --git a/mbbsd/var.c b/mbbsd/var.c index 9c2c443c..277bef8d 100644 --- a/mbbsd/var.c +++ b/mbbsd/var.c @@ -390,7 +390,6 @@ rc_t chc_from, chc_to, chc_select, chc_cursor; int chc_lefttime; int chc_my, chc_turn, chc_selected, chc_firststep; char chc_warnmsg[64], *chc_mateid; -int chc_hiswin, chc_hislose, chc_histie; /* screen.c */ screenline_t *big_picture = NULL; -- cgit v1.2.3