/*
 * Created on Mar 19, 2004
 *
 * @author: Owen Astrachan
 */
package ooga;


public class GUIPlayer implements IPlayer, Runnable {

	private Move myMove;
	private Thread myStaller;
	
	public GUIPlayer(){
		
	}
	
	public Move getMove(final IGameModel model) throws InterruptedException {
		/**
		 * TODO: enable GUI interactions. This
		 * means allowing button presses to have an effect,
		 * perhaps this player is passed to the GUI?
		 */ 
		
		myMove = null;
		Thread self = new Thread(this);
		self.start();
		
		for(int k=0; k < 10; k++){
	
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// nothing to do
			}
			System.out.println("counting to 10 at "+k);
		}
		setMove(new Move(2,3));
		myStaller = new Thread(){
			public void run() {
				int count = 0;
				while (myMove != null){
					try {
						Thread.sleep(500);
					} 
					catch (InterruptedException e) {
						// nothing to do
					}
					System.out.println("spin wait for move "+count);
					count++;	
				}
			}
		};
		myStaller.start();
		myStaller.join();
		
	}

	public String getName() {
		return "Fred";
	}

	/**
	 * Runs until move set by some object, then stops
	 * This method is from Runnable interface
	 */
	public void run() {
		int count=0;
		while (myMove == null){
			try {
				Thread.sleep(500);
			} 
			catch (InterruptedException e) {
				// nothing to do
			}
			System.out.println("spin wait for move "+count);
			count++;
		}
	}
	
	/**
	 * Called, for example, by GUI to set move
	 * @param m is the move that is set by some object
	 */
	public void setMove(Move m){
		myMove = m;
	}
}
