import SimpleOutput;
import SimpleInput;
import Board;
import Player;
import OthelloRules;
import ComputerPlayer;
import GameState;
 
/** This "faux" class represents a textual user interface and is designed 
* to work with the above classes. 
*
* Ideally, this TUI will organize the other groups' classes in such a way 
* that will allow students, staff, and computers to play the Othello game
* conceived by Sam Rebelsky's CompSci 152 class.
*
* @author Amber McNett
* @author Hisako Watanabe
* @author Katt Thorne 
* version 1.3 of April 2000
*/
 
public class TUISample {
 

  // +--------+--------------------------------------------------
  // | Fields |
  // +--------+
// player 1's name
protected String name;
 
// player 2's name
protected String name2;

// game state
protected GameState gamestate;

// skill level
protected int skill;


 

// +------------+-----------------------------------------------
  // | Helper Methods |
  // +----------------+

/**
 * This method returns true if the user typed y or Y.
 * This method also asks user to type until s/he types
 * Y, y, N, or n.
 * Pre: the parameter is String. 
 * Problem: if input is other than String, it might crash.
 */
public boolean isYes(Object input) {
    
    SimpleOutput out = new SimpleOutput();
    SimpleInput in = new SimpleInput();

    //Check if the input is Y, y, N, or n.
    //If it is other than those answer,
    while (!input.equals("Y") ||
           !input.equals("y") ||
           !input.equals("N") ||
           !input.equals("n")) {

        //ask to type again.
        out.println("Invalid. Please type again. ");
        input = in.readString();
    }
    
    return (input.equals("Y") || input.equals("y"));
} //public boolean isYes(Object)


 
// +------------+-----------------------------------------------
  // | Modifier Methods |
  // +------------------+




/**  This is a helper method employed by run() which helps to make sure the
* current move is legal, then updates the board and advances the player's
*  turn.      **/

public void promptMove() {
     OthelloRules othrules = new OthelloRules();

    // human player is prompted to enter row and column number...
     out.print("Please enter the row number where you");
     out.println(" would like to move next.");

     int row = in.readInt();    

     out.print("Please enter the column number where you would"); 
     out.println(" like to move next.");
  
     int col = in.readInt();

     int currentp = othrules.getPlayer();

     //checks to see if  move is legal..
     while  (rules.legalMove(currentp, row, col) == false) {
        
         out.println("That move is not legal.");
         othtui2.promptMove();
        }

     // we though game state and rules would communicate with the board about 
     // the proper piece/color changes on the board once a piece was placed 
     // on a new location.  The updateBoard method, if it does not already
     // exist, would look at the new piece placed by the player, then update
     // the rest of the board accordingly.  Is this is a problem, please
     // let us know, but we hoped not to mess with the Piece class in this TUI.
     currboard = gamestate.updateBoard(row, col);

     
     //this is assuming Game State has such a method...
     gamestate.nextTurn();


} // promptMove()
      

public void run() {
   SimpleOutput out = new SimpleOutput();
   SimpleInput in = new SimpleInput();
   TUIsample othtui2 = new TUIsample();
   OthelloRules rules = new OthelloRules();
   GameState gs = new GameState();
   ComputerPlayer complayer = new ComputerPlayer(skill, ____);  
  // need to add color param. Might need to implement an array?
   Player player = new Player();
   

   gamestate = gs;  // initializing field

   // prompting the player for Board dimensions...
   out.println("How many rows would you like to comprise the game board?");
   introw = in.readInt();
   while (introw < 3 || introw > 16) {
     out.print("That would not be a very enjoyable game.  Please enter a");
     out.println(" number between 3 and 16.");
     introw = in.readInt(); } 
    // while

   out.println("How many columns would you like to comprise the game board?");
   intcol = in.readInt();
   while (intcol < 3 || intcol > 16) {
     out.print("That would not be a very enjoyable game.  Please enter a");
     out.println(" number between 3 and 16.");
     intcol = in.readInt(); } 
   // while
 
   // creating Board...
   Board board = new Board(introw, intcol);



   // Since all versions of the game apply to a rectangular board, 
   // we weren't sure if this method would
   // change.  That is, we did not allow for versions which would 
   // allow for alternate starting piece con-
   // figurations (though we certainly can, if necessary).  
   rules.startingPieces(gamestate);


   while (rules.EndofGame(gamestate) == false) {
      currplayer = rules.getPlayer();
      
      // WE NEED AN isHuman(int player) METHOD IN THE PLAYER CLASS.
      if (player.isHuman(currplayer) == true){

           // We are now prompting the player to enter his/her move.

          out.println("Player " rules.getPlayer() + " it is now your turn.");
          out.print("Would you like a list of available moves?");
          out.println(" Enter Y or N.");
          
          String input = in.readString();
          if (isYes(input) == true) {
             rules.allMoves(gamestate,currplayer);
          }   

          othtui2.promptMove(); 
      } // if (player.isHuman())


      else {
          complayer.getMove(gamestate, rules); 
     
        // see above (in promptMove()) for notes on this...
        currboard = gamestate.updateBoard(row, col);

        //this is assuming Game State has such a method...
        gamestate.nextTurn();                  
            
      } // else
   } // while (rules.EndofGame(gamestate);)


   // CAN WE HAVE A WINNER METHOD IN RULES?
    int winner = rules.getWinner();
    if (player.isHuman(winner) == true) {
      out.print("Congratulations, Player " + winner + "! You are the winner!");
    }
    else out.println("Computer " + winner + " is the winner.");

  out.println(" Hooray!");
  out.println("Do you want to play again?  Type Y or N");
  String answer = in.readString();
  if (isYes(answer) == true) {

      
    // *********HOW DO WE RE-EXECUTE THE MAIN METHOD HERE?***********
  

  }
  // DO WE NEED AN EXIT METHOD HERE, OR IS IT AUTOMATIC?
  else rules.exit();
      
   } // run()
  


 
// +------------+-----------------------------------------------
  // | Main Methods |
  // +--------------+



/** This is the main method which will begin the program and let it know 
 * how many human players will be playing. 
 * It will also interact with the above classes to set the board, allow
 * players to interact with the game, update the board, change turns, 
 * check legality of moves, and end the game. */

public static void main(String[] args) {
    SimpleOutput out = new SimpleOutput();
    SimpleInput in = new SimpleInput();
    TUIsample othtui = new TUIsample();
    int playnum;
    out.println("Welcome to CS152 Othello, version 1.0!");
    out.println(" How many human players will be playing today?");
    playnum = in.readInt();
      while  (playnum > 2 || playnum < 0) {
           out.println("Invalid number.  Please enter again.");
           playnum = in.readInt();
         }
      if (playnum == 0) {
            out.print("What skill level would you like the computers to");
            // HOW MANY LEVELS HERE?
            out.println(" play at?  Enter a number 1 - 3"); 
            this.skill = in.readInt();
            while (skill < 1 || skill > 3) {
               out.println("Please enter a number between 1 and 3");
            }
            othtui.run();
             
          }
      else if (playnum == 1) {
      out.print("What skill level would you like your Computer opponent to");
            // HOW MANY LEVELS HERE?
            out.println(" play at?  Enter a number 1 - 3"); 
            this.skill = in.readInt();
            while (skill < 1 || skill > 3) {
               out.println("Please enter a number between 1 and 3");
            }

               othtui.run();       
      }
      else {
                    othtui.run();

         } 
  } // main
   
} // TUIsample


