import java.awt.*;
import BreezyGUI.*;


public class BJApplet extends GBApplet
{

	//Define GUI elements
	Label dealerScoreLabel = addLabel ("Dealer's Score",1,1,1,1);
	IntegerField dealerScoreField = addIntegerField (0,2,1,1,1);
	Label dealerHandLabel = addLabel ("Dealer's Hand",1,2,1,1);
	TextArea dealerHandField = addTextArea ("",2,2,2,3);

	Label playerScoreLabel = addLabel ("Your Score",5,1,1,1);
	IntegerField playerScoreField = addIntegerField (0,6,1,1,1);
	Label playerHandLabel = addLabel ("Your Hand",5,2,1,1);
	TextArea playerHandField = addTextArea ("",6,2,2,3);

	Button dealButton = addButton ("Deal",9,1,1,1);
	Button resetButton = addButton ("Reset",9,2,1,1);
	Button hitButton = addButton ("Hit",10,1,1,1);
	Button stayButton = addButton ("Stay",10,2,1,1);
	

	//Declare variables
	int pcount, dcount, point;
	boolean dealer = false;
	boolean dIsSoft = false;
	boolean pIsSoft = false;
	String card;

	
	public void buttonClicked (Button buttonObj) 
	{
		//Test which button was clicked. The deal and hit buttons do the same thing.
		if ((buttonObj == dealButton)||(buttonObj == hitButton))
		{
			//This checks to see if its the dealer's turn or the player's.
			if (dealer)
			{
				//If the dealer's hand is over 16 he must hold
		   		if (dcount > 16)
		   		{
			 		dealer = false;
			 		hitButton.setVisible (true);
		        		stayButton.setVisible (true);
		   	 		dealButton.setVisible (false);
		   		}
		   		else 
		   		{
					//This creates the cards for the dealer's hand
		   			point = (int)(1 + Math.random() * (13 - 1 + 1));
					
					//This assigns the correct names for the cards and sets the point value
					//If its an Ace it can be worth 1 or 11 points
		   			if (point == 1)
		   			{
						card = "Ace";
						if (dcount <= 10)
						{
							point = 11;
							dIsSoft = true;
						}
						else point = 1;
		   			}	
		   			else if (point == 2)card = "Two";
		   			else if (point == 3)card = "Three";
		   			else if (point == 4)card = "Four";
		   			else if (point == 5)card = "Five";
		   			else if (point == 6)card = "Six";
		   			else if (point == 7)card = "Seven";
		   			else if (point == 8)card = "Eight";
		   			else if (point == 9)card = "Nine";
		   			else if (point == 10)card = "Ten";
		   			else if (point == 11)card = "Jack";
		   			else if (point == 12)card = "Queen";
		   			else if (point == 13)card = "King";

		 			if ((card == "Jack")||(card == "Queen")||(card == "King"))point = 10;

					//This keeps the total for the dealers hand.
		 			dcount += point;

					//This checks to see if the total contains an Ace that is counted as 11.
					//If it does and the total goes over twenty one this will change the Ace's value to one.
					if ((dIsSoft == true)&&(dcount > 21))
					{
						dcount -= 10;
						dIsSoft = false;
					}

					//Display the dealers points and hand.		   		   
		 			dealerScoreField.setNumber (dcount);
	        			card = card + "\n";
		 			dealerHandField.append(card);

					//Checks if the dealer has busted. If he has hide all buttons except the reset.
					if (dcount > 21)
					{
						dealerHandField.setText ("The Dealer has Busted.\nYou Win!");
		   				hitButton.setVisible (false);
		   				stayButton.setVisible (false);
		   				dealButton.setVisible (false);
					}
					else 
					{
				 		hitButton.setVisible (true);
		  		 		stayButton.setVisible (true);
		   	        		dealButton.setVisible (false);
					}
		   			dealer = false;
		   		}
			}
			else 
			{

				//Creates cards for the player's hand.	
		   		point = (int)(1 + Math.random() * (13 - 1 + 1));

				//This assigns the correct names for the cards and sets the point value
				//If its an Ace it can be worth 1 or 11 points
		   		if (point == 1)
		   		{
					card = "Ace";
					if (pcount <= 10)
					{
						point = 11;
						pIsSoft = true;
					}
					else point = 1;
		   		}
		   		else if (point == 2)card = "Two";
		   		else if (point == 3)card = "Three";
		   		else if (point == 4)card = "Four";
		   		else if (point == 5)card = "Five";
		   		else if (point == 6)card = "Six";
		   		else if (point == 7)card = "Seven";
		   		else if (point == 8)card = "Eight";
		   		else if (point == 9)card = "Nine";
		   		else if (point == 10)card = "Ten";
		   		else if (point == 11)card = "Jack";
		   		else if (point == 12)card = "Queen";
		   		else if (point == 13)card = "King";

		   		if ((card == "Jack")||(card == "Queen")||(card == "King"))point = 10;
		      
				//This keeps the total for the players hand.
		   		pcount += point;

				//This checks to see if the total contains an Ace that is counted as 11.
				//If it does and the total goes over twenty one this will change the Ace's value to one
				if ((pIsSoft == true)&&(pcount > 21))
				{
					pcount -= 10;	
					pIsSoft = false;
				}

				//Displays the players points and hand				   		   
		   		playerScoreField.setNumber (pcount);
	          		card = card + "\n";
		   		playerHandField.append(card);

				//Checks if the player has busted. If he has hide all buttons except the reset.
				if (pcount > 21)
				{
					playerHandField.setText ("You have Busted.\nThe Dealer Wins!");
		   			hitButton.setVisible (false);
		   			stayButton.setVisible (false);
		   			dealButton.setVisible (false);
				}
				else
				{
		   			dealer = true;
		   			hitButton.setVisible (false);
		   			stayButton.setVisible (false);
		   			dealButton.setVisible (true);
				}
			}
		}

		//This checks if the stay button has been clicked.
		else if (buttonObj == stayButton)
		{
		   //This changes it to the dealers turn and hides the hit and the stay buttons.
		   dealer = true;
		   hitButton.setVisible (false);
		   stayButton.setVisible (false);
		   dealButton.setVisible (true);

			//This checks to see if the dealer's points are over 16.
			if (dcount > 16)
			{
				//This checks if the player's points are higher than the dealer's
				if (pcount > dcount)
				{

				    //This tells you you have won and hides all buttons except the reset.
				    playerHandField.setText("You Win");
		   		    hitButton.setVisible (false);
		   		    stayButton.setVisible (false);
		   		    dealButton.setVisible (false);
		       	}

				//This checks for a tie
				else if (pcount == dcount)
				{
				    playerHandField.setText("It's a Push");
		   		    hitButton.setVisible (false);
		   		    stayButton.setVisible (false);
		   		    dealButton.setVisible (false);
				}
					
		       }
		}

		//This is the code that runs when the reset button is clicked.
		else 
		{
			pIsSoft = false;
			dIsSoft = false;
			pcount = 0;
			dcount = 0;
			card = "";
			playerScoreField.setNumber (pcount);
			dealerScoreField.setNumber (dcount);
			playerHandField.setText(card);
		       dealerHandField.setText(card);
			dealer = false;
		       hitButton.setVisible (true);
		       stayButton.setVisible (true);
		   	dealButton.setVisible (false);
		}
	}

}