//  package Java.TicTacToe.Game;

/* 
 * Usage : java HumanPlayer <playerNum> <ORBArgs>
 *         playerNum =  0 or 1
 *         ORBArgs = Arguements for initializing the ORB(i.e -ORBInitialPort 1050)
 * Note  : This program allows a user to play Tic Tac Toe with another
 *         player or a computer on the network.  First, the game server must be 
 *         started on your network before executing the HumanPlayer.
 *         To execute the game server type see the TicTacToe.java file.
 *         To play two players start up two HumanPlayer 0 and 1
 *         To play the compuer see the ComputerPlayer.java file
 *         
 */


import RemoteInterface.*;
import java.beans.*;  // needed for gameBoard add...ChangeListener
import java.io.*;

// HumanPlayer is a Corba Servant Class
class HumanPlayer
    extends Player
{
    protected int[] boardState = new int[9]; 
    protected TicTacToeBoard ticTacToeBoard; 
  

    public static void main(String args[])
    {
	try {
	    if ( (args.length < 1) || 
		 !( ( args[0] != "1") && (args[0] != "0")  ) ) {
		throw new InstantiationException("Incorrect commandline arguments");
	    }
	    String ORBArgs[]= new String[args.length-1];
	    System.arraycopy(args, 1, ORBArgs, 0, args.length-1);
	    
	    Player player = new HumanPlayer(args[0]);
	       
	    // Establish two way communication with remoteGame
System.out.println("Ready to initialize\n");
	    player.initialize(ORBArgs);  
	    
	}
	catch (Exception e) {
	    System.err.println("Error creating instance of HumanPlayer: " + e);
	    e.printStackTrace();
	    
	    System.err.print("\nUsage: java HumanPlayer <playerNum> <ORBArgs>\n");
	    System.err.print("playerNum =  0 or 1\n");
	    System.err.print("OrbArgs = optional arguments to start the ORB (i.e. -ORBInitialPort 1050)\n");
	    System.exit(1);
	}
	
    }
    
    
    public HumanPlayer(String newPlayerNo) 
    {
	
	super(newPlayerNo);
	
	// Reset the game board
	for(int i = 0; i < boardState.length; i++)
	    boardState[i] = 2;
	
	try {
	    // create and display TicTacToeBoard    
	    ticTacToeBoard = new TicTacToeBoard(playerNo);

	    // Board will tell us when a position has been taken
	    ticTacToeBoard.addPropertyChangeListener( new PropertyChangeListener() {
		public void propertyChange(PropertyChangeEvent evt) {
		    setMove((Move)evt.getNewValue());  // change gameState
		}
	    });

	    // Board will tell us if a position has been clicked on
	    ticTacToeBoard.addVetoableChangeListener( new VetoableChangeListener() {
		public void vetoableChange(PropertyChangeEvent evt) 
		    throws PropertyVetoException {
		    if (!isValidMove((Move)evt.getNewValue() ) ) 
			throw new PropertyVetoException("Invalid Move", evt);
		}		
	    });

	    ticTacToeBoard.showTicTacToe();
	}
	catch (Exception e) {
	    System.err.println("Error creating instance of HumanPlayer: " + e);
	    e.printStackTrace();
	    System.exit(1);
	}
    }

    protected void setYourTurn( boolean turn )
    {
	super.setYourTurn(turn);
	ticTacToeBoard.setYourTurn(turn);
    }

    protected void setGameStatus( int newStatus ){
	super.setGameStatus(newStatus);
	ticTacToeBoard.setGameStatus(newStatus);
    }

 //     public int[] getCurrentState(){ // nobody calls - jlr
//  	return boardState;
//      }

    protected void setCurrentState( int[] newState ){
	boardState = newState;
	ticTacToeBoard.setGameBoardPositions( newState );
    }
    
    public void wonGame(int[] newState){
	setYourTurn(false);
	setGameStatus(1);
	setCurrentState( newState );
    }

    public void lostGame(int[] newState){
	setYourTurn(false);
	setGameStatus(0);
	setCurrentState( newState );
    }
    
    public void tieGame(int[] newState){
	setYourTurn(false);
	setGameStatus(2);
	setCurrentState( newState );
    }
    
    public boolean isValidMove(Move possibleMove) {
	return (isYourTurn() && 
		possibleMove.newPosition >= 0 && 
		possibleMove.newPosition  < boardState.length && 
		boardState[possibleMove.newPosition ] == 2);
    }
  
    public void yourMove(int[] newState)
    {
	setYourTurn(true);
	setCurrentState(newState);
    }
    
    public void setMove(Move newMove) 
    {
	
	if(playerNo == newMove.playerId) {
	    // Player just made a move.
	    // Release the token 
	    setYourTurn(false);
	  
	    boardState[newMove.newPosition] = newMove.playerId;
	    remoteGame.setMove(newMove);    
	}
	else {
	    // This should never happen
	    Exception e = new Exception("Illegal value playerID");
	    System.err.println("setMove: " + e);
	    e.printStackTrace();
	    System.exit(1);
	}
    }

    public void resetGame() 
    {
	setYourTurn(false);
	setGameStatus(-1);
	// Reset the game board
	for(int i = 0; i < boardState.length; i++)
	    boardState[i] = 2;

	ticTacToeBoard.setGameBoardPositions(boardState);
    }

 
}
