diff options
Diffstat (limited to 'mbbsd')
-rw-r--r-- | mbbsd/card.c | 208 | ||||
-rw-r--r-- | mbbsd/guess.c | 171 |
2 files changed, 157 insertions, 222 deletions
diff --git a/mbbsd/card.c b/mbbsd/card.c index b2c3300b..b2de67ec 100644 --- a/mbbsd/card.c +++ b/mbbsd/card.c @@ -1,9 +1,72 @@ /* $Id$ */ #include "bbs.h" +////////////////////////////////////////////////////// +// +// we don't use real user money anymore, because there +// are too many bots... +// +// if you want to associate card games with real money, +// override the functions below. +// +////////////////////////////////////////////////////// + +static int card_money = 0; + +int card_reset_money() +{ + card_money = 100; // initialize + // alert user for the new money change + clear(); + move(t_lines/2-2, 0); + prints( " 請注意: 本遊樂場目前已改為使用與 BBS 系統金錢無關的籌碼,\n\n" + " 每場遊戲開始您都會重新獲得 %u 枚籌碼,並在結算後重置。\n" + , card_money); + vmsg(NULL); + return card_money; +} + +int card_get_money() +{ + return card_money; +} + +int card_add_money(int delta) +{ + if (card_money + delta < 0) + return (card_money = 0); + card_money += delta; + return card_money; +} + +void card_end_game() +{ + clear(); + vs_hdr("遊樂場"); + if (card_get_money()) + { + prints("\n遊戲結束,您目前手上的籌碼共有 " + ANSI_COLOR(1;33) "%u" ANSI_RESET " 枚。\n" + "感謝您的光臨!\n", + card_get_money()); + } else { + prints("\n抱歉,您已經輸光了。\n"); + } + vmsg(NULL); +} + +void card_anti_bot_sleep() +{ + // sleep 1 second + sleep(1); +} + +////////////////////////////////////////////////////// + enum CardSuit { Spade, Heart, Diamond, Club }; + static int card_remain(int cards[]) { @@ -359,10 +422,12 @@ card_99(void) static int game_log(int type, int money) { - FILE *fp; - if (money > 0) - demoney(money); + card_add_money(money); + + // if the money is not real user money, no need to log anymore. +#if 0 + FILE *fp; switch (type) { case JACK: @@ -380,7 +445,8 @@ game_log(int type, int money) fclose(fp); break; } - usleep(2*1000000); // sleep 2s +#endif + return 0; } @@ -390,10 +456,9 @@ card_double_ask(void) char buf[100], buf2[3]; snprintf(buf, sizeof(buf), - "[ %s ]您現在共有 %d 元, 現在要分組(加收 %d 元)嗎? [y/N]", - cuser.userid, cuser.money, JACK); - reload_money(); - if (cuser.money < JACK) + "[ %s ]您現在共有 %d 枚籌碼, 現在要分組(加收 %d 枚)嗎? [y/N]", + cuser.userid, card_get_money(), JACK); + if (card_get_money() < JACK) return 0; getdata(20, 0, buf, buf2, sizeof(buf2), LCECHO); if (buf2[0] == 'y' || buf2[0] == 'Y') @@ -406,8 +471,8 @@ card_ask(void) { char buf[100], buf2[3]; - snprintf(buf, sizeof(buf), "[ %s ]您現在共有 %d 元, 還要加牌嗎? [y/N]", - cuser.userid, cuser.money); + snprintf(buf, sizeof(buf), "[ %s ]您現在共有 %d 枚籌碼, 還要加牌嗎? [y/N]", + cuser.userid, card_get_money()); getdata(20, 0, buf, buf2, sizeof(buf2), LCECHO); if (buf2[0] == 'y' || buf2[0] == 'Y') return 1; @@ -474,11 +539,11 @@ card_jack(int *db) c[0]=1; card_show(6, cpu, c, me, m); game_log(JACK, JACK); - vmsgf("你跟電腦都拿到黑傑克, 退還 %d 元", JACK); + vmsgf("你跟電腦都拿到黑傑克, 退還 %d 枚籌碼", JACK); return 0; } game_log(JACK, JACK * 5/2); - vmsgf("很不錯唷! (黑傑克!! 加 %d 元)", JACK * 5/2); + vmsgf("很不錯唷! (黑傑克!! 加 %d 枚籌碼)", JACK * 5/2); return 0; } else if(card_isblackjack(cpu[0],cpu[1])) { c[0] = 1; @@ -510,7 +575,7 @@ card_jack(int *db) } if (i == 6) { /* 畫面只能擺六張牌, 因此直接算玩家贏. 黑傑克實際上沒這規則 */ game_log(JACK, JACK * 10); - vmsgf("好厲害唷! 六張牌還沒爆! 加 %d 元!", 5 * JACK); + vmsgf("好厲害唷! 六張牌還沒爆! 加 %d 枚籌碼!", 5 * JACK); return 0; } @@ -522,7 +587,7 @@ card_jack(int *db) if (card_alls_lower(cpu) > 21) { card_show(6, cpu, c, me, m); game_log(JACK, JACK * 2); - vmsgf("呵呵...電腦爆掉了! 你贏了! 可得 %d 元", JACK * 2); + vmsgf("呵呵...電腦爆掉了! 你贏了! 可得 %d 枚籌碼", JACK * 2); return 0; } j++; @@ -530,12 +595,12 @@ card_jack(int *db) card_show(6, cpu, c, me, m); if(card_alls_upper(cpu)==card_alls_upper(me)) { game_log(JACK, JACK); - vmsgf("平局,退回 %d 元!", JACK); + vmsgf("平局,退回 %d 枚籌碼!", JACK); return 0; } if(card_alls_upper(cpu)<card_alls_upper(me)) { game_log(JACK, JACK * 2); - vmsgf("呵呵...電腦比較小! 你贏了! 可得 %d 元", JACK * 2); + vmsgf("呵呵...電腦比較小! 你贏了! 可得 %d 枚籌碼", JACK * 2); return 0; } game_log(JACK, 0); @@ -543,43 +608,6 @@ card_jack(int *db) return 0; } -int -g_card_jack(void) -{ - int db; - char buf[3]; - // int times = 0; - - setutmpmode(JACK_CARD); - while (1) { - reload_money(); - if (cuser.money < PMONEY) { - outs("您的錢不夠唷!去多發表些有意義的文章再來~~~"); - return 0; - } -#if 0 // should we also notify this? - if (times++ % 5 == 0) - { - move(b_lines-2, 0); clrtoeol(); - outs(ANSI_COLOR(1;31) - "警告: 本遊戲由 PttGames 看板評鑑為黑店,請小心!" ANSI_RESET); - } -#endif - getdata(b_lines - 1, 0, "確定要玩黑傑克嗎 一次十元唷?(Y/N)?[N]", - buf, 3, LCECHO); - if ((*buf != 'y') && (*buf != 'Y')) - break; - else { - db = -1; - vice(PMONEY, "黑傑克"); - do { - card_jack(&db); - } while(db>=0); - } - } - return 0; -} - static int card_all(int all[]) { @@ -623,7 +651,7 @@ ten_helf(void) } if (i == 5) { /* 過五關 */ game_log(TEN_HALF, PMONEY * 5); - vmsgf("好厲害唷! 過五關嘍! 加 %d 元!", 5 * PMONEY); + vmsgf("好厲害唷! 過五關嘍! 加 %d 枚籌碼!", 5 * PMONEY); return 0; } j = 1; @@ -635,7 +663,7 @@ ten_helf(void) if (card_all(cpu) > 21) { card_show(5, cpu, c, me, m); game_log(TEN_HALF, PMONEY * 2); - vmsgf("呵呵...電腦爆掉了! 你贏了! 可得 %d 元", PMONEY * 2); + vmsgf("呵呵...電腦爆掉了! 你贏了! 可得 %d 枚籌碼", PMONEY * 2); return 0; } j++; @@ -646,36 +674,70 @@ ten_helf(void) return 0; } +// Main Game Loops + +int +g_card_jack(void) +{ + int db; + char buf[3]; + + setutmpmode(JACK_CARD); + card_reset_money(); + + buf[0] = 'y'; + while (1) { + + if (buf[0] == 'n') + { + card_end_game(); + break; + } + + db = -1; + card_add_money(-PMONEY); + do { + card_jack(&db); + } while(db>=0); + + if (card_get_money() >= PMONEY) + { + getdata(b_lines - 1, 0, "再玩一局? (Y/n) [Y]", + buf, 3, LCECHO); + card_anti_bot_sleep(); + } else + buf[0] = 'n'; + } + return 0; +} + int g_ten_helf(void) { char buf[3]; - int times = 0; setutmpmode(TENHALF); + card_reset_money(); + + buf[0] = 'y'; while (1) { - reload_money(); - if (cuser.money < PMONEY) { - outs("您的錢不夠唷!去多發表些有意義的文章再來~~~"); - return 0; - } - if (times++ == 0) + if (buf[0] == 'n') { - move(b_lines-2, 0); clrtoeol(); - outs(ANSI_COLOR(1;31) - "警告: 本遊戲由 PttGames 看板評鑑為極度黑店,請小心!" ANSI_RESET); + card_end_game(); + break; } - getdata(b_lines - 1, 0, - ANSI_COLOR(1;37) "確定要玩十點半嗎 一次十元唷?(Y/N)?[N]" ANSI_RESET, + card_add_money(-PMONEY); + ten_helf(); + + if (card_get_money() >= PMONEY) + { + getdata(b_lines - 1, 0, "再玩一局? (Y/n) [Y]", buf, 3, LCECHO); - if (buf[0] != 'y' && buf[0] != 'Y') - return 0; - else { - vice(PMONEY, "十點半"); - ten_helf(); - } + card_anti_bot_sleep(); + } else + buf[0] = 'n'; } return 0; } diff --git a/mbbsd/guess.c b/mbbsd/guess.c index c5408b88..dea421b5 100644 --- a/mbbsd/guess.c +++ b/mbbsd/guess.c @@ -2,61 +2,6 @@ #include "bbs.h" #define LOGPASS BBSHOME "/etc/winguess.log" -static void -show_table(char TABLE[], char ifcomputer) -{ - int i; - - move(0, 35); - outs(ANSI_COLOR(1;44;33) " 【 猜數字 】 " ANSI_RESET); - move(8, 1); - outs(ANSI_COLOR(1;44;36) "目 前 倍 率" ANSI_RESET "\n"); - outs(ANSI_COLOR(1;33) "=================" ANSI_RESET "\n"); - if (ifcomputer) { - outs("贏電腦: 2 倍\n"); - outs("輸電腦: 0 倍\n"); - } else { - for (i = 1; i <= 6; i++) - prints("第%d次, %02d倍\n", i, TABLE[i]); - } - outs(ANSI_COLOR(33) "=================" ANSI_RESET); -} - -static int -get_money(void) -{ - int money, i; - char data[20]; - - move(1, 0); - prints("您目前有:%d " MONEYNAME "$", cuser.money); - do { - getdata(2, 0, "要賭多少(5-10或按q離開): ", data, 9, LCECHO); - money = 0; - if (data[0] == 'q' || data[0] == 'Q') { - unlockutmpmode(); - return 0; - } - for (i = 0; data[i]; i++) - if (data[i] < '0' || data[i] > '9') { - money = -1; - break; - } - if (money != -1) { - money = atoi(data); - reload_money(); - if (money > cuser.money || money <= 4 || money > 10 || - money < 1) - money = -1; - } - } while (money == -1); - move(1, 0); - clrtoeol(); - reload_money(); - prints("您目前有:%d " MONEYNAME "$", cuser.money - money); - return money; -} - static int check_data(const char *str) { @@ -209,53 +154,34 @@ Diff_Random(char *answer) answer[4] = 0; } -#define lockreturn0(unmode, state) if(lockutmpmode(unmode, state)) return 0 - int guess_main(void) { char data[5]; - int money; char computerwin = 0, youwin = 0; int count = 0, c_count = 0; char ifcomputer[2]; char answer[5]; - int *n = NULL; char yournum[5]; - char *flag = NULL; - char TABLE[] = {0, 10, 8, 4, 2, 1, 0, 0, 0, 0, 0}; - FILE *file; + const int max_guess = 10; + + // these variables are not very huge, no need to use malloc + // to prevent heap allocation. + char flag[10000]; + int n[1500]; + setutmpmode(GUESSNUM); clear(); showtitle("猜數字", BBSName); - lockreturn0(GUESSNUM, LOCK_MULTI); - - reload_money(); - if (cuser.money < 5) { - clear(); - move(12, 35); - unlockutmpmode(); - vmsg("錢不夠啦 至少要 5 " MONEYNAME "$"); - return 1; - } - if ((money = get_money()) == 0) - return 1; - vice(money, "猜數字"); Diff_Random(answer); move(2, 0); clrtoeol(); - prints("您下注 :%d " MONEYNAME "$", money); - getdata_str(4, 0, "您要和電腦比賽嗎? <y/n>[y]:", - ifcomputer, sizeof(ifcomputer), LCECHO, "y"); - if (ifcomputer[0] == 'y') { - ifcomputer[0] = 1; - show_table(TABLE, 1); - } else { - ifcomputer[0] = 0; - show_table(TABLE, 0); - } + getdata(4, 0, "您要和電腦比賽嗎? <Y/n>[y]:", + ifcomputer, sizeof(ifcomputer), LCECHO); + *ifcomputer = (*ifcomputer == 'n') ? 0 : 1; + if (ifcomputer[0]) { do { getdata(5, 0, "請輸入您要讓電腦猜的數字: ", @@ -263,107 +189,54 @@ guess_main(void) } while (!legal(atoi(yournum))); move(8, 25); outs("電腦猜"); - flag = malloc(sizeof(char) * 10000); - n = malloc(sizeof(int) * 1500); initcomputer(flag); } move(8, 55); outs("你猜"); - while (((!computerwin || !youwin) && count < 10 && (ifcomputer[0])) || - (!ifcomputer[0] && count < 10 && !youwin)) { + while (((!computerwin || !youwin) && count < max_guess && (ifcomputer[0])) + || (!ifcomputer[0] && count < max_guess && !youwin)) { if (!computerwin && ifcomputer[0]) { ++c_count; if (computer(atoi(yournum), c_count, flag, n)) computerwin = 1; } move(20, 55); - prints("第 %d 次機會 ", count + 1); + prints("第 %d/%d 次機會 ", count + 1, max_guess); if (!youwin) { ++count; if (guess_play(get_data(data, count), answer, count)) youwin = 1; } } - move(17, 35); - free(flag); - free(n); + move(17, 33); if (ifcomputer[0]) { if (count > c_count) { - outs("你輸給電腦了"); - move(18, 35); - prints("你賠了 %d ", money); - if ((file = fopen(LOGPASS, "a"))) { - fprintf(file, "電腦第%d次猜中, ", c_count); - if (youwin) - fprintf(file, "%s 第%d次猜中, ", cuser.userid, count); - else - fprintf(file, "%s 沒猜中, ", cuser.userid); - fprintf(file, "電腦賺走了%s %d " MONEYNAME "$\n", cuser.userid, money); - fclose(file); - } + outs(" 你輸給電腦了"); } else if (count < c_count) { - outs("真厲害, 讓你賺到囉"); - move(18, 35); - prints("你賺走了 %d ", money * 2); - demoney(money * 2); - if ((file = fopen(LOGPASS, "a"))) { - fprintf(file, "id: %s, 第%d次猜中, 電腦第%d次猜中, " - "贏了電腦 %d " MONEYNAME "$\n", cuser.userid, count, - c_count, money * 2); - fclose(file); - } + outs("真厲害, 讓你猜到囉"); } else { - prints("真厲害, 和電腦打成平手了, 拿回本錢%d\n", money); - demoney(money); - if ((file = fopen(LOGPASS, "a"))) { - fprintf(file, "id: %s 和電腦打成了平手\n", cuser.userid); - fclose(file); - } + prints("真厲害, 和電腦打成平手了"); } - unlockutmpmode(); pressanykey(); return 1; } if (youwin) { - demoney(TABLE[count] * money); if (count < 5) { - outs("真厲害, 錢被你賺走了"); - if ((file = fopen(LOGPASS, "a"))) { - fprintf(file, "id: %s, 第%d次猜中, 贏了 %d " MONEYNAME "$\n", - cuser.userid, count, TABLE[count] * money); - fclose(file); - } + outs("真厲害!"); } else if (count > 5) { outs("唉, 太多次才猜出來了"); - if ((file = fopen(LOGPASS, "a"))) { - fprintf(file, "id: %s, 第%d次才猜中, 賠了 %d " MONEYNAME "$\n", - cuser.userid, count, money); - fclose(file); - } } else { - outs("五次猜出來, 還你本錢吧"); + outs("五次猜出來, 還可以~"); move(18, 35); clrtoeol(); - prints("你拿回了%d " MONEYNAME "$\n", money); - if ((file = fopen(LOGPASS, "a"))) { - fprintf(file, "id: %s, 第%d次猜中, 拿回了本錢 %d " MONEYNAME "$\n", - cuser.userid, count, money); - fclose(file); - } } - unlockutmpmode(); pressanykey(); return 1; } - move(17, 35); + move(17, 32); prints("嘿嘿 標準答案是 %s ", answer); - move(18, 35); + move(18, 32); outs("下次再來吧"); - if ((file = fopen(BBSHOME "/etc/loseguess.log", "a"))) { - fprintf(file, "id: %s 賭了 %d " MONEYNAME "$\n", cuser.userid, money); - fclose(file); - } - unlockutmpmode(); pressanykey(); return 1; } |