/*
* Craps_Applet.java
*
* Created on 20. september 2007, 16:34
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Craps_Applet extends JApplet implements ActionListener {
final int WON = 0, LOST = 1, CONTINUE = 2;
boolean FirstRoll = true;
int SumOfDice = 0;
int MyPoint = 0;
int GameStatus = CONTINUE;
JLabel Dice_1_Label, Dice_2_Label, SumLabel, PointLabel, BetLabel, CashLabel;
JTextField FirstDice, SecondDice, Sum, Point, Status, Bet, Cash;
JButton Roll;
public void init() {
Container c = getContentPane();
c.setLayout(new FlowLayout());
Dice_1_Label = new JLabel("1st Dice");
c.add(Dice_1_Label);
FirstDice = new JTextField(10);
FirstDice.setEditable(false);
c.add(FirstDice);
Dice_2_Label = new JLabel("2nd Dice");
c.add(Dice_2_Label);
SecondDice = new JTextField(10);
SecondDice.setEditable(false);
c.add(SecondDice);
SumLabel = new JLabel("Sum Is");
c.add(SumLabel);
Sum = new JTextField(10);
Sum.setEditable(false);
c.add(Sum);
PointLabel = new JLabel("Point Is");
c.add(PointLabel);
Point = new JTextField(10);
Point.setEditable(false);
c.add(Point);
BetLabel = new JLabel("Bet");
c.add(BetLabel);
Bet = new JTextField(10);
Bet.setEditable(true);
Bet.setText("0");
c.add(Bet);
CashLabel = new JLabel("Total Cash");
c.add(CashLabel);
Cash = new JTextField(10);
Cash.setEditable(false);
c.add(Cash);
Cash.setText("10000");
Roll = new JButton("Roll Dice");
Roll.addActionListener(this);
c.add(Roll);
Status = new JTextField(25);
Status.setEditable(false);
c.add(Status);
Status.setText("Starting Cash: 10,000$");
}
public void actionPerformed(ActionEvent e) {
String x = Bet.getText();
String y = Cash.getText();
int t = Integer.parseInt(x);
int c$ = Integer.parseInt(y);
if (c$ <= 0) {
Status.setText("Sorry sir, but you are out of cash.");
}
else if (t > c$) {
Status.setText("Sorry sir, you cant bet more than you have.");
}
else if (t > 0) {
play();
}
else {
Status.setText("Excuse me sir, but you must place a bet!");
}
}
public void play() {
if (FirstRoll) {
SumOfDice = RollDice();
switch (SumOfDice) {
case 7: case 11:
GameStatus = WON;
Point.setText("");
break;
case 2: case 3: case 12:
GameStatus = LOST;
Point.setText("");
break;
default:
GameStatus = CONTINUE;
MyPoint = SumOfDice;
Point.setText(Integer.toString(MyPoint));
FirstRoll = false;
Bet.setEditable(false);
break;
}
}
else {
SumOfDice = RollDice();
if (SumOfDice == MyPoint) {
GameStatus = WON;
}
else if (SumOfDice == 7) {
GameStatus = LOST;
}
}
if (GameStatus == CONTINUE) {
Status.setText("Roll Again.");
}
else {
if (GameStatus == WON) {
Status.setText("You win sir. " + "Click Roll Dice to Play again");
String x = Bet.getText();
int t = Integer.parseInt(x);
int c$ = Integer.parseInt(Cash.getText());
c$ += t;
Cash.setText(Integer.toString(c$));
FirstRoll = true;
Bet.setEditable(true);
Bet.setText("0");
}
else {
Status.setText("The house wins. " + "Click Roll Dice to Play again");
String x = Bet.getText();
int t = Integer.parseInt(x);
int c$ = Integer.parseInt(Cash.getText());
c$ -= t;
Cash.setText(Integer.toString(c$));
FirstRoll = true;
Bet.setEditable(true);
Bet.setText("0");
}
}
}
public int RollDice() {
Status.setText("");
int Dice_1, Dice_2, WorkSum;
Dice_1 = 1 + (int) (Math.random() * 6);
Dice_2 = 1 + (int) (Math.random() * 6);
WorkSum = Dice_1 + Dice_2;
FirstDice.setText(Integer.toString(Dice_1));
SecondDice.setText(Integer.toString(Dice_2));
Sum.setText(Integer.toString(WorkSum));
return WorkSum;
}
}