// constant variables for the status of the
game
final int WON = 0, LOST = 1, CONTINUE = 2;
// other variables used in the program
boolean firstRoll = true;
int sumOfDice = 0;
int myPoint = 0;
int gameStatus = CONTINUE;
int numberHouseWins = 0;
int numberPlayerWins = 0;
String divider = "* * * * * * * *";
// graphical user interface components
JLabel die1Label, die2Label, sumLabel, rollLabel, pointLabel;
JLabel lblHouseWins, lblPlayerWins;
JLabel leftDivider, rightDivider;
JTextField firstDie, secondDie, sum, point;
JTextField txtHouseWins, txtPlayerWins;
JButton roll;
public void init( )
{
// setup graphical user interface
components
JPanel display = new JPanel();
display.setLayout( new GridLayout(8,2,10,10) );
die1Label = new JLabel( "Die 1: ", SwingConstants.RIGHT);
display.add(die1Label);
firstDie = new JTextField(3);
firstDie.setEditable(false);
display.add(firstDie);
die2Label = new JLabel( "Die 2: ", SwingConstants.RIGHT );
display.add(die2Label);
secondDie = new JTextField(3);
secondDie.setEditable(false);
display.add(secondDie);
sumLabel = new JLabel( "Their sum is: ", SwingConstants.RIGHT );
display.add(sumLabel);
sum = new JTextField(4);
sum.setEditable(false);
display.add(sum);
rollLabel = new JLabel( "Roll Again: ", SwingConstants.RIGHT );
display.add(rollLabel);
roll = new JButton("Roll Dice");
roll.addActionListener( this );
display.add(roll);
pointLabel = new JLabel( "Point is: ", SwingConstants.RIGHT );
display.add(pointLabel);
point = new JTextField(3);
point.setEditable(false);
display.add(point);
leftDivider = new JLabel( divider, SwingConstants.RIGHT );
display.add(leftDivider);
rightDivider = new JLabel( divider, SwingConstants.LEFT );
display.add(rightDivider);
lblPlayerWins = new JLabel( "Player wins: ", SwingConstants.RIGHT );
display.add(lblPlayerWins);
txtPlayerWins = new JTextField(4);
txtPlayerWins.setEditable(false);
display.add(txtPlayerWins);
lblHouseWins = new JLabel( "House wins: ", SwingConstants.RIGHT );
display.add(lblHouseWins);
txtHouseWins = new JTextField(4);
txtHouseWins.setEditable(false);
display.add(txtHouseWins);
setContentPane(display);
} // end method init( )
// call method play when button is pressed
public void actionPerformed(ActionEvent e)
{
play( );
}
public void play( )
{
// process one roll of the dice
if (firstRoll)
{
sumOfDice = rollDice( );
switch (sumOfDice)
{
// player wins on first roll
case 7: case 11:
gameStatus = WON;
point.setText("");
break;
// house wins - player loses on
first roll
case 2: case 3: case 12:
gameStatus = LOST;
point.setText("");
break;
// neither wins and point is set
// for game to continue
default:
gameStatus = CONTINUE;
myPoint = sumOfDice;
point.setText(Integer.toString(myPoint));
firstRoll = false;
break;
}
}
// if not the firstRoll then need to
// process continuation and/or
// completion possibilities
else
{
sumOfDice = rollDice( );
// when gameStatus == continue
// need to test whether new roll
// results in win for player
// win for house - loss for player
// or coninuation with current point
if (sumOfDice == myPoint)
gameStatus = WON;
else
if (sumOfDice == 7)
gameStatus = LOST;
}
// checking to see whether game is a
continuation
// or has been completed and requires adjustments
if (gameStatus == CONTINUE)
showStatus("Roll Again");
else
{
// section of code to deal with
the end of a game
// where gameStatus is no longer continue
// where wither the player or house has won
if (gameStatus == WON)
{
showStatus("Player wins. " + "Click Roll Dice to play
again");
numberPlayerWins++;
txtPlayerWins.setText(Integer.toString(numberPlayerWins));
}
else
{
showStatus("House wins. " + "Click Roll Dice to play
again.");
numberHouseWins++;
txtHouseWins.setText(Integer.toString(numberHouseWins));
}
firstRoll = true;
}
}
// roll the dice
public int rollDice( )
{
int die1, die2, workSum;
die1 = 1 + (int) (Math.random( ) * 6);
die2 = 1 + (int) (Math.random( ) * 6);
workSum = die1 + die2;
firstDie.setText(Integer.toString(die1));
secondDie.setText(Integer.toString(die2));
sum.setText(Integer.toString(workSum));
return workSum;
}