<HTML>
 <head><meta http-equiv="Content-Type"
 content="text/html; charset=big5" /></head>

<BODY bgcolor=#bbffee> <b><font size=4>
<font color=blue size=4><b><pre>
     1	//bat6ok.java -- modified from bat6ok.c
     2	//javac bat6ok.java
     3	//java bat6ok
     4	///////////////////////////////////////////////////
     5	//bat6ok.c  -- @CopyLeft by tsaiwn@csie.nctu.edu.tw
     6	//BATtle of NUMbers
     7	//modified by __Ǹ__  _m_W_  and  __Ǹ__  _m_W_ 
     8	///
     9	// Y Java h class ]_; classWٶPɮצW٬ۦP !
    10	// B main  public static void main(String xxx[ ]) {
 <font color=red>
    11	import java.io.*;
    12	import java.util.*;
    13	
    14	class bat6ok {   //  filename should be the same as class name
    15	
    16	 public final static int MAX_STONE = 31;
    17	 public final static int MIN_STONE = 18;
    18	 public final static int MAX_TAKE = 9;
    19	
    20	 // define some global variables for the Game
    21	  int nStone; // number of Stone
    22	  int maxTake; // max stone to take for some game
    23	
    24	  boolean lastToWin;  // Win or Lose if took last one stone?
    25	  boolean userFirst, playAgain;
    26	  /// 
    27	enum player {USER, COMPUTER};  // o˼g᭱iHUSER/COMPUTER
    28	 player who;  // who is taking the stone? ? `N Java gk !
    29	
    30	public static void main(String xxx[  ]) {
    31	    bat6ok ggyy = new bat6ok( );
    32	    ggyy.main( );
    33	}
    34	bat6ok( ) {   // constructor
    35	     prepareIO( );
    36	}
    37	BufferedReader cin=null;
    38	PrintStream cout=null;
    39	
    40	void prepareIO( ) {
    41	     try {
    42	        cin =  new BufferedReader(
    43	                     new InputStreamReader( System.in )  );
    44	        cout = System.out;
    45	     }catch(Exception e) {  }
    46	}
    47	
    </font>
    48	int main( ) { // Ӫ main( ); Note that it has different prototype
    49	   hello( ); 
    50	   randomize( );  // randomize; make it true random
    51	   do {
    52	      prepareGame( );
    53	      userFirst = askUserFirst( );
    54	      playGame( );
    55	      playAgain = askPlayAgain( );
    56	   } while( playAgain );
    57	   cout.printf("Thank you for your play.\r\n");
    58	   return 0;
    59	}
    60	
    61	int rand( ) {
    62	     return (int) (32767* Math.random( ) );
    63	}
    64	void srand(int seed) {
    65	} // not necessary :-)
    66	void randomize( ) {
    67	     srand((int)System.currentTimeMillis( ) );
    68	}
    69	
    70	void hello( ) {  
    71	   cout.printf("Welcome to the BATNUM game ...\r\n");
    72	}
    73	
    74	void prepareGame( ) {
    75	   String msg[ ]={"Lose the game.", "win the game."};
    76	   nStone = MIN_STONE + rand( ) % (MAX_STONE-MIN_STONE+1);
    77	   maxTake = 3 + rand( ) % (MAX_TAKE -3 +1);  // 3..
    78	   lastToWin = rand( )%2 == 1;  // half chance @b|
    79	   cout.printf("\r\nThere are %d stones.\r\n", nStone);
    80	   cout.printf("At least take 1, at most %d stones.\r\n", maxTake);
    81	   cout.printf("The one who take the last stone will ");
    82	   int kkk;   kkk = 0;
    83	   if(lastToWin) kkk = 1;
    84	   cout.printf("%s\r\n", msg[kkk]); // ̫@ӿ٬OĹ?
    85	} // prepareGame
    86	
    87	boolean askUserFirst( ) {
    88	    cout.printf( "Do you want to go first(Y, N)? ");
    89	    String buf = null;
    90	    try {
    91	          buf = cin.readLine( );
    92	    } catch(Exception e) { }
    93	    if(buf == null) return false;
    94	    if(buf.length( )==0) buf = "n";
    95	    if(buf.charAt(0)=='N' || buf.charAt(0)=='n') return false; //no
    96	    if(buf.charAt(0)==0) return false;  // no! 
    97	    return true;  //  Yes otherwise
    98	}
    99	
   100	boolean askPlayAgain( ) {
   101	    cout.printf( "Do you want to go first(Y, N)? ");
   102	    String buf = null;
   103	    try {
   104	          buf = cin.readLine( );
   105	    } catch(Exception e) { }
   106	    if(buf == null) return false;
   107	    if(buf.length( )==0) buf = "n";
   108	    if(buf.charAt(0)=='N' || buf.charAt(0)=='n') return false; //no
   109	    if(buf.charAt(0)==0) return false;  // no! 
   110	    return true;  // Yes
   111	} // askPlayAgain
   112	
   113	void playGame( ) {
   114	   boolean userWin=true; // assume user win
   115	   if(userFirst) userTurn( );
   116	   else cout.printf(" OK. I go first.\r\n");
   117	   while(nStone > 0 ) {
   118	        computerTurn( );
   119	        if(nStone &lt;= 0) break; // gameOver
   120	        userTurn( );
   121	   } // game Over when leave the while Loop
   122	   userWin = true; // assume you win
   123	   if(lastToWin &amp;&amp; (who == player.COMPUTER) ) userWin = false; // user !
   124	   if( (!lastToWin) &amp;&amp; (who == player.USER) ) userWin = false; // user ]O
   125	   if(userWin) cout.printf(" Congratulations! You win!\r\n");
   126	   else cout.printf(" Sorry, I won! Ha ha!\n");
   127	} // playGame
   128	
   129	void userTurn( ) { 
   130	   int nTake, canTake;
   131	   String buf;
   132	   who = player.USER;  // now USER 's turn  O USER
   133	   canTake = maxTake;  // ӳWw̦huள maxTake
   134	   if(nStone &lt; maxTake) canTake = nStone;  // ShF
   135	   cout.printf(" How many you want to take(1..%d)? ", canTake);
   136	   try {
   137	        buf = cin.readLine( );
   138	        if(buf==null) buf="0";
   139	        nTake = Integer.parseInt(buf);
   140	   }catch(Exception e) { nTake = 0;  }
   141	   while(nTake &lt; 1 || nTake > canTake) { // illegal
   142	      cout.printf("  ?? Error, please re-type(1..%d)? ", canTake);
   143	      try {
   144	           buf = cin.readLine( );
   145	           if(buf==null) buf="0";
   146	           if(buf.length( )==0) buf = "0";
   147	           nTake = Integer.parseInt(buf);
   148	      }catch(Exception e) {  nTake = 0; }
   149	   }
   150	   nStone -= nTake;
   151	   if(nStone == 0) cout.printf(" You just took the last stone.\r\n");
   152	   else cout.printf(" %d stone(s) left.\r\n", nStone);
   153	   return;
   154	} // userTurn
   155	
   156	void computerTurn( ) {
   157	   int nTake = 1;
   158	   who = player.COMPUTER;  // now is COMPUTER 's turn O COMPUTER
   159	   // Ĺ?  Ҽ{̫@Ĺ٬O
   160	   nTake = nStone;
   161	   if(!lastToWin) nTake--;  // ̫@ӬO飮, d@ӵ
   162	   nTake = nTake % (1+maxTake);
   163	   if(nTake == 0) nTake = 1; // q|, ֮@ӵ⮳:-)
   164	   if(nTake > nStone) nTake = nStone; // Sh! (io)
   165	  ///
   166	   nStone -= nTake;  // 
   167	   cout.printf(" I take %d stone", nTake);
   168	   if(nTake > 1) cout.printf("s");  // Ƽ
   169	   cout.printf(" this time.");
   170	   if(nStone == 0) cout.printf(" I just took the last stone.\r\n");
   171	   else cout.printf(" Now %d stone(s) left.\r\n", nStone);
   172	} // computerTurn
   173	
   174	} // class bat6ok
   175	///   === === END of the program BATNUM === ===

