import java.awt.*;

// Yet another Element of the Focus-Family
class PlayYourSelf extends Strategy
{
	private int lastScore;
	private PYSClientFrame cf = null;

	void priv_init()
	{
		cf = new PYSClientFrame(maxPlayableNumber);	
		cf.show();
		lastScore = 200;
	}

	public int initial_turn() 
	{ 
		return cf.selection(1,0,0); 
	}

	public int turn(int lt[], int sc[], int carryOver, int turnNo)
	{
		int bid = cf.selection(turnNo, sc[0], sc[0]-lastScore); 
		lastScore = sc[0];
		return bid;
	}
	
	// I'm not currently sure what of the following actions is
	// essential and what is entirely redundant.
	public void destroy()
	{
		cf.hide();
		cf = null; // Should be unnecessary
	}
}


// In Java1.1 I had made this a private subclass of PlayYourSelf!
class PYSClientFrame extends Frame
{
	private int mpn;
	private Button bs[];	
	private Label myL1, myL2, myL3;

	PYSClientFrame(int maxPlayableNumber)
	{
		setTitle("Your Turn!");
		setBackground(new Color(255,250,220));
		mpn =maxPlayableNumber; 

		Panel p = new Panel();
		setResizable(false);
		setLayout(new BorderLayout());
		p.setLayout(new GridLayout((maxPlayableNumber+1)/10,0));
		bs = new Button[maxPlayableNumber+1];
		
		for (int i = 0; i <= mpn; i++)
		{
			bs[i] = new Button();
			bs[i].setLabel(new Integer(i).toString());
			p.add(bs[i]);
		}
		add("South", p);	
		p = new Panel();
		p.setLayout(new GridLayout(2,4));
		p.add(new Label("Turn:", Label.RIGHT));
		myL1 = new Label(); p.add(myL1);
		p.add(new Label("Score:", Label.RIGHT));
		myL2 = new Label(); p.add(myL2);
		p.add(new Label());
		p.add(new Label());
		p.add(new Label("Delta:", Label.RIGHT));
		myL3 = new Label(); p.add(myL3);
		add("North", p);
		pack();
		//setAllButtonEnabled(false);
	}

	private synchronized void 
	setAllButtonEnabled(boolean what)
	{
		for(int j = 0; j <= mpn; j++) bs[j].setEnabled(what);				
	}

	private int retVal = -1;

	public synchronized boolean action(Event evt, Object arg)
	{
		for (int i = 0; i <= mpn; i++)
			if ( bs != null && bs[i] != null 
				&& evt.target.equals(bs[i]) )
			{
				//setAllButtonEnabled(false);
				retVal = i;
				break;
			}
		return false;
	}

	int selection(int turnNo, int score, int gain)
	{
		synchronized(this)
		{
			retVal = -1;
		}
		myL1.setText(""+turnNo);
		myL2.setText(""+score);
		myL3.setText(""+gain);
		//setAllButtonEnabled(true);
		while ( retVal == -1 && bs != null )
		{
			try
			{
				Thread.currentThread().sleep(25);
			}
			catch(Exception e)
			{
			}
		}
		return retVal;
	}	

}

