//bat0.c -- prototype by tsaiwn@csie.nctu.edu.tw //BATtle of NUMbers // === 以下是要 讓 do{...}while(x); 可寫成repeat...until(!(x)); #define repeat do { #define until(x) } while(! (x) ) #define bool int //^^^^^^^^^^^^^^^^^^ 因為 C 不認識 bool, 用 int 代替;也可用enum #include #include #include // // define some global variables for the Game int nStone; // number of Stone int maxTake; // max stone to take bool userFirst, playAgain; // 是否 USER 先拿? 是否要繼續玩? // more variable needed? 想到再補上來 // // 先把用到的各小弟 (function; 函數; 函式) 宣告在這 .. void hello( ), prepareGame( ), playGame( ); // 看名稱就知道意思 bool askUserFirst( ), askPlayAgain( ); void userTurn(void), computerTurn(void); // take turn to play // int main( ) { // 把工作都儘量分給 function, 讓 main 看起來很乾爽! hello( ); // 必要時可做 srand(time(0)); //用時間做亂數種子, 就是 randomize repeat prepareGame( ); userFirst = askUserFirst( ); playGame( ); playAgain = askPlayAgain( ); until( ! playAgain ); printf("Thank you for your play.\n"); return 0; } // main void hello( ) { // welcome message and/or game rules } void prepareGame( ) { // 用亂數 rand( )生出 遊戲所需的各個 Global variables } bool askUserFirst( ) { // 若 USER 要先拿, 就傳回 1 return (38==38); // YES / TRUE; 因為 C 不認識 true/false } bool askPlayAgain( ) { // 若 USER 要繼續玩, 就傳回 1 return (38==49); // NO / FALSE; 因為 C 不認識 true/false } void userTurn( ) { // 問 USER 要拿幾個存入 nTake, 但要 check 是否合乎規定! // 然後當然要從 nStone 減去 nTake } void computerTurn( ) { // 先寫隨便拿但要合乎規定, 再想出可贏就會贏的策略 (很簡單 :-) } void playGame( ) { // user and the computer take Turn until no more stones left // if(userFirst) userTurn( ); // Loop until no more stone // computerTurn( ); // if no more stone then leave the Loop; // userTurn( ); // end Loop // Judge the game result and print some message.. // 如何判斷誰輸誰贏? 須知道誰拿走最後一個! 拿最後一個是輸還是贏? } // playGame