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 java.awt.event.*;
import java.applet.*;
import java.lang.Object.*;
public class BlackJack extends Applet
implements ActionListener
{
//Define the GUI elements
private Label titleBlackJackLabel;
private Label dealerScoreLabel;
private Label dealerHandLabel;
private TextField dealerScoreField;
private TextArea dealerHandField;
private Label dealerWinLabel;
private TextField dealerWinField;
private Label playerScoreLabel;
private Label playerHandLabel;
private TextField playerScoreField;
private TextArea playerHandField;
private Label playerWinLabel;
private TextField playerWinField;
private Label resultLabel;
private TextField resultField;
private Button dealButton;
private Button resetButton;
private Button hitButton;
private Button stayButton;
private Button infoButton;
//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;
String[] deck = new String[53];
public void init()
{
// Instantiate the window objects
titleBlackJackLabel = new Label ("Black Jack Java Game");
dealerScoreLabel = new Label ("Dealer's Score");
dealerHandLabel = new Label ("Dealer's Hand");
dealerScoreField = new TextField (" ", 5);
dealerHandField = new TextArea (5,27);
dealerWinLabel = new Label ("Dealer's Wins");
dealerWinField = new TextField (" ", 5);
playerScoreLabel = new Label ("Your Score");
playerHandLabel = new Label ("Your Hand");
playerScoreField = new TextField (" ", 5);
playerHandField = new TextArea (5,27);
playerWinLabel = new Label ("Your Wins");
playerWinField = new TextField (" ", 5);
resultLabel = new Label ("Result:");
resultField = new TextField (" ", 27);
dealButton = new Button ("Deal");
resetButton = new Button ("Reset");
hitButton = new Button ("Hit");
stayButton = new Button ("Stay");
infoButton = new Button ("Help");
// Instantiate and set a grid bag layout
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
// Add the window objects to the layout.
// row,col,width,height
addComponent(layout, titleBlackJackLabel,1,1,2,1);
addComponent(layout, dealerScoreLabel ,2,1,1,1);
addComponent(layout, dealerHandLabel ,2,2,1,1);
addComponent(layout, dealerScoreField ,3,1,1,1);
addComponent(layout, dealerHandField ,3,2,2,3);
addComponent(layout, dealerWinLabel ,4,1,1,1);
addComponent(layout, dealerWinField ,5,1,1,1);
addComponent(layout, playerScoreLabel ,6,1,1,1);
addComponent(layout, playerHandLabel ,6,2,1,1);
addComponent(layout, playerScoreField ,7,1,1,1);
addComponent(layout, playerHandField ,7,2,2,3);
addComponent(layout, playerWinLabel ,8,1,1,1);
addComponent(layout, playerWinField ,9,1,1,1);
addComponent(layout, resultLabel ,10,1,1,1);
addComponent(layout, resultField ,10,2,1,1);
addComponent(layout, dealButton ,11,1,1,1);
addComponent(layout, resetButton ,11,2,1,1);
addComponent(layout, hitButton ,12,1,1,1);
addComponent(layout, stayButton ,12,2,1,1);
addComponent(layout, infoButton ,13,1,1,1);
dealButton.addActionListener (this);
resetButton.addActionListener (this);
hitButton.addActionListener (this);
stayButton.addActionListener (this);
infoButton.addActionListener (this);
}
// Add a component to the layout in the indicated row and column
// with the indicated height and width.
private void addComponent(GridBagLayout layout, Component component, int row, int col, int width, int height)
{
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets.bottom = 2;
constraints.insets.top = 2;
constraints.insets.left = 2;
constraints.insets.right = 2;
constraints.gridx = col;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
add (component);
}
public void actionPerformed (ActionEvent e)
{
Button b = (Button)e.getSource();
//Test to see which button was clicked
if (b == dealButton)
{ //This code runs if the Deal button is clicked
//Initialize the deck array items
deck[1] = "Ace_diamonds";
deck[2] = "Ace_clubs";
deck[3] = "Ace_hearts";
deck[4] = "Ace_spades";
deck[5] = "Two_diamonds";
deck[6] = "Two_clubs";
deck[7] = "Two_hearts";
deck[8] = "Two_spades";
deck[9] = "Three_diamonds";
deck[10] = "Three_clubs";
deck[11] = "Three_hearts";
deck[12] = "Three_spades";
deck[13] = "Four_diamonds";
deck[14] = "Four_clubs";
deck[15] = "Four_hearts";
deck[16] = "Four_spades";
deck[17] = "Five_diamonds";
deck[18] = "Five_clubs";
deck[19] = "Five_hearts";
deck[20] = "Five_spades";
deck[21] = "Six_diamonds";
deck[22] = "Six_clubs";
deck[23] = "Six_hearts";
deck[24] = "Six_spades";
deck[25] = "Seven_diamonds";
deck[26] = "Seven_clubs";
deck[27] = "Seven_hearts";
deck[28] = "Seven_spades";
deck[29] = "Eight_diamonds";
deck[30] = "Eight_clubs";
deck[31] = "Eight_hearts";
deck[32] = "Eight_spades";
deck[33] = "Nine_diamonds";
deck[34] = "Nine_clubs";
deck[35] = "Nine_hearts";
deck[36] = "Nine_spades";
deck[37] = "Ten_diamonds";
deck[38] = "Ten_clubs";
deck[39] = "Ten_hearts";
deck[40] = "Ten_spades";
deck[41] = "Jack_diamonds";
deck[42] = "Jack_clubs";
deck[43] = "Jack_hearts";
deck[44] = "Jack_spades";
deck[45] = "Queen_diamonds";
deck[46] = "Queen_clubs";
deck[47] = "Queen_hearts";
deck[48] = "Queen_spades";
deck[49] = "King_diamonds";
deck[50] = "King_clubs";
deck[51] = "King_hearts";
deck[52] = "King_spades";
//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);
deal(dealer);
}
if (pcount == 21)
{
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
{
//Display the result message
resultField.setText("You Have BlackJack!");
//This adds a win to the player's total
pwins += 1;
//This displays the player's new win total
playerWinField.setText ("" + pwins);
//The unhole module shows the dealer's hole card
//and adds the value to his hand score
unhole(dcount, holepoint);
}
}
else if ((dcount + holepoint) == 21)
{
resultField.setText("The Dealer has BlackJack!");
//This adds a win to the dealer's total
dwins += 1;
//This displays the dealer's new win total
dealerWinField.setText (" " + dwins);
//The unhole module shows the dealer's hole card
//and adds the value to his hand score
unhole(dcount, holepoint);
}
else
{
//The disp module hides or shows the buttons as appropriate
disp(true, false, false);
}
}
else if (b == hitButton)
{ //This code runs when the Hit button is clicked
//dealer is a boolean variable that determines if it is the player's turn or the dealer's
dealer = false;
//Deal a card to the player
deal(dealer);
if (pcount == 21)
{
compare();
}
//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.setText ("" + dwins);
//The unhole module shows the dealer's hole card and adds the value to his hand score
unhole(dcount, holepoint);
}
}
else if (b == stayButton)
{ //This code runs when the Stay button is clicked
compare();
}
else if (b == 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.setText ("" + pcount);
dealerScoreField.setText ("" + 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 player)
{ //This code creates the cards and assigns them to the correct variables
//Declare local variable
int n;
//Run a do/while loop to check if a card has already been used
do
{
//This creates a random number between 1 and 52
n = (int)(1 + Math.random() * (52 - 1 + 1));
//This assigns point values based on the index number of the deck array
if ((n > 0) && (n < 5)) point = 1;
else if ((n > 4) && (n < 9)) point = 2;
else if ((n > 8) && (n < 13)) point = 3;
else if ((n > 12) && (n < 17)) point = 4;
else if ((n > 16) && (n < 21)) point = 5;
else if ((n > 20) && (n < 25)) point = 6;
else if ((n > 24) && (n < 29)) point = 7;
else if ((n > 28) && (n < 33)) point = 8;
else if ((n > 32) && (n < 37)) point = 9;
else if ((n > 36) && (n <= 52)) point = 10;
//This sets the value of the card variable to the random deck index value
card = deck[n];
}
//This checks if the card has already been used and if so loops
//back to pick another random number for n
while (card == " ");
//This sets the value of the used card to an empty string
deck[n] = " ";
if (player)
{ //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 ((point == 1)&&((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.setText ("" + dcount);
//This adds the card name to the dealer's hand and displays it
card = card + "\n";
dealerHandField.append(card);
dealer = false;
}
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 ((point == 1)&&(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.setText ("" + pcount);
//This adds the card name to the player's hand and displays it
card = card + "\n";
playerHandField.append(card);
dealer = true;
}
}
private void unhole(int cnt, int hpnt)
{ //This code unhides the dealer's hole card
//Declare local variable
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);
cnt += hpnt;
//This adds the value of the holecard to the dealer's score
dealerScoreField.setText ("" + cnt);
//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);
}
private void compare()
{
//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.setText ("" + 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.setText ("" + 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.setText ("" + dwins);
//The unhole module shows the dealer's hole card
//and adds the value to his hand score
unhole(dcount, holepoint);
}
}
}
}
}