BLACKJACK SOURCE CODE
// author: anthony f. ortiz program: blackjack.java
import java.applet.Applet;
import java.awt.*;
import java.math.*;
public class blackjack extends Applet
{
private imagelabel [] back_of_card = new imagelabel [4];
private imagelabel [][] cards = new imagelabel [13][4];
private String [] type = {"2", "3", "4", "5", "6", "7", "8", "9", "t", "j",
"q", "k", "a"};
private String [] suit = {"c", "d", "h", "s"};
private Button deal, hit, stay;
private Label bet_label, win1, win2, counter1, counter2;
private Label your_status, dealer_status, who_won;
private TextField bet_textfield;
private int [] user_type = new int [10];
private int [] user_suit = new int [10];
private int [] dealer_type = new int [10];
private int [] dealer_suit = new int [10];
private int [] totals = new int [2];
private int user_cards, dealer_cards;
private int flag, bet, pay, counter;
public void init ()
{
load_images ();
load_labels ();
}
public void start ()
{
counter = 100;
counter2.setText ("" + counter);
counter2.invalidate ();
counter2.validate ();
erase_screen ();
display_screen ();
initialize_values ();
}
// load the screen images.
public void load_images ()
{
for (int i = 0; i < 4; i++)
{
back_of_card [i] = new imagelabel (getCodeBase (), "e.gif");
back_of_card [i].wait_for_image (true);
}
for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 4; j++)
{
String card = new String (type [i] + suit [j] + ".gif");
cards [i][j] = new imagelabel (getCodeBase (), card);
cards [i][j].wait_for_image (true);
}
}
}
// load the screen components (buttons, labels, etc).
public void load_labels ()
{
Color color = new Color (0, 128, 0);
setBackground (color);
your_status = new Label ();
dealer_status = new Label ();
who_won = new Label ();
deal = new Button ("deal");
hit = new Button ("hit");
stay = new Button ("stay");
bet_label = new Label ("bet (1-5):");
bet_textfield = new TextField ();
win1 = new Label ("you won: ");
win2 = new Label ();
counter1 = new Label ("total: ");
counter2 = new Label ();
}
// display the initial screen.
public void display_screen ()
{
Font font = new Font ("Courier", Font.BOLD, 12);
setFont (font);
setLayout (null);
draw_backgrounds ();
dealer_status.reshape (5, 118, 250, 10);
add (dealer_status);
your_status.reshape (5, 318, 250, 10);
add (your_status);
who_won.reshape (5, 150, 250, 10);
add (who_won);
deal.reshape (5, 340, 75, 25);
add (deal);
hit.reshape (110, 340, 75, 25);
add (hit);
stay.reshape (215, 340, 75, 25);
add (stay);
bet_label.reshape (5, 382, 75,10);
add (bet_label);
bet_textfield.reshape (110, 377, 75, 25);
add (bet_textfield);
win1.reshape (215, 382, 75, 10);
add (win1);
win2.reshape (290, 382, 75, 10);
add (win2);
counter1.reshape (365, 382, 75, 10);
add (counter1);
counter2.reshape (440, 382, 75, 10);
add (counter2);
}
// draw the backs of the playing cards.
public void draw_backgrounds ()
{
int count = 0;
for (int j = 0; j < 2; j++)
{
for (int i = 0; i < 2; i++)
{
back_of_card [count].reshape (5 + (i * 105), 5 + (j * 200),
75, 100);
add (back_of_card [count]);
back_of_card [count].wait_for_image (true);
count++;
}
}
}
// deal cards to the opponent of the dealer.
public void deal_user ()
{
int type, suit, same_card = 0;
for (int i = 0; i < 2; i++)
{
user_cards++;
do
{
type = (int) (Math.random () * (14 - 1));
suit = (int) (Math.random () * (5 - 1));
same_card = card_already_used (type, suit);
} while (same_card == 1);
user_type [i] = type;
user_suit [i] = suit;
remove (back_of_card [i + 2]);
cards [type][suit].reshape (5 + (i * 105), 5 + 200, 75, 100);
add (cards [type][suit]);
cards [type][suit].wait_for_image (true);
}
}
// deal cards to the dealer.
public void deal_dealer ()
{
int type, suit, same_card = 0;
for (int i = 0; i < 1; i++)
{
dealer_cards++;
do
{
type = (int) (Math.random () * (14 - 1));
suit = (int) (Math.random () * (5 - 1));
same_card = card_already_used (type, suit);
} while (same_card == 1);
dealer_type [i] = type;
dealer_suit [i] = suit;
remove (back_of_card [1]);
cards [type][suit].reshape (5 + 105, 5, 75, 100);
add (cards [type][suit]);
cards [type][suit].wait_for_image (true);
}
}
// hit the dealer's opponent.
public void hit_user ()
{
int type, suit, same_card = 0;
for (int i = user_cards; i < user_cards + 1 ; i++)
{
do
{
type = (int) (Math.random () * (14 - 1));
suit = (int) (Math.random () * (5 - 1));
same_card = card_already_used (type, suit);
} while (same_card == 1);
user_type [i] = type;
user_suit [i] = suit;
cards [type][suit].reshape (5 + (i * 105), 5 + 200, 75, 100);
add (cards [type][suit]);
cards [type][suit].wait_for_image (true);
}
user_cards++;
}
// show dealer's other card.
public void show_dealer_card ()
{
int type, suit, same_card = 0;
for (int i = 1; i < 2 ; i++)
{
dealer_cards++;
do
{
type = (int) (Math.random () * (14 - 1));
suit = (int) (Math.random () * (5 - 1));
same_card = card_already_used (type, suit);
} while (same_card == 1);
dealer_type [i] = type;
dealer_suit [i] = suit;
remove (back_of_card [0]);
cards [type][suit].reshape (5, 5, 75, 100);
add (cards [type][suit]);
cards [type][suit].wait_for_image (true);
}
}
// hit the dealer.
public void hit_dealer ()
{
int type, suit, same_card = 0;
for (int i = dealer_cards; i < dealer_cards + 1 ; i++)
{
do
{
type = (int) (Math.random () * (14 - 1));
suit = (int) (Math.random () * (5 - 1));
same_card = card_already_used (type, suit);
} while (same_card == 1);
dealer_type [i] = type;
dealer_suit [i] = suit;
cards [type][suit].reshape (5 + (i * 105), 5, 75, 100);
add (cards [type][suit]);
cards [type][suit].wait_for_image (true);
}
dealer_cards++;
}
// calculate the user's or dealer's hand.
public void calculate_hand (int who, int number_cards, int [] hand)
{
int ace_count = 0, ace_count2 = 0;
totals [who] = 0;
for (int i = 0; i < number_cards; i++)
{
switch (hand [i])
{
case 0: totals [who] = totals [who] + 2;
break;
case 1: totals [who] = totals [who] + 3;
break;
case 2: totals [who] = totals [who] + 4;
break;
case 3: totals [who] = totals [who] + 5;
break;
case 4: totals [who] = totals [who] + 6;
break;
case 5: totals [who] = totals [who] + 7;
break;
case 6: totals [who] = totals [who] + 8;
break;
case 7: totals [who] = totals [who] + 9;
break;
case 8: totals [who] = totals [who] + 10;
break;
case 9: totals [who] = totals [who] + 10;
break;
case 10: totals [who] = totals [who] + 10;
break;
case 11: totals [who] = totals [who] + 10;
break;
case 12: totals [who] = totals [who] + 11;
ace_count++;
break;
}
}
ace_count2 = ace_count;
for (int i = 0; i < ace_count2; i++)
{
if (ace_count > 0 && totals [who] > 21)
{
totals [who] = totals [who] - 10;
ace_count = ace_count - 1;
}
}
}
// determine who won.
public void determine_winner ()
{
if (totals [0] > 21)
{
deal.enable (true);
hit.enable (false);
stay.enable (false);
show_dealer_card ();
calculate_hand (1, dealer_cards, dealer_type);
dealer_status.setText ("dealer has " + totals [1] + ".");
dealer_status.invalidate ();
dealer_status.validate ();
your_status.setText ("you have " + totals [0] + ". you busted.");
your_status.invalidate ();
your_status.validate ();
who_won.setText ("dealer won");
who_won.invalidate ();
who_won.validate ();
pay = 0;
counter = counter - bet;
update_counter ();
}
else if (totals [1] > 21)
{
dealer_status.setText ("dealer has " + totals [1] +
". dealer busted.");
dealer_status.invalidate ();
dealer_status.validate ();
who_won.setText ("you won.");
who_won.invalidate ();
who_won.validate ();
pay = bet * 2;
counter = counter + pay - bet;
}
else if (totals [1] >= totals [0])
{
dealer_status.setText ("dealer has " + totals [1] + ".");
dealer_status.invalidate ();
dealer_status.validate ();
who_won.setText ("dealer won.");
who_won.invalidate ();
who_won.validate ();
pay = 0;
counter = counter - bet;
}
else if (totals [0] > totals [1] && totals [1] >= 17)
{
dealer_status.setText ("dealer has " + totals [1] + ".");
dealer_status.invalidate ();
dealer_status.validate ();
who_won.setText ("you won.");
who_won.invalidate ();
who_won.validate ();
pay = bet * 2;
counter = counter + pay - bet;
}
}
// determine if this card is already being used.
public int card_already_used (int type, int suit)
{
int same_card = 0;
for (int i = 0; i < user_cards; i++)
{
if (user_type [i] == type && user_suit [i] == suit)
{
return (same_card = 1);
}
else
{
same_card = 0;
}
}
for (int i = 0; i < dealer_cards; i++)
{
if (dealer_type [i] == type && dealer_suit [i] == suit)
{
return (same_card = 1);
}
else
{
same_card = 0;
}
}
return same_card;
}
// initialize values.
public void initialize_values ()
{
bet = 1;
flag = 0;
user_cards = 0;
dealer_cards = 0;
totals [0] = 0;
totals [1] = 0;
for (int i = 0; i < 10; i++)
{
user_type [i] = -1;
user_suit [i] = -1;
dealer_type [i] = -1;
dealer_suit [i] = -1;
}
deal.enable (true);
hit.enable (false);
stay.enable (false);
your_status.setText ("");
your_status.invalidate ();
your_status.validate ();
dealer_status.setText ("");
dealer_status.invalidate ();
dealer_status.validate ();
who_won.setText ("");
who_won.invalidate ();
who_won.validate ();
bet_textfield.enable (true);
bet_textfield.setText ("" + bet);
win2.setText ("");
win2.invalidate ();
win2.validate ();
}
// erase the images from the screen.
public void erase_screen ()
{
for (int i = 0; i < user_cards; i++)
{
remove (cards [user_type [i]][user_suit [i]]);
}
for (int i = 0; i < dealer_cards; i++)
{
remove (cards [dealer_type [i]][dealer_suit [i]]);
}
}
// show the user's winning/losings and the new counter.
public void update_counter ()
{
win2.setText ("" + pay);
win2.invalidate ();
win2.validate ();
counter2.setText ("" + counter);
counter2.invalidate ();
counter2.validate ();
}
// deal, hit, or stay.
public boolean action (Event event, Object object)
{
if (event.target == deal && flag == 0)
{
flag = 1;
deal.enable (false);
hit.enable (true);
stay.enable (true);
bet_textfield.enable (false);
deal_user ();
deal_dealer ();
calculate_hand (0, user_cards, user_type);
return (true);
}
else if (event.target == deal && flag == 1)
{
erase_screen ();
draw_backgrounds ();
initialize_values ();
return (true);
}
else if (event.target == hit)
{
hit_user ();
calculate_hand (0, user_cards, user_type);
determine_winner ();
return (true);
}
else if (event.target == stay)
{
hit.enable (false);
stay.enable (false);
deal.enable (true);
your_status.setText ("you have " + totals [0] + ".");
your_status.invalidate ();
your_status.validate ();
show_dealer_card ();
calculate_hand (1, dealer_cards, dealer_type);
do
{
if (totals [1] < 17 && totals [1] < totals [0])
{
hit_dealer ();
calculate_hand (1, dealer_cards, dealer_type);
}
}
while (totals [1] < 17 && totals [1] < totals [0]);
determine_winner ();
update_counter ();
return (true);
}
else
{
return (false);
}
}
// make a bet from 0 to 5 coins.
public boolean keyUp (Event event, int key)
{
if (event.target == bet_textfield)
{
if (key > 53 || key < 49)
{
bet_textfield.setText ("1");
}
else
{
bet = key - 48;
bet_textfield.setText (bet + "");
}
return (true);
}
else
{
return (false);
}
}
}
// use this class for the card images used in poker class.
import java.awt.*;
import java.net.*;
// similar to label component.
public class imagelabel extends Canvas
{
private Image image;
private static String default_image_string
= "http://java.sun.com/lib/images/" +
"logo.java.color-transp.55x60.gif";
private String image_string = "default image string";
private boolean debug = false;
private int border = 0;
private Color border_color = null;
private int width, height;
private boolean explicit_size = false;
private int explicit_width = 0, explicit_height = 0;
private MediaTracker tracker;
private static int last_tracker_id = 0;
private int current_tracker_id;
private boolean done_loading = false;
private Container parent_container;
// this creates a image label from the file specified by the default
// string
public imagelabel ()
{
this (default_image_string);
}
// this create a image label from the specified file, which should be
// in gif or jpeg format.
public imagelabel (String image_url_string)
{
this (make_url (image_url_string));
}
// similar to above constructor.
public imagelabel (URL image_url)
{
this (load_image (image_url));
image_string = image_url.toExternalForm ();
}
// similar to above constructor.
public imagelabel (URL image_directory, String file)
{
this (make_url (image_directory, file));
image_string = file;
}
// turn preexisting image into image label.
public imagelabel (Image image)
{
this.image = image;
tracker = new MediaTracker (this);
current_tracker_id = last_tracker_id++;
tracker.addImage (image, current_tracker_id);
}
// this starts loading the image. it won't return until image is
// fully loaded.
public void wait_for_image (boolean do_layout)
{
if (!done_loading)
{
debug ("resizing and waiting for " + image_string);
try
{
tracker.waitForID (current_tracker_id);
}
catch (InterruptedException ie)
{
}
catch (Exception e)
{
System.out.println ("error loading " + image_string + ": "
+ e.getMessage());
e.printStackTrace ();
}
if (tracker.isErrorID (0))
new Throwable ("error loading image " +
image_string).printStackTrace();
done_loading = true;
if (explicit_width != 0)
width = explicit_width;
else
width = image.getWidth (this) + 2 * border;
if (explicit_height != 0)
height = explicit_height;
else
height = image.getHeight(this) + 2 * border;
resize (width, height);
debug (image_string + " is " + width + "x" + height + ".");
if (((parent_container = getParent ()) != null) && do_layout)
{
setBackground (parent_container.getBackground ());
parent_container.layout ();
}
}
}
// this moves the image label so that its center is at (x, y).
public void center_it (int x, int y)
{
debug ("centering" + image_string + " at (" + x + "," + y + ")");
move (x - width/2, y - height/2);
}
// determine if point (x, y) is inside the image label's
// coordinate system.
public synchronized boolean inside (int x, int y)
{
return ((x >= 0) && (x <= width) && (y >= 0) && (y <= height));
}
// draw the image.
public void paint (Graphics g)
{
if (!done_loading)
wait_for_image (true);
else
{
if (explicit_size)
g.drawImage (image, border, border, width - 2 * border,
height - 2 * border, this);
else
g.drawImage(image, border, border, this);
draw_rect (g, 0, 0, width - 1, height - 1, border, border_color);
}
}
// calculate the usual size allocated for the component.
public Dimension preferred_size ()
{
if (!done_loading)
wait_for_image (false);
return (super.preferredSize());
}
// calculate the smallest size allocated for the component.
public Dimension minimum_size ()
{
if (!done_loading)
wait_for_image (false);
return (super.minimumSize ());
}
// resize the image label.
public void resize (int width, int height)
{
if (!done_loading) {
explicit_size = true;
if (width > 0)
explicit_width = width;
if (height > 0)
explicit_height = height;
}
super.resize (width, height);
}
// similar to the above function.
public void reshape (int x, int y, int width, int height)
{
if (!done_loading)
{
explicit_size = true;
if (width > 0)
explicit_width = width;
if (height > 0)
explicit_height = height;
}
super.reshape(x, y, width, height);
}
// draws the border of the image label.
protected void draw_rect (Graphics g, int left, int top, int width,
int height, int line_thickness,
Color rectangle_color)
{
g.setColor(rectangle_color);
for (int i = 0; i < line_thickness; i++)
{
g.drawRect (left, top, width, height);
if (i < line_thickness - 1)
{
left = left + 1;
top = top + 1;
width = width - 2;
height = height - 2;
}
}
}
// prints out a string if debug is true, otherwise it does nothing.
protected void debug (String message)
{
if (debug)
System.out.println (message);
}
// creates the url.
private static URL make_url (String s)
{
URL u = null;
try
{
u = new URL (s);
}
catch (MalformedURLException mue)
{
System.out.println ("bad url " + s + ": " + mue);
mue.printStackTrace ();
}
return(u);
}
// similar to the above function.
private static URL make_url (URL directory, String file)
{
URL u = null;
try
{
u = new URL (directory, file);
}
catch (MalformedURLException mue)
{
System.out.println ("bad url " + directory.toExternalForm() +
", " + file + ": " + mue);
mue.printStackTrace ();
}
return (u);
}
// load the image used by the image label.
private static Image load_image (URL url)
{
return (Toolkit.getDefaultToolkit ().getImage (url));
}
// retrieve the image used in the image label.
public Image get_image ()
{
return (image);
}
// specifies the size of the border around the image label.
public int get_border ()
{
return (border);
}
// retrieves the size of the border around the image label.
public void set_border (int border)
{
this.border = border;
}
// specifies the color of the image label's border.
public Color get_border_color ()
{
return (border_color);
}
// retrieves the color of the image label's border.
public void set_border_color (Color border_color)
{
this.border_color = border_color;
}
// retrieves the width the image label.
public int get_width ()
{
return (width);
}
// retrieves the height of the image label.
public int get_height ()
{
return (height);
}
// this determines whether the programmer called resize or reshape
// on the image label before it was shown.
protected boolean has_explicit_size ()
{
return (explicit_size);
}
// retrieve the default image file.
public static String get_default_image_string ()
{
return (default_image_string);
}
// specify the default image file.
public static void set_default_image_string (String file)
{
default_image_string = file;
}
// this will show the file name if your created the image label from a
// a file.
protected String get_image_string ()
{
return (image_string);
}
// returns status of debugging flag.
public boolean is_debugging ()
{
return (debug);
}
// sets the status of the debugging flag.
public void set_is_debugging (boolean debug)
{
this.debug = debug;
}
}
// you can find the card images inside my directory. go to my directory
// "http://members.aol.com/afo2871/. the first lines of source code will
// tell you the names of each card. then, just bring each card up one
// by one and copy them. for example, the king of clubs can be retrieved
// with the url "http://members.aol.afo2871/kc.gif/.
BACK TO BLACKJACK APPLET.