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


public class BJ2App extends GBApplet
{

	//Define the GUI elements
	Label dealerScoreLabel = addLabel ("Dealer's Score",1,1,1,1);
	IntegerField dealerScoreField = addIntegerField (0,2,1,1,1);

	Label dealerWinLabel = addLabel ("Dealer's  Wins",3,1,1,1);
	IntegerField dealerWinField = addIntegerField (0,4,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 playerWinLabel = addLabel ("Your  Wins",7,1,1,1);
	IntegerField playerWinField = addIntegerField (0,8,1,1,1);

	Label playerHandLabel = addLabel ("Your Hand",5,2,1,1);
	TextArea playerHandField = addTextArea ("",6,2,2,3);

	Label resultLabel = addLabel ("Result:",9,1,1,1);
	TextField resultField = addTextField ("",9,2,1,1);


	Button dealButton = addButton ("Deal",10,1,1,1);
	Button resetButton = addButton ("Reset",10,2,1,1);
	Button hitButton = addButton ("Hit",11,1,1,1);
	Button stayButton = addButton ("Stay",11,2,1,1);
	
	//Declare Variables
	int pcount, dcount, point, i, holepoint, dwins = 0, pwins = 0;
	
	boolean dIsSoft = false;
	boolean pIsSoft = false;
	
	String card, holecard, tempstr;


	public void buttonClicked (Button buttonObj) 
	{
		//Test to see which button is clicked		
		if (buttonObj == dealButton)
		{
		//This code runs if the deal button is clicked
			holepoint =0;

			//Run a loop to initially deal out two cards to the dealer and the player
			for (i = 0; i<2; i++)
			{
				//This module creates the cards
				deal();
				//This checks if the card is an Ace and if it should count as one or eleven
				if ((card == "Ace")&&((dcount + holepoint) <= 10))
				{
					point = 11;
					dIsSoft = true;
				}
				//This code makes the first card dealt to the dealer face down (hole card)
				if (i == 0)
				{
					holepoint = point;
					holecard = card;
					point = 0;
					card = "Hole Card";
				}
				//This totals the dealers score
		 		dcount += point;
				//This checks to see if the hand includes an Ace counted as eleven and 
				//the score goes over twenty one
				if ((dIsSoft == true)&&((dcount + holepoint) > 21))
				{
					dcount -= 10;
					dIsSoft = false;
				}
				//This displays the dealers score and hand		   		   
		 		dealerScoreField.setNumber (dcount);
	        		card = card + "\n";
		 		dealerHandField.append(card);
				
				//This creates a card for the player	
		   		deal();
				//This checks if an Ace should count as one or eleven
				if ((card == "Ace")&&(pcount <= 10))
				{
					point = 11;
					pIsSoft = true;
				}
				//This totals the players score
		   		pcount += point;
				//This checks if the hand includes an Ace counted as eleven and
				//the score goes over twenty one
				if ((pIsSoft == true)&&(pcount > 21))
				{
					pcount -= 10;	
					pIsSoft = false;
				}
				//This displays the players score and hand				   		   
		   		playerScoreField.setNumber (pcount);
	          		card = card + "\n";
		   		playerHandField.append(card);				
			}
			//This hides the deal and reset buttons and shows the hit and stay buttons
			dealButton.setVisible (false);
			resetButton.setVisible (false);
		   	hitButton.setVisible (true);
		   	stayButton.setVisible (true);
		}				
		else if (buttonObj == hitButton)
		{
		//This code runs when the hit button is clicked
			//This creates a card for the player		
			deal();
			//This checks the value an Ace should get
			if ((card == "Ace")&&(pcount <= 10))
			{
				point = 11;
				pIsSoft = true;
			}
			//Totals the players score
		   	pcount += point;
			//Checks for an Ace that counts eleven when the score goes over twenty one
			if ((pIsSoft == true)&&(pcount > 21))
			{
				pcount -= 10;	
				pIsSoft = false;
			}
			//Displays the players score and hand				   		   
		   	playerScoreField.setNumber (pcount);
	          	card = card + "\n";
		   	playerHandField.append(card);
			//This checks to see if the players score has gone over twenty one
			if (pcount > 21)
			{
				//If the players score is over twenty one display the message in the result field
				resultField.setText ("You have Busted. The Dealer Wins!");
				//Add one to the dealers wins
				dwins += 1;
				//display the dealers wins 
				dealerWinField.setNumber (dwins);
				//Remove the "Hole Card" entry from the dealers hand
				tempstr = dealerHandField.getText();
				card = tempstr.substring(9);
				//Display the actual value for the hole card
				card = holecard + card;
				dealerHandField.setText(card);
				//Display the dealers total score
				dealerScoreField.setNumber (dcount + holepoint);
				//Hide all buttons except the reset because the hand is finished
		   		hitButton.setVisible (false);
		   		stayButton.setVisible (false);
		   		dealButton.setVisible (false);
				resetButton.setVisible (true);
			}
			else
			{
		   		//If the players score is not over twenty one give the chance to hit again or stay		
		   		hitButton.setVisible (true);
		   		stayButton.setVisible (true);
		   		dealButton.setVisible (false);

			}
		}
		else if (buttonObj == stayButton)
		{
		//This code runs when the stay button is clicked
			//This runs a loop to keep dealing cards to the dealer until his score is greater than sixteen
			while ((dcount + holepoint) <= 16)
			{
				//This creates a card for the dealer
				deal();
				//This checks to see what value an Ace should be given
				if ((card == "Ace")&&((dcount + holepoint) <= 10))
				{
					point = 11;
					dIsSoft = true;
				}
				//Totals the dealers score
		 		dcount += point;
				//Checks for an Ace counting eleven if score goes over twenty one
				if ((dIsSoft == true)&&((dcount + holepoint) > 21))
				{
					dcount -= 10;
					dIsSoft = false;
				}
				//Display dealers score and hand		   		   
		 		dealerScoreField.setNumber (dcount);
	        		card = card + "\n";
		 		dealerHandField.append(card);
			}
			//The dealer must stay if his score is greater than sixteen
			if ((dcount + holepoint) > 16)
		   	{
				//This checks if the players score is greater than the dealers
				if (pcount > (dcount + holepoint))
				{
					//Display message in result field
				    	resultField.setText("You Win!");
					//Adds one to players win total
					pwins += 1;
					//Displays players wins total
					playerWinField.setNumber (pwins);
					//This code strips out the "Hole Card" entry from the dealers hand and
					//adds in the actual value of the hole card
					tempstr = dealerHandField.getText();
					card = tempstr.substring(9);
					card = holecard + card;
					dealerHandField.setText(card);
					//This displays the dealers total score including the hole point value
					dealerScoreField.setNumber (dcount + holepoint);
					//This hides all buttons except the reset button
		   		    	hitButton.setVisible (false);
		   		    	stayButton.setVisible (false);
		   		    	dealButton.setVisible (false);
					resetButton.setVisible (true);
		       	}
				//This checks if the players score is equal to the dealers
				else if (pcount == (dcount + holepoint))
				{
					//Display message in result field and display dealers full hand and score	
				    	resultField.setText("It's a Push.");
					tempstr = dealerHandField.getText();
					card = tempstr.substring(9);
					card = holecard + card;
					dealerHandField.setText(card);
					dealerScoreField.setNumber (dcount + holepoint);
					//This hides all buttons except the reset button
		   		    	hitButton.setVisible (false);
		   		    	stayButton.setVisible (false);
		   		    	dealButton.setVisible (false);
					resetButton.setVisible (true);
				}
				//This checks if the players score is less than the dealers
				else if (pcount < (dcount + holepoint))		
				{
					//This checks to see if the dealer has gone over twenty one
					if ((dcount + holepoint) > 21)
					{
						//Display message in result field add one to players win total and display						
						resultField.setText ("The Dealer has Busted. You Win!");
						pwins += 1;
						playerWinField.setNumber (pwins);
						//display dealers full hand and score
						tempstr = dealerHandField.getText();
						card = tempstr.substring(9);
						card = holecard + card;
						dealerHandField.setText(card);
						dealerScoreField.setNumber (dcount + holepoint);
						//Hide all buttons except reset button
		   				hitButton.setVisible (false);
		   				stayButton.setVisible (false);
		   				dealButton.setVisible (false);
						resetButton.setVisible (true);
					}
					//This code runs if the players score is less than the dealers 
					//and the dealer has not gone over twenty one
					else 
					{
						//Display message in result field
						resultField.setText("You lose! The Dealer wins!");
						//Add one to dealers win total and display win total
						dwins += 1;
						dealerWinField.setNumber (dwins);
						//Display dealers full hand and score
						tempstr = dealerHandField.getText();
						card = tempstr.substring(9);
						card = holecard + card;
						dealerHandField.setText(card);
						dealerScoreField.setNumber (dcount + holepoint);
						//Hide all buttons except the reset button
		   		    		hitButton.setVisible (false);
		   		    		stayButton.setVisible (false);
		   		    		dealButton.setVisible (false);
						resetButton.setVisible (true);
					}				 		
				}
			}			
		}
		//This code runs when the reset button is clicked
		else 
		{
			//Reset all variables except wins to their original values			
			pIsSoft = false;
			dIsSoft = false;
			pcount = 0;
			dcount = 0;
			card = "";
			holepoint = 0;
			//Clear the displays for a new hand
			playerScoreField.setNumber (pcount);
			dealerScoreField.setNumber (dcount);
			playerHandField.setText(card);
		       dealerHandField.setText(card);
			resultField.setText (" ");
			//This hides all buttons except the deal button			
		       hitButton.setVisible (false);
		       stayButton.setVisible (false);
		   	dealButton.setVisible (true);
			resetButton.setVisible (false);
		}
	}
	//This is the module that creates the cards
	private void deal()
	{
		//This sets the point variable to a random number between one and thirteen
		point = (int)(1 + Math.random() * (13 - 1 + 1));
		//This takes the value of the point variable and determines what card it corresponds to
		if (point == 1)card = "Ace";
		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";
		//This changes the point value for any face card to 10
		if ((card == "Jack")||(card == "Queen")||(card == "King"))point = 10;
	}

}