POKER SOURCE CODE
// author: anthony f. ortiz program: poker.java
import java.applet.Applet;
import java.awt.*;
import java.math.*;
public class poker extends Applet
{
private imagelabel [] back_of_card = new imagelabel [5];
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 Label [] menu = new Label [10];
private Label [] hold = new Label [5];
private Button [] keep = new Button [5];
private Button deal;
private Label bet_label, win1, win2, counter1, counter2;
private TextField bet_textfield;
private int bet;
private int flag;
private int counter;
private int [] flags = new int [5];
private int [] hand_type = new int [5];
private int [] hand_suit = new int [5];
private int [] hand_type2 = new int [5];
private int [] hand_suit2 = new int [5];
public void init ()
{
load_images ();
load_labels ();
}
public void start ()
{
counter = 100;
counter2.setText ("" + counter);
counter2.invalidate ();
counter2.validate ();
erase_images ();
display_screen ();
initialize_values ();
}
// load the screen images.
public void load_images ()
{
for (int i = 0; i < 5; 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);
menu [0] = new Label (" 1 2 3 4 5 ");
menu [1] = new Label (" royal flush: 250 500 750 1000 4000");
menu [2] = new Label (" straight flush: 50 100 150 200 250");
menu [3] = new Label (" 4 of a kind: 25 50 75 100 125");
menu [4] = new Label (" full house: 9 18 27 36 45");
menu [5] = new Label (" flush: 6 12 18 24 30");
menu [6] = new Label (" straight: 4 8 12 16 20");
menu [7] = new Label (" 3 of a kind: 3 6 9 12 15");
menu [8] = new Label (" 2 pairs: 2 4 6 8 10");
menu [9] = new Label ("jacks or better: 1 2 3 4 5");
hold [0] = new Label ();
hold [1] = new Label ();
hold [2] = new Label ();
hold [3] = new Label ();
hold [4] = new Label ();
keep [0] = new Button ("hold");
keep [1] = new Button ("hold");
keep [2] = new Button ("hold");
keep [3] = new Button ("hold");
keep [4] = new Button ("hold");
deal = new Button ("deal");
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);
for (int i = 0; i < 10; i++)
{
menu [i].reshape (125, 5 + (i * 15), 400, 10);
add (menu [i]);
}
draw_backgrounds ();
for (int i = 0; i < 5; i++)
{
hold [i].reshape (25 + (i * 125), 275, 75, 10);
add (hold [i]);
}
for (int i = 0; i < 5; i++)
{
keep [i].reshape (25 + (i * 125), 300, 75, 25);
add (keep [i]);
}
deal.reshape (25, 350, 75, 25);
add (deal);
bet_label.reshape (150, 358, 75, 10);
add (bet_label);
bet_textfield.reshape (275, 350, 75, 25);
add (bet_textfield);
win1.reshape (400, 358, 65, 10);
add (win1);
win2.reshape (465, 358, 60 ,10);
add (win2);
counter1.reshape (525, 358, 65, 10);
add (counter1);
counter2.reshape (590, 358, 60, 10);
add (counter2);
}
// draw the backs of the playing cards.
public void draw_backgrounds ()
{
for (int i = 0; i < 5; i++)
{
back_of_card [i].reshape (25 + (i * 125), 175, 85, 100);
add (back_of_card [i]);
back_of_card [i].wait_for_image (true);
}
}
// deal the first 5 cards.
public void deal_cards ()
{
int type, suit, same_card = 0;
for (int i = 0; i < 5; i++)
{
do
{
type = (int) (Math.random () * (14 - 1));
suit = (int) (Math.random () * (5 - 1));
if ((hand_type [0] == type && hand_suit [0] == suit) ||
(hand_type [1] == type && hand_suit [1] == suit) ||
(hand_type [2] == type && hand_suit [2] == suit) ||
(hand_type [3] == type && hand_suit [3] == suit) ||
(hand_type [4] == type && hand_suit [4] == suit))
{
same_card = 1;
}
else
{
same_card = 0;
}
} while (same_card == 1);
hand_type [i] = type;
hand_type2 [i] = type;
hand_suit [i] = suit;
hand_suit2 [i] = suit;
remove (back_of_card [i]);
cards [type][suit].reshape (25 + (i * 125), 175, 75, 100);
add (cards [type][suit]);
cards [type][suit].wait_for_image (true);
}
}
// deal new cards (unheld cards).
public void draw_cards ()
{
int type, suit, same_card = 0;
for (int i = 0; i < 5; i++)
{
if (hold [i].getText () == " ")
{
do
{
type = (int) (Math.random () * (14 - 1));
suit = (int) (Math.random () * (5 - 1));
if ((hand_type2 [0] == type && hand_suit2 [0] == suit) ||
(hand_type2 [1] == type && hand_suit2 [1] == suit) ||
(hand_type2 [2] == type && hand_suit2 [2] == suit) ||
(hand_type2 [3] == type && hand_suit2 [3] == suit) ||
(hand_type2 [4] == type && hand_suit2 [4] == suit) ||
(hand_type [0] == type && hand_suit [0] == suit) ||
(hand_type [1] == type && hand_suit [1] == suit) ||
(hand_type [2] == type && hand_suit [2] == suit) ||
(hand_type [3] == type && hand_suit [3] == suit) ||
(hand_type [4] == type && hand_suit [4] == suit))
{
same_card = 1;
}
else
{
same_card = 0;
}
} while (same_card == 1);
hand_type [i] = type;
hand_suit [i] = suit;
remove (cards [hand_type2 [i]][hand_suit2 [i]]);
cards [type][suit].reshape (25 + (i * 125), 175, 75, 100);
cards [type][suit].wait_for_image (true);
add (cards [type][suit]);
}
}
}
// determine the player's hand.
public void determine_hand ()
{
int [] winner1 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int [] winner2 = {0, 0, 0, 0};
int [] flags2 = {0, 0, 0, 0, 0, 0, 0};
int pay = 0;
counter = counter - bet;
// determine 2, 3, or 4 of a kind.
for (int i = 0; i < 5; i++)
{
switch (hand_type [i])
{
case 0: winner1 [0] = winner1 [0] + 1;
break;
case 1: winner1 [1] = winner1 [1] + 1;
break;
case 2: winner1 [2] = winner1 [2] + 1;
break;
case 3: winner1 [3] = winner1 [3] + 1;
break;
case 4: winner1 [4] = winner1 [4] + 1;
break;
case 5: winner1 [5] = winner1 [5] + 1;
break;
case 6: winner1 [6] = winner1 [6] + 1;
break;
case 7: winner1 [7] = winner1 [7] + 1;
break;
case 8: winner1 [8] = winner1 [8] + 1;
break;
case 9: winner1 [9] = winner1 [9] + 1;
break;
case 10: winner1 [10] = winner1 [10] + 1;
break;
case 11: winner1 [11] = winner1 [11] + 1;
break;
case 12: winner1 [12] = winner1 [12] + 1;
break;
}
}
// determine a flush.
for (int i = 0; i < 5; i++)
{
switch (hand_suit [i])
{
case 0: winner2 [0] = winner2 [0] + 1;
break;
case 1: winner2 [1] = winner2 [1] + 1;
break;
case 2: winner2 [2] = winner2 [2] + 1;
break;
case 3: winner2 [3] = winner2 [3] + 1;
break;
}
}
// 2, 3, 4 or a kind and pairs.
for (int i = 0; i < 13; i++)
{
// any 2 of a kind or 2 pairs.
if (winner1 [i] == 2)
{
flags2 [0] = flags2 [0] + 1;
}
// jacks or better, 3 of a kind, or 4 of a kind.
if (winner1 [i] == 2 && i >= 9)
{
flags2 [1] = flags2 [1] + 1;
}
else if (winner1 [i] == 3)
{
flags2 [2] = flags2 [2] + 1;
}
else if (winner1 [i] == 4)
{
flags2 [3] = flags2 [3] + 1;
}
}
// a straight or part of a straight flush.
for (int i = 0; i < 9; i++)
{
if (winner1 [i + 0] == 1 && winner1 [i + 1] == 1 &&
winner1 [i + 2] == 1 && winner1 [i + 3] == 1 &&
winner1 [i + 4] == 1 && i == 8)
{
flags2 [4] = flags2 [4] + 1;
}
else if (winner1 [i + 0] == 1 && winner1 [i + 1] == 1 &&
winner1 [i + 2] == 1 && winner1 [i + 3] == 1 &&
winner1 [i + 4] == 1 && i < 8 )
{
flags2 [5] = flags2 [5] + 1;
}
}
if (winner1 [0] == 1 && winner1 [1] == 1 && winner1 [2] == 1 &&
winner1 [3] == 1 && winner1 [12] == 1)
{
flags2 [5] = flags2 [5] + 1;
}
// a flush.
for (int i = 0; i < 4; i++)
{
if (winner2 [i] == 5)
{
flags2 [6] = flags2 [6] + 1;
}
}
// pay the winner (bet * odds).
if (flags2 [6] == 1 && flags2 [4] == 1) // royal flush.
{
if (bet < 5)
{
pay = bet * 250;
}
else
{
pay = bet * 800;
}
}
else if (flags2 [6] == 1 && flags2 [5] == 1) // straight flush.
{
pay = bet * 50;
}
else if (flags2 [3] == 1) // 4 of a kind.
{
pay = bet * 25;
}
else if (flags2 [0] == 1 && flags2 [2] == 1) // full house.
{
pay = bet * 9;
}
else if (flags2 [6] == 1) // flush.
{
pay = bet * 6;
}
else if (flags2 [4] == 1 || flags2 [5] == 1) // straight.
{
pay = bet * 4;
}
else if (flags2 [2] == 1) // 3 of a kind.
{
pay = bet * 3;
}
else if (flags2 [0] == 2) // 2 pairs.
{
pay = bet * 2;
}
else if (flags2 [1] == 1) // jacks of better.
{
pay = bet * 1;
}
else
{
pay = 0;
}
counter = counter + pay;
win2.setText ("" + pay);
win2.invalidate ();
win2.validate ();
counter2.setText ("" + counter);
counter2.invalidate ();
counter2.validate ();
}
// initialize values.
public void initialize_values ()
{
bet = 1;
flag = 0;
for (int i = 0; i < 5; i++)
{
hand_type [i] = hand_type2 [i] = -1;
hand_suit [i] = hand_type2 [i] = -1;
flags [i] = 0;
}
for (int i = 0; i < 5; i++)
{
hold [i].setText (" ");
hold [i].invalidate ();
hold [i].validate ();
flags [i] = 0;
}
bet_textfield.enable (true);
bet_textfield.setText ("1");
win2.setText ("");
win2.invalidate ();
win2.validate ();
}
// erase screen images.
public void erase_images ()
{
if (hand_type [0] != -1)
{
for (int i = 0; i < 5; i++)
{
remove (cards [hand_type [i]][hand_suit [i]]);
}
}
}
// deal, draw, or start new game. also determine which cards will be held.
public boolean action (Event event, Object object)
{
if (event.target == deal && flag == 0)
{
flag = 1;
bet_textfield.enable (false);
deal_cards ();
return (true);
}
else if (event.target == deal && flag == 1)
{
flag = 2;
draw_cards ();
determine_hand ();
return (true);
}
else if (event.target == deal && flag == 2)
{
erase_images ();
draw_backgrounds ();
initialize_values ();
return (true);
}
for (int i = 0; i < 5; i++)
{
if (event.target == keep [i] && flag == 1 && flags [i] == 0)
{
flags [i] = 1;
hold [i].setAlignment (Label.CENTER);
hold [i].setText ("hold");
hold [i].invalidate ();
hold [i].validate ();
return (true);
}
else if (event.target == keep [i] && flag == 1 && flags [i] == 1)
{
flags [i] = 0;
hold [i].setText (" ");
hold [i].invalidate ();
hold [i].validate ();
return (true);
}
}
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 POKER APPLET.