CRAPS SOURCE CODE
// author: anthony f. ortiz program: craps.java import java.applet.Applet; import java.awt.*; import java.math.*; public class craps extends Applet { Label bet_label, win1, win2, counter1, counter2, instructions; TextField bet_textfield; Button roll_em, reset; int bet, pay, counter, flag, last; int dice [] = new int [2]; public void init () { load_components (); } public void start () { counter = 100; counter2.setText ("" + counter); counter2.invalidate (); counter2.validate (); Color color = new Color (0, 128, 0); setBackground (color); display_components (); initialize_values (); } // paint the initial board (two dice). public void paint (Graphics g) { g.setColor (Color.red); g.fillRect (245, 215, 50, 50); g.fillRect (345, 215, 50, 50); Color color = new Color (0, 128, 0); g.setColor (color); g.setColor (Color.black); show_dice (); getGraphics ().setColor (color); } // load components (buttons, labels, textfields). public void load_components () { bet_label = new Label ("bet 1-5:"); win1 = new Label ("you won:"); win2 = new Label (""); counter1 = new Label ("counter:"); counter2 = new Label (""); bet_textfield = new TextField (""); roll_em = new Button ("roll'em"); reset = new Button ("reset"); instructions = new Label ("", Label.CENTER); } // display components (buttons, labels, textfields). public void display_components () { setLayout (null); bet_label.reshape (5, 120, 75, 10); add (bet_label); bet_textfield.reshape (80, 115, 75, 25); add (bet_textfield); win1.reshape (5, 165, 75, 10); add (win1); win2.reshape (80, 165, 75, 10); add (win2); counter1.reshape (5, 215, 75, 10); add (counter1); counter2.reshape (80, 215, 75, 10); add (counter2); roll_em.reshape (5, 265, 75, 25); add (roll_em); reset.reshape (5, 315, 75, 25); add (reset); instructions.reshape (140, 290, 360, 10); add (instructions); } // display new dice (1-12). public void show_dice () { for (int i = 0; i < 2; i++) { switch (dice [i]) { case 1: getGraphics ().fillOval (269 + i * 100, 238, 2, 4); break; case 2: for (int j = 0; j < 2; j++) { getGraphics ().fillOval (255 + j * 28 + i * 100, 238, 2, 4); } break; case 3: for (int j = 0; j < 3; j++) { getGraphics ().fillOval (255 + j * 14 + i * 100, 238, 2, 4); } break; case 4: for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { getGraphics ().fillOval (255 + j * 28 + i * 100, 225 + k * 28, 2, 4); } } break; case 5: for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { getGraphics ().fillOval (255 + j * 28 + i * 100, 225 + k * 28, 2, 4); } } getGraphics ().fillOval (255 + 1 * 14 + i * 100, 238, 2, 4); break; case 6: for (int j = 0; j < 2; j++) { for (int k = 0; k < 3; k++) { getGraphics ().fillOval (255 + j * 28 + i * 100, 225 + k * 14, 2, 4); } } break; } } } // initialize beginning values. public void initialize_values () { bet = 1; pay = 0; flag = 0; last = 0; dice [0] = 6; dice [1] = 6; roll_em.enable (true); reset.enable (false); bet_textfield.enable (true); bet_textfield.setText ("" + 1); bet_textfield.invalidate (); bet_textfield.validate (); win2.setText (""); win2.invalidate (); win2.validate (); instructions.setText ("win on 2, 3, 12. lose on 7 and 11. roll again on any other number."); instructions.invalidate (); instructions.validate (); } // roll the dice (1-12). public void roll_dice () { int value, winner; winner = 0; for (int i = 0; i < 2; i++) { dice [i] = (int) (Math.random () * 6 + 1); } value = dice [0] + dice [1]; if (flag == 0) { flag = 1; switch (value) { case 2: case 3: winner = 2; instructions.setText ("you rolled " + value + ". you win."); instructions.invalidate (); instructions.validate (); roll_em.enable (false); reset.enable (true); break; case 4: case 5: case 6: instructions.setText ("you rolled " + value + ". roll again. win on " + value + ". lose on 7."); instructions.invalidate (); instructions.validate (); break; case 7: winner = 1; instructions.setText ("you rolled " + value + ". you lose."); instructions.invalidate (); instructions.validate (); roll_em.enable (false); reset.enable (true); break; case 8: case 9: case 10: instructions.setText ("you rolled " + value + ". roll again. win on " + value + ". lose on 7."); instructions.invalidate (); instructions.validate (); break; case 11: winner = 1; instructions.setText ("you rolled " + value + ". you lose."); instructions.invalidate (); instructions.validate (); break; case 12: instructions.setText ("you rolled " + value + ". you win."); instructions.invalidate (); instructions.validate (); winner = 2; break; } } else { if (value == last) { instructions.setText ("you rolled " + value + ". you win."); instructions.invalidate (); instructions.validate (); roll_em.enable (false); reset.enable (true); winner = 2; } else if (value == 7) { instructions.setText ("you rolled " + value + ". you lose."); instructions.invalidate (); instructions.validate (); roll_em.enable (false); reset.enable (true); winner = 1; } else { instructions.setText ("you rolled " + value + ". roll again. win on " + value + ". lose on 7."); instructions.invalidate (); instructions.validate (); } } if (winner == 2) { pay = bet * 2; update_counter (); } else if (winner == 1) { pay = 0; update_counter (); } last = value; } // display winnings and new counter. public void update_counter () { counter = counter - bet + pay; win2.setText ("" + pay); win2.invalidate (); win2.validate (); counter2.setText ("" + counter); counter2.invalidate (); counter2.validate (); } // roll or reset the dice. public boolean action (Event event, Object object) { if (event.target == roll_em) { bet_textfield.enable (false); roll_dice (); repaint (); return (true); } else if (event.target == reset) { initialize_values (); repaint (); return (true); } else { return (false); } } // bet from 1 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); } } }
BACK TO CRAPS APPLET.