You can click the "HELP" button at any time during the game for tips on how to play.
Clicking the "HELP" button a second time will bring you back to the game.
import java.awt.*;
import BreezyGUI.*;
public class bjv25 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);
Button infoButton = addButton ("Help",12,1,1,1);
//Declare Integer variables
int pcount, dcount, point, i, holepoint, dwins = 0, pwins = 0;
//Declare Boolean variables
boolean dIsSoft = false, pIsSoft = false, dealer = false, info = false;
boolean stay, del, rset;
//Declare String variables
String card, holecard, tempD, tempP, tempHelp, helpText, returnText;
public void buttonClicked (Button buttonObj)
{
//Test to see which button was clicked
if (buttonObj == dealButton)
{ //This code runs if the Deal button is clicked
//Initialize the variable to store the point value of the Hole Card
holepoint = 0;
//This runs a loop to deal out two cards to the dealer and two to the player
for (i = 0; i<2; i++)
{
//The deal module creates the cards and adds the values to the correct variables
deal(dealer);
//The Dealer variable is a boolean that
//determines if the dealer or the player gets the card
dealer = true;
deal(dealer);
dealer = false;
}
//The disp module hides or shows the buttons as appropriate
disp(true, false, false);
}
else if (buttonObj == hitButton)
{ //This code runs when the Hit button is clicked
//Deal a card to the player
deal(dealer);
//This checks to see if you have gone over 21
if (pcount > 21)
{
//This tells you you have gone over 21
resultField.setText ("You have Busted. The Dealer Wins!");
//This adds a win to the dealer's total
dwins += 1;
//This displays the dealer's new win total
dealerWinField.setNumber (dwins);
//The unhole module shows the dealer's hole card and adds the value to his hand score
unhole(dcount, holepoint);
}
}
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 16
while ((dcount + holepoint) <= 16)
{
dealer = true;
deal(dealer);
}
if ((dcount + holepoint) > 16)
{ //Once the dealer's score is greater than 16 this checks
//to see if the player's score is higher
if (pcount > (dcount + holepoint))
{
//Display the result message
resultField.setText("You Win!");
//This adds a win to the player's total
pwins += 1;
//This displays the player's new win total
playerWinField.setNumber (pwins);
//The unhole module shows the dealer's hole card
//and adds the value to his hand score
unhole(dcount, holepoint);
}
else if (pcount == (dcount + holepoint))
{
//If the dealer and player's scores are tied
resultField.setText("It's a Push.");
//The unhole module shows the dealer's hole card
//and adds the value to his hand score
unhole(dcount, holepoint);
}
else if (pcount < (dcount + holepoint))
{ //Checks if the player's score is less than the dealer's
if ((dcount + holepoint) > 21)
{ //Checks if the dealer has gone over 21
resultField.setText ("The Dealer has Busted. You Win!");
//This adds a win to the player's total
pwins += 1;
//This displays the player's new win total
playerWinField.setNumber (pwins);
//The unhole module shows the dealer's hole card
//and adds the value to his hand score
unhole(dcount, holepoint);
}
else
{ //This code runs if the dealer hasn't gone over 21
resultField.setText("You lose! The Dealer wins!");
//This adds a win to the dealer's total
dwins += 1;
//This displays the dealer's new win total
dealerWinField.setNumber (dwins);
//The unhole module shows the dealer's hole card
//and adds the value to his hand score
unhole(dcount, holepoint);
}
}
}
dealer = false;
}
else if (buttonObj == infoButton)
{ //This code runs when the Help button is clicked
if (!info)
{ //This checks to see if it is the first click of the Help button
//This code checks the state of the buttons so it can return them
//to the same state when the help button is clicked the second time
if (stayButton.isVisible()) stay = true;
else stay = false;
if (dealButton.isVisible()) del = true;
else del = false;
if (resetButton.isVisible()) rset = true;
else rset = false;
//This saves the hand information displayed in the text areas
tempD = dealerHandField.getText();
tempP = playerHandField.getText();
//This creates the information to display in
//the text areas when the Help button is clicked
helpText = "Click the DEAL button \nto start a hand. The object \nof the game is to get\nas close to 21 as you \ncan without going over.\nClick the HIT button to take \nanother card. Click the STAY \nbutton when you don't want \nanymore cards. \nThe first card dealt to the \ndealer is face down(Hole Card).\nIf the dealer's hand is 16 \nor less, he must take another\ncard. If his hand is worth \nmore than 16, he must stay.\nAn Ace can be worth 1 or 11. \nIt will be counted as 11 \nunless that would put you \nover 21. When the hand is \nfinished click the \nRESET button. \nThe program will keep track\nof your wins and \nthe dealer's wins.";
returnText = "Click the HELP button again \nto return to the game.";
//This displays the help files
playerHandField.setText(returnText);
dealerHandField.setText(helpText);
//This is the switch to tell the program the Help button has been clicked once
info = true;
//This hides all buttons except the Help button
disp(false, false, false);
}
else
{ //This code runs when the Help button is clicked the second time
//It restores the hand information so the game can resume
dealerHandField.setText(tempD);
playerHandField.setText(tempP);
//Reset the switch
info = false;
//Return the buttons to their original state
disp(stay, del, rset);
}
}
else
{ //This code runs when the Reset button is clicked
//Return all variables except the wins to their original state
pIsSoft = false;
dIsSoft = false;
pcount = 0;
dcount = 0;
card = "";
holepoint = 0;
playerScoreField.setNumber (pcount);
dealerScoreField.setNumber (dcount);
playerHandField.setText(card);
dealerHandField.setText(card);
resultField.setText (" ");
//Hide the Hit, Stay, and Reset buttons and show the deal button
disp(false, true, false);
}
}
private void deal(boolean dealer)
{ //This code creates the cards and assigns them to the correct variables
//This creates a random number between 1 and 13
point = (int)(1 + Math.random() * (13 - 1 + 1));
//This checks what the random number is and assigns a card name
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 of face cards to 10
if ((card == "Jack")||(card == "Queen")||(card == "King"))point = 10;
if (dealer)
{ //This code runs when it is the dealers turn
//This checks if the card is an Ace and whether it will be counted as 1 point or 11
if ((card == "Ace")&&((dcount + holepoint) <= 10))
{
point = 11;
dIsSoft = true;
}
//This checks if it is the first card dealt to the dealer and makes it the Hole Card
if (i == 0)
{
holepoint = point;
holecard = card;
point = 0;
card = "Hole Card";
}
//This adds the point value to the dealer's score
dcount += point;
//This checks for an Ace counting 11 if the dealer's score
//goes over 21 and sets it to 1 point if so
if ((dIsSoft == true)&&((dcount + holepoint) > 21))
{
dcount -= 10;
dIsSoft = false;
}
//This displays the dealer's new score
dealerScoreField.setNumber (dcount);
//This adds the card name to the dealer's hand and displays it
card = card + "\n";
dealerHandField.append(card);
}
else
{ //This code runs when it is the players turn
//This checks if the card is an Ace and whether it will be counted as 1 point or 11
if ((card == "Ace")&&(pcount <= 10))
{
point = 11;
pIsSoft = true;
}
//This adds the point value to the player's score
pcount += point;
//This checks for an Ace counting 11 if the player's score
//goes over 21 and sets it to 1 point if so
if ((pIsSoft == true)&&(pcount > 21))
{
pcount -= 10;
pIsSoft = false;
}
//This displays the player's new score
playerScoreField.setNumber (pcount);
//This adds the card name to the player's hand and displays it
card = card + "\n";
playerHandField.append(card);
}
}
private void unhole(int cnt, int hpnt)
{ //This code unhides the dealer's hole card
String tempstr;
//This assigns the dealer's hand display to a variable
tempstr = dealerHandField.getText();
//This cuts out the "Hole Card" from the string
tempstr = tempstr.substring(9);
//This adds the holecard to the string
tempstr = holecard + tempstr;
//This displays the hand with the hole card showing
dealerHandField.setText(tempstr);
//This adds the value of the holecard to the dealer's score
dealerScoreField.setNumber (cnt + hpnt);
//This hides all the buttons except the Reset and Help buttons
disp(false, false, true);
}
private void disp(boolean hit, boolean dl, boolean rst)
{ //This module hides or shows the buttons
hitButton.setVisible (hit);
stayButton.setVisible (hit);
dealButton.setVisible (dl);
resetButton.setVisible (rst);
}
}