//bat6ok.java -- modified from bat6ok.c
//javac bat6ok.java
//java bat6ok
///////////////////////////////////////////////////
//bat6ok.c  -- @CopyLeft by tsaiwn@csie.nctu.edu.tw
//BATtle of NUMbers
//modified by __學號__  _姓_名_  and  __學號__  _姓_名_ 
///
// 若用 Java 則須用 class 包起來; class名稱須與檔案名稱相同 !
// 且 main 必須 public static void main(String xxx[ ]) {
import java.io.*;
import java.util.*;

class bat6ok {   //  filename should be the same as class name

 public final static int MAX_STONE = 31;
 public final static int MIN_STONE = 18;
 public final static int MAX_TAKE = 9;

 // define some global variables for the Game
  int nStone; // number of Stone
  int maxTake; // max stone to take for some game

  boolean lastToWin;  // Win or Lose if took last one stone?
  boolean userFirst, playAgain;
  /// 
enum player {USER, COMPUTER};  // 這樣寫後面可以用USER/COMPUTER
 player who;  // who is taking the stone? 輪到誰? 注意 Java 寫法 !

public static void main(String xxx[  ]) {
    bat6ok ggyy = new bat6ok( );
    ggyy.main( );
}
bat6ok( ) {   // constructor
     prepareIO( );
}
BufferedReader cin=null;
PrintStream cout=null;

void prepareIO( ) {
     try {
        cin =  new BufferedReader(
                     new InputStreamReader( System.in )  );
        cout = System.out;
     }catch(Exception e) {  }
}

int main( ) { // 原來的 main( ); Note that it has different prototype
   hello( ); 
   randomize( );  // randomize; make it true random
   do {
      prepareGame( );
      userFirst = askUserFirst( );
      playGame( );
      playAgain = askPlayAgain( );
   } while( playAgain );
   cout.printf("Thank you for your play.\r\n");
   return 0;
}

int rand( ) {
     return (int) (32767* Math.random( ) );
}
void srand(int seed) {
} // not necessary :-)
void randomize( ) {
     srand((int)System.currentTimeMillis( ) );
}

void hello( ) {  
   cout.printf("Welcome to the BATNUM game ...\r\n");
}

void prepareGame( ) {
   String msg[ ]={"Lose the game.", "win the game."};
   nStone = MIN_STONE + rand( ) % (MAX_STONE-MIN_STONE+1);
   maxTake = 3 + rand( ) % (MAX_TAKE -3 +1);  // 3..
   lastToWin = rand( )%2 == 1;  // half chance 一半的機會
   cout.printf("\r\nThere are %d stones.\r\n", nStone);
   cout.printf("At least take 1, at most %d stones.\r\n", maxTake);
   cout.printf("The one who take the last stone will ");
   int kkk;   kkk = 0;
   if(lastToWin) kkk = 1;
   cout.printf("%s\r\n", msg[kkk]); // 拿最後一個輸還是贏?
} // prepareGame

boolean askUserFirst( ) {
    cout.printf( "Do you want to go first(Y, N)? ");
    String buf = null;
    try {
          buf = cin.readLine( );
    } catch(Exception e) { }
    if(buf == null) return false;
    if(buf.length( )==0) buf = "n";
    if(buf.charAt(0)=='N' || buf.charAt(0)=='n') return false; //no
    if(buf.charAt(0)==0) return false;  // no! 
    return true;  //  Yes otherwise
}

boolean askPlayAgain( ) {
    cout.printf( "Do you want to go first(Y, N)? ");
    String buf = null;
    try {
          buf = cin.readLine( );
    } catch(Exception e) { }
    if(buf == null) return false;
    if(buf.length( )==0) buf = "n";
    if(buf.charAt(0)=='N' || buf.charAt(0)=='n') return false; //no
    if(buf.charAt(0)==0) return false;  // no! 
    return true;  // Yes
} // askPlayAgain

void playGame( ) {
   boolean userWin=true; // assume user win
   if(userFirst) userTurn( );
   else cout.printf(" OK. I go first.\r\n");
   while(nStone > 0 ) {
        computerTurn( );
        if(nStone <= 0) break; // gameOver
        userTurn( );
   } // game Over when leave the while Loop
   userWin = true; // assume you win
   if(lastToWin && (who == player.COMPUTER) ) userWin = false; // user 輸!
   if( (!lastToWin) && (who == player.USER) ) userWin = false; // user 也是輸
   if(userWin) cout.printf(" Congratulations! You win!\r\n");
   else cout.printf(" Sorry, I won! Ha ha!\n");
} // playGame

void userTurn( ) { 
   int nTake, canTake;
   String buf;
   who = player.USER;  // now USER 's turn  記住輪到 USER
   canTake = maxTake;  // 照規定最多只能拿取 maxTake
   if(nStone < maxTake) canTake = nStone;  // 沒那麼多了
   cout.printf(" How many you want to take(1..%d)? ", canTake);
   try {
        buf = cin.readLine( );
        if(buf==null) buf="0";
        nTake = Integer.parseInt(buf);
   }catch(Exception e) { nTake = 0;  }
   while(nTake < 1 || nTake > canTake) { // illegal
      cout.printf("  ?? Error, please re-type(1..%d)? ", canTake);
      try {
           buf = cin.readLine( );
           if(buf==null) buf="0";
           if(buf.length( )==0) buf = "0";
           nTake = Integer.parseInt(buf);
      }catch(Exception e) {  nTake = 0; }
   }
   nStone -= nTake;
   if(nStone == 0) cout.printf(" You just took the last stone.\r\n");
   else cout.printf(" %d stone(s) left.\r\n", nStone);
   return;
} // userTurn

void computerTurn( ) {
   int nTake = 1;
   who = player.COMPUTER;  // now is COMPUTER 's turn 記住輪到 COMPUTER
   // 必贏的策略?  須考慮拿最後一個贏還是輸
   nTake = nStone;
   if(!lastToWin) nTake--;  // 拿最後一個是輸ㄝ, 留一個給對手
   nTake = nTake % (1+maxTake);
   if(nTake == 0) nTake = 1; // 電腦會輸, 先賴皮拿一個等對手拿錯:-)
   if(nTake > nStone) nTake = nStone; // 沒那麼多! (不可能發生)
  ///
   nStone -= nTake;  // 拿走啊
   cout.printf(" I take %d stone", nTake);
   if(nTake > 1) cout.printf("s");  // 複數
   cout.printf(" this time.");
   if(nStone == 0) cout.printf(" I just took the last stone.\r\n");
   else cout.printf(" Now %d stone(s) left.\r\n", nStone);
} // computerTurn

} // class bat6ok
///   === === END of the program BATNUM === ===
