//bat.java
import java.io.*;

class bat {  //
      //BATNUM  --  BATtle of NUMbers   數字大戰

   int nStone, maxTake;  // 總石頭數, 最多可拿幾個?
   boolean lastWin = true; // 拿最後一個贏還是輸?
                          // 注意 C 不認識  true 與 boolean
   boolean userFirst = true;
   PrintStream cout = System.out;
   BufferedReader cin = new BufferedReader ( new InputStreamReader( System.in ) );

   public static void main(String xxx[ ]) {
      new bat(  );
   }
   bat(  ) {
      bbb( );
   }
void bbb( ) {
   //BATtle of NUMbers
   hello( ); // welcome message
   boolean playAgain = true;
   while(playAgain) {
      prepareGame( );
      userFirst = askUserFirst( );
      playGame( );
      playAgain = askPlayAgain( );
   }
   print("Thank you and Bye bye!\n");
} // bbb( )
void print(String s) {  cout.print(s);  }
String readLine( ) {
    String s=null;
    try {
        s = cin.readLine( );
    } catch(Exception e) {   }
    return s;
}
//======================
void hello( ) {
    print(" Welcome 歡迎來玩 BATNUM game !\n");
}
void prepareGame( ) {
    // 用亂數 生出 遊戲所需的各個 Global variables
    nStone =   15 + (int) (Math.random( ) * (31-15+1));    // 用亂數取得 15 .. 31;
    maxTake =  3 + (int) (Math.random( ) * (7-3+1));    //用亂數取得 3..7 ;
    //用亂數決定 winLast 是 true 還是 false
    lastWin = true; 
    if(Math.random( ) >= 0.5) lastWin = false;   // half possibility
    print("Total " + nStone + " stones. 最多可拿 " + maxTake + ", ");
    print("   拿最後一個的是 ");
    if(lastWin) print(" 贏 \n");  else print(" 輸 ! \n");
}
boolean askUserFirst( ) {
   // 若 USER 要先拿, 就傳回 1 
   print(" want to go first(yes, no)? ");
   String s;
   try {
      s = readLine( );
   }catch(Exception e) { s = "no"; }
   if( s.equalsIgnoreCase("NO")  ) return false;  // 他輸入 No
   return (38==38);  // YES / TRUE
}
boolean askPlayAgain( ) {
   // 問user 並讀入答案, 若 USER 要繼續玩, 就傳回 true 
   print(" want to go play Again(yes, no)? ");
   String s;
   try {
      s = readLine( );
   }catch(Exception e) { s = "no"; }
   if( s.equalsIgnoreCase("NO")  ) return false;  // 他輸入 No
   return true;  //  yes to play again 了 :-) 
}
void userTurn( ) {
   int nTake =  1;  // 問 USER 要拿幾個;
   print(" how many you want to take? ");
   String s=null;
   try {
      s = cin.readLine( );
   } catch(Exception e) {  } 
   try {
       nTake = Integer.parseInt(s);  // 類似 C 的 atoi(char*) 轉為整數 int 
   } catch(Exception e) { nTake = 1; }
   int gg = Math.min(nStone, maxTake);
   if(nTake > gg)  {
        nTake = gg;
        print("   ?? You 亂拿, 現在最多只能拿 " + gg + "個, ");
        print(" 我幫你改為拿 " + nTake + " 個!!\n");
   }
   if(nTake < 0)  {
        nTake = 1;
        print("   ?? You 亂拿, 我幫你改為拿 " + nTake + " 個!!\n");
   }
   // 檢查 nTake 是否合乎規定? 若不符合則要求 user 重新輸入 
   // 然後當然要從 nStone 減去 nTake
   nStone = nStone - nTake;
   print(" .. now " + nStone + " stone");
   if(nStone > 1) print("s left.."); else print(" left..");
}
void computerTurn( ) {
  // 先寫隨便拿但要合乎規定, 就是取 1..min(maxTake, nStone) 的亂數 
  // 再想出可贏就會贏的策略 (很簡單 :-)
  // Hint: 拿 max(1, (nStone - xxx) % (1+maxTake) ) 就會贏
  //    其中 xxx 是  0  if lastWin is true
  ///        xxx 是  1  if lastWin == false
  int nTake = nStone;
  if(! lastWin) nTake = nTake - 1;
  nTake = nTake % (1 + maxTake);
  if(nTake == 0) nTake = 1;
  print(" computer takes " + nTake + "\n");
  nStone -= nTake;  //////
  print(" Now 剩下 " + nStone + " 個");
  if(nStone > 0) print(", 換你拿.."); else print("\n");
}
void playGame( ) {
  // user and the computer take Turn until no more stones left
  if(userFirst) userTurn( );
  boolean uWin = true;  // 假設 user 贏 
  while(nStone > 0)  {  // Loop until no more stone
      computerTurn( ); 
      if(nStone <= 0) {  //   if no more stone then leave the Loop;
           if(lastWin) uWin = false;  else uWin = true;
           break;
      }
      userTurn( );
      if(nStone <= 0) {  //   if no more stone then leave the Loop;
           if(lastWin) uWin = true;  else uWin = false;
      }
  } // end Loop
  if(uWin) print("\nCongratulations .. you win!\n");
  else print("Ha ha ! 我贏了啦! 哈 哈 哈! \n");
  // Judge the game result and print some message..
  // 如何判斷誰輸誰贏? 須知道誰拿走最後一個! 
  // 注意拿最後一個是輸還是贏? 
  //   (lastWin == true 表示拿到最後一個的贏 )
} // playGame

}  // calss bat
