Guest_imported
New member
- Jan 1, 1970
- 0
I am trying to create a game of craps that initilizes a bankbalance of $10,000. Prompts the player to enter a Wage. Check that Wage is less than or equal to Bankbalance, if not, have the user reenter Wage until it's a valid Wage. If player wins, Banksbalance goes up and if he looses, bankbalance goes down. If Bankbalance goes down to zero, message is displayed that says "Sorry, you have Lost" Any help is appricated.
Thanks,
Here is what I have right now,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Craps 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 die1Label, die2Label, sumLabel, pointLabel;
JTextField die1Field, die2Field, sumField, pointField;
JButton rollButton;
public void init()
{
Container container = getContentPane();
container.setLayout( new FlowLayout() );
die1Label = new JLabel( "Die 1" );
container.add( die1Label );
die1Field = new JTextField( 10 );
die1Field.setEditable( false );
container.add( die1Field );
die2Label = new JLabel( "Die 2" );
container.add( die2Label );
die2Field = new JTextField( 10 );
die2Field.setEditable( false );
container.add( die2Field );
sumLabel = new JLabel( "Sum is" );
container.add( sumLabel );
sumField = new JTextField( 10 );
sumField.setEditable( false );
container.add( sumField );
pointLabel = new JLabel( "Point is" );
container.add( pointLabel );
pointField = new JTextField( 10 );
pointField.setEditable( false );
container.add( pointField );
rollButton = new JButton( "Roll Dice" );
rollButton.addActionListener( this );
container.add( rollButton );
}
public void actionPerformed( ActionEvent actionEvent )
{
if ( firstRoll ) {
sumOfDice = rollDice();
switch ( sumOfDice ) {
case 7: case 11:
gameStatus = WON;
pointField.setText( "" );
field
break;
case 2: case 3: case 12:
gameStatus = LOST;
pointField.setText( "" );
field
break;
default:
gameStatus = CONTINUE;
myPoint = sumOfDice;
pointField.setText( Integer.toString( myPoint ) );
firstRoll = false;
break;
}
}
else {
sumOfDice = rollDice();
if ( sumOfDice == myPoint )
gameStatus = WON;
else
if ( sumOfDice == 7 )
gameStatus = LOST;
}
displayMessage();
}
public int rollDice()
{
int die1, die2, sum;
die1 = 1 + ( int ) ( Math.random() * 6 );
die2 = 1 + ( int ) ( Math.random() * 6 );
sum = die1 + die2; // sum die values
die1Field.setText( Integer.toString( die1 ) );
die2Field.setText( Integer.toString( die2 ) );
sumField.setText( Integer.toString( sum ) );
return sum;
}
public void displayMessage()
{
if ( gameStatus == CONTINUE )
showStatus( "Roll again." );
else {
if ( gameStatus == WON )
showStatus( "Player wins. " +
"Click Roll Dice to play again." );
else
showStatus( "Player loses. " +
"Click Roll Dice to play again." );
firstRoll = true;
}
}
}
Thanks,
Here is what I have right now,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Craps 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 die1Label, die2Label, sumLabel, pointLabel;
JTextField die1Field, die2Field, sumField, pointField;
JButton rollButton;
public void init()
{
Container container = getContentPane();
container.setLayout( new FlowLayout() );
die1Label = new JLabel( "Die 1" );
container.add( die1Label );
die1Field = new JTextField( 10 );
die1Field.setEditable( false );
container.add( die1Field );
die2Label = new JLabel( "Die 2" );
container.add( die2Label );
die2Field = new JTextField( 10 );
die2Field.setEditable( false );
container.add( die2Field );
sumLabel = new JLabel( "Sum is" );
container.add( sumLabel );
sumField = new JTextField( 10 );
sumField.setEditable( false );
container.add( sumField );
pointLabel = new JLabel( "Point is" );
container.add( pointLabel );
pointField = new JTextField( 10 );
pointField.setEditable( false );
container.add( pointField );
rollButton = new JButton( "Roll Dice" );
rollButton.addActionListener( this );
container.add( rollButton );
}
public void actionPerformed( ActionEvent actionEvent )
{
if ( firstRoll ) {
sumOfDice = rollDice();
switch ( sumOfDice ) {
case 7: case 11:
gameStatus = WON;
pointField.setText( "" );
field
break;
case 2: case 3: case 12:
gameStatus = LOST;
pointField.setText( "" );
field
break;
default:
gameStatus = CONTINUE;
myPoint = sumOfDice;
pointField.setText( Integer.toString( myPoint ) );
firstRoll = false;
break;
}
}
else {
sumOfDice = rollDice();
if ( sumOfDice == myPoint )
gameStatus = WON;
else
if ( sumOfDice == 7 )
gameStatus = LOST;
}
displayMessage();
}
public int rollDice()
{
int die1, die2, sum;
die1 = 1 + ( int ) ( Math.random() * 6 );
die2 = 1 + ( int ) ( Math.random() * 6 );
sum = die1 + die2; // sum die values
die1Field.setText( Integer.toString( die1 ) );
die2Field.setText( Integer.toString( die2 ) );
sumField.setText( Integer.toString( sum ) );
return sum;
}
public void displayMessage()
{
if ( gameStatus == CONTINUE )
showStatus( "Roll again." );
else {
if ( gameStatus == WON )
showStatus( "Player wins. " +
"Click Roll Dice to play again." );
else
showStatus( "Player loses. " +
"Click Roll Dice to play again." );
firstRoll = true;
}
}
}