</pre>
<hr size=3 color=red>
<font color=red size=5>
oO쥻 C  bat6ok.c
 </font>
<b><font color=blue size=4>
<pre>
   1 //bat6ok.c  -- @CopyLeft by tsaiwn@csie.nctu.edu.tw
   2 //BATtle of NUMbers
   3 //modified by __Ǹ__  _m_W_  and  __Ǹ__  _m_W_ 
   4 ///
   5 // Y Java h class ]_; classWٶPɮצW٬ۦP !
   6 // B main  public static void main(String xxx[ ]) {
   7 #define repeat  do {
   8 #define until(x)   } while(! (x) )
   9 
  10 #include &lt;stdio.h>
  11 #include &lt;stdlib.h>
  12 #include &lt;time.h>
  13 
  14 #define MAX_STONE 31
  15 #define MIN_STONE 18
  16 #define MAX_TAKE 9
  17 //Java HWγo public final static int MAX_TAKE = 9;
  18 
  19 #ifndef __cplusplus
  20   typedef enum {false=0, true } bool; // C { o
  21 #endif
  22 
  23 // define some global variables for the Game
  24 int nStone; // number of Stone
  25 int maxTake; // max stone to take for some game
  26 
  27 bool lastToWin;  // Win or Lose if took last one stone?
  28 bool userFirst, playAgain;
  29 /// 
  30 enum player {USER, COMPUTER};  // o˼g᭱iHUSER/COMPUTER
  31 enum player who; // who is taking the stone? ?
  32   // Java  enum Ьݥɮ׳̫᪺ɥR ! 
  33 void hello( ), prepareGame( ), playGame( );
  34 bool askUserFirst( ), askPlayAgain( );
  35 void userTurn( ), computerTurn( );
  36 void randomize( ) { srand(time(0));  } // ɶӷ@üƺؤl
  37 
  38 int main( ) {
  39    hello( ); 
  40    randomize( );  // randomize; make it true random
  41    repeat
  42       prepareGame( );
  43       userFirst = askUserFirst( );
  44       playGame( );
  45       playAgain = askPlayAgain( );
  46    until( ! playAgain );
  47    printf("Thank you for your play.\r\n");
  48    return 0;
  49 }
  50 
  51 void hello( ) {  
  52    printf("Welcome to the BATNUM game ...\r\n");
  53 }
  54 
  55 void prepareGame( ) {
  56    char msg[ ][88]={"Lose the game.", "win the game."};
  57    nStone = MIN_STONE + rand( ) % (MAX_STONE-MIN_STONE+1);
  58    maxTake = 3 + rand( ) % (MAX_TAKE -3 +1);  // 3..
  59    lastToWin = rand( )%2 == 1;  // half chance @b|
  60    printf("\r\nThere are %d stones.\r\n", nStone);
  61    printf("At least take 1, at most %d stones.\r\n", maxTake);
  62    printf("The one who take the last stone will ");
  63    printf("%s\r\n", msg[lastToWin]); // ̫@ӿ٬OĹ?
  64 #ifdef DEBUG
  65    printf("lastToWin=%d\n", lastToWin);
  66 #endif
  67 } // prepareGame
  68 
  69 bool askUserFirst( ) {
  70    static char buf[99];
  71    fprintf(stderr, "Do you want to go first(Y, N)? ");
  72    fgets(buf, sizeof(buf), stdin);
  73    if(buf[0]=='N' || buf[0]=='n') return false; //no
  74    if(buf[0]==0) return false;  // no!  C{ true/false
  75    return true;  //  Yes otherwise
  76 }
  77 
  78 bool askPlayAgain( ) {
  79    static char buf[99];
  80    fprintf(stderr, " Play again(Y, N)? ");
  81    fgets(buf, sizeof(buf), stdin);
  82    if(buf[0]=='N' || buf[0]=='n') return false; //no
  83    if(feof(stdin)) return false;  // EOF encounted
  84    return true;  // Yes
  85 } // askPlayAgain
  86 
  87 void playGame( ) {
  88    bool userWin=true; // assume user win
  89    if(userFirst) userTurn( );
  90    else printf(" OK. I go first.\r\n");
  91    while(nStone > 0 ) {
  92       computerTurn( );
  93       if(nStone &lt;= 0) break; // gameOver
  94       userTurn( );
  95    } // game Over when leave the while Loop
  96    userWin = true; // assume you win
  97    if(lastToWin &amp;&amp; (who == COMPUTER) ) userWin = false; // user !
  98    if( (!lastToWin) &amp;&amp; (who == USER) ) userWin = false; // user ]O
  99    if(userWin) printf(" Congratulations! You win!\r\n");
 100    else printf(" Sorry, I won! Ha ha!\n");
 101 } // playGame
 102 
 103 void userTurn( ) { 
 104    int nTake, canTake;
 105    static char buf[99];
 106    who = USER;  // now USER 's turn  O USER
 107    canTake = maxTake;  // ӳWw̦huள maxTake
 108    if(nStone &lt; maxTake) canTake = nStone;  // ShF
 109    printf(" How many you want to take(1..%d)? ", canTake);
 110    fgets(buf, sizeof(buf), stdin);
 111    nTake = (int)atol(buf);  
 112    // check if the nTake is legal ?
 113    // .. YųWwhnD USER sJ
 114    // .. ]igYJtƪ give up, N game :-(
 115    while(nTake &lt; 1 || nTake > canTake) { // illegal
 116       printf("  ?? Error, please re-type(1..%d)? ", canTake);
 117       fgets(buf, sizeof(buf), stdin);
 118       nTake = (int)atol(buf);  
 119    }
 120    nStone -= nTake;
 121    if(nStone == 0) printf(" You just took the last stone.\r\n");
 122    else printf(" %d stone(s) left.\r\n", nStone);
 123    return;
 124 } // userTurn
 125 
 126 void computerTurn( ) {
 127    int nTake = 1;
 128    who = COMPUTER;  // now is COMPUTER 's turn O COMPUTER
 129    // Ĺ?  Ҽ{̫@Ĺ٬O
 130    nTake = nStone;
 131    if(!lastToWin) nTake--;  // ̫@ӬO飮, d@ӵ
 132    nTake = nTake % (1+maxTake);
 133    if(nTake == 0) nTake = 1; // q|, ֮@ӵ⮳:-)
 134    if(nTake > nStone) nTake = nStone; // Sh! (io)
 135   ///
 136    nStone -= nTake;  // 
 137    printf(" I take %d stone", nTake);
 138    if(nTake > 1) printf("s");  // Ƽ
 139    printf(" this time.");
 140    if(nStone == 0) printf(" I just took the last stone.\r\n");
 141    else printf(" Now %d stone(s) left.\r\n", nStone);
 142 } // computerTurn
 143 ///   === === END of the program BATNUM === ===
 144 /***  Java  enum: b C/C++  enum O, i Java O!
 145   Java  enum O|ܦ class ! нsĶݬݥؿhX class !
 146   HUO²d, isĶåB:
 147   //gg.java  -- test enum in Java, by tsaiwn@csie.nctu.edu.tw
 148   //avac gg.java
 149   //java gg
 150   //javap gg$player
 151   class gg {
 152        enum player {USER, COMPUTER}; 
 153     public static void main(String xx[ ]) {
 154        player yy = player.USER;
 155        System.out.println("user="+yy);
 156        if(yy == player.USER) System.out.println("YES yy is player.USER");
 157        else System.out.println("NO hahaha!");
 158     }//main
 159   } // class gg
 160 *************************************************************/

</pre>
<hr color=red>
You are the
<img src="http://counter.nsysu.edu.tw/Count.cgi?dd=1">
-th visitors to this page.

<hr size=3>
</pre>
<font color=blue size=5>
<a href="./">
^쥻@~(@~|)ؿ
</a>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
<a href="../">
^@~ؿ
</a>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
<a href="../../">
^ҵ{ؿ
</a><br>


