回精華區
作者IQgame.bbs@amtigers.adsldns.org (IQgame), 看板: plan
標題[文件] IQgame 原始碼
時間五虎幫 (Tue Dec 24 00:27:40 2002) Updated: 2005/03/23
: src/game/iq.c 新增此程式

/*-------------------------------------------------------*/
/* iq.c     ( NTHU CS MapleBBS Ver 3.10 )                */
/*-------------------------------------------------------*/
/* target : 數學大富翁                                   */
/* create : 02/12/18                                     */
/* update :   /  /                                       */
/* author : codeman.bbs@amtigers.adsldns.org             */
/*          hxw.bbs@bbs.sayya.org                        */
/*-------------------------------------------------------*/


#include "bbs.h"

#ifdef  HAVE_GAME

/* 題目數(Q_MAX)要大於出題數(Q_MUCH) */

#define Q_LENTH     160         /* 題目長度 */
#define Q_MAX       1024        /* 最多能有幾題 */
#define Q_MUCH      10          /* 出多少題 */
#define Q_TOTAL     total       /* 檔案中共有多少題目 */
#define FULLSCORE   100         /* 定義滿分是 100 分 */

#undef  SHOWRANSER              /* show the right anser 秀出解答 */
                                /* 建議不要,因為這樣就不好玩了 */

/*-------------------------------------------------------*/
/* 題庫格式                                              */
/* 題目長以一個螢幕可輸出為限                            */
/* 範例:                                                 */
/* 1+1=2? y                                              */
/* 2+3=4? n                                              */
/* ^^題目 ^答案                                          */
/* 註:答案是該字串扣掉換行的最後一個字元                 */
/*-------------------------------------------------------*/


#define IQFILE  "etc/iqdata.aq"             /* 定義題庫檔的檔名 */


static int
logo()              /* 標題 */
{
  move(1,0);
  outs("\033[1;33;41m");
  outs("*****************************************************************\n");
  outs("*                            IQgame                             *\n");
  outs("*                         歡迎投稿題目                          *\n");
  outs("*               請po到 telnet://amtigers.adsldns.org            *\n");
  outs("*                           IQgame板                            *\n");
  outs("*****************************************************************");
  outs("\033[m");
}


static int
showans(anser)              /* 把使用者輸入的答案秀出來 */
  char anser[Q_MAX];
{
  int i;

  move(Q_MUCH + 1, 0);
  outs("您的答案是:\n");
  for (i = 0; i < Q_MUCH; i++)
    prints("%d:%c  ", i + 1, anser[i]);
}


#ifdef SHOWRANSER
static int
showrans(ranser, aqnum, question)   /* 秀出正解的函式 */
  char ranser[Q_MAX];
  int aqnum[Q_MUCH];
  char *question[Q_LENTH];
{
  int i;

  vs_bar("數學大富翁");
  for (i = 0; i < Q_MUCH; i++)
    prints("%d.%s :%c\n", i+1, question[aqnum[i]], ranser[aqnum[i]]);
}
#endif


static int          /* 回傳對了幾題 */
anscompare(anser, ranser, aqnum)   /* 比對答案的函式 */
  char anser[Q_MAX];
  char ranser[Q_MAX];
  int aqnum[Q_MUCH];
{
  int i, rmuch;

  rmuch = 0;
  for (i = 0; i < Q_MUCH; i++)
  {
    if (ranser[aqnum[i]] == anser[i])
      rmuch++;
  }
  return rmuch;
}


static int          /* 回傳分數 */
countscore(rmuch)               /* 計算分數的函式 */
  int rmuch;
{
  return rmuch * FULLSCORE / Q_MUCH;
}


static void
startgame(fpath)
  char *fpath;           /* 題庫的檔名 */
{
  FILE *fp;
  char *question[Q_MAX];
  char anser[Q_MAX], ranser[Q_MAX]; /* anser是使用者的答案,ranser是正解 */
  int qnum, total;                  /* total是檔案中的總題目數,qnum是題號 */
  int aqnum[Q_MUCH] = {0};          /* aqnum是己出現過的題號 */
  int alnum[Q_MAX] = {0};           /* already exists num 己出現過的題號 */
  int i;
  char buf[3];

  if (!(fp = fopen(fpath, "r")))
    return;

  logo();

  /* 將 question 的記憶體準備好 */
  for (i = 0; i < Q_MAX; i++)
    question[i] = (char *) malloc(Q_LENTH);

  /* 把題目讀到 question 陣列中 */
  total = 0;
  while (fgets(question[total], Q_LENTH, fp))
  {
    i = strlen(question[total]) - 2;
    ranser[total] = (char) *(question[total] + i);
                                        /* 把倒數第二個字元讀入ranser陣列 */
    *(question[total] + i) = '\0';
    total++;
  }

  fclose(fp);

  /* 亂數取題,列印到螢幕上,並將答案存到anser陣列 */
  srand(time(NULL) + cuser.userno);
  for (i = 0; i < Q_MUCH; i++)
  {
    /* 判斷亂數是否出現過,若有就重取 */
    do
    {
      qnum = rnd(total);
    } while (alnum[qnum] == 1);

    alnum[qnum] = 1;
    move(9, 0);
    clrtobot();
    prints("\033[1m%s\033[m", question[qnum]);
    anser[i] = vget(10, 0, "請輸入答案 : ", buf, 3, LCECHO);
    aqnum[i] = qnum;
  }

  /* 比對ranser和anser答案 */
  i = anscompare(anser, ranser, aqnum);

  /* 計分 */
  move(9, 0);
  clrtobot();
  move(9, 18);
  prints("您答對了 %d 題!", i);
  move(10, 18);
  prints("您得了 %d 分,滿分是 %d 分。", countscore(rmuch), FULLSCORE);
  vmsg(NULL);

#ifdef SHOWRANSER  /* 秀出解答 */
  showrans(ranser, aqnum, question);
  showans(anser);
  vmsg(NULL);
#endif

  for (i = 0; i < Q_MAX; i++)
    free(question[i]);

  return;
}


int
main_iq()
{
  vs_bar("數學大富翁");
  startgame(IQFILE);                    /* 以IQFILE為題庫開始遊戲 */
  return 0;
}
#endif  /* HAVE_GAME */


: src/game/Makefile

SO =    .... iq.so

: src/maple/menu.c

  "bin/tetris.so:main_tetris", 0, - M_GAME,
  "Tetris     ♂ 俄羅斯塊 ♀",

+ "bin/iq.so:main_iq", 0, - M_GAME,
+ "IQgame     ♂ 數學富翁 ♀",


: etc/iqdata.aq

  把題庫檔(iqdata.aq)放在etc下

--
    ╭── Origin ╮ 五虎幫  amtigers.adsldns.org  ~ αβγ ──┼
    ┼     Author ╯ sw59-202-248.adsl.seed.net.tw
精華選讀←離開[主題上]主題下(k)上篇(j)下篇S/a搜尋 G串列 TAB精華 ↑↓捲 Pg/Space翻