Applet


R

roohbir

I have this applet from Deitel's Java:How to Program
It is a game of craps and I am trying to warn the player if h/she
enters a wager greater than $10000. I wrote a method called
promptPlayer for that. I just wanted to know if I am on the right track
and any tips would be appreciated(since I am new to Java.)
Thanks
Roohbir

// Fig. 6.9: Craps.java
// Craps.
import java.awt.*; // Container, FlowLayout
import java.awt.event.*; // ActionEvent, ActionListener

import javax.swing.*; // JApplet, JButton, JLabel, JTextField

public class Craps extends JApplet implements ActionListener {
int bankBalance = 1000;
// constant variables for game status
final int WON = 0, LOST = 1, CONTINUE = 2;

boolean firstRoll = true; // true if first roll of dice
int sumOfDice = 0; // sum of the dice
int myPoint = 0; // point if no win or loss on first roll
int gameStatus = CONTINUE; // game not over yet

// graphical user interface components
JLabel die1Label, die2Label, sumLabel, pointLabel, wagerLabel;
JTextField die1Field, die2Field, sumField, pointField, wagerField;
JButton rollButton;

// set up GUI components
public void init()
{
// obtain content pane and change its layout to FlowLayout
Container container = getContentPane();
container.setLayout( new FlowLayout() );

// create label and text field for wager
wagerLabel = new JLabel("Your wager ");
container.add(wagerLabel);
wagerField = new JTextField(10);
wagerField.setEditable(true);
container.add(wagerField);


// create label and text field for die 1
die1Label = new JLabel( "Die 1" );
container.add( die1Label );
die1Field = new JTextField( 10 );
die1Field.setEditable( false );
container.add( die1Field );

// create label and text field for die 2
die2Label = new JLabel( "Die 2" );
container.add( die2Label );
die2Field = new JTextField( 10 );
die2Field.setEditable( false );
container.add( die2Field );

// create label and text field for sum
sumLabel = new JLabel( "Sum is" );
container.add( sumLabel );
sumField = new JTextField( 10 );
sumField.setEditable( false );
container.add( sumField );

// create label and text field for point
pointLabel = new JLabel( "Point is" );
container.add( pointLabel );
pointField = new JTextField( 10 );
pointField.setEditable( false );
container.add( pointField );

// create button user clicks to roll dice
rollButton = new JButton( "Roll Dice" );
rollButton.addActionListener( this );
container.add( rollButton );

} // end method init

// process one roll of dice
public void actionPerformed( ActionEvent actionEvent )
{


sumOfDice = rollDice(); // roll dice

// first roll of dice
if ( firstRoll ) {

switch ( sumOfDice ) {

// win on first roll
case 7:
case 11:
gameStatus = WON;
pointField.setText( "" ); // clear point field
break;

// lose on first roll
case 2:
case 3:
case 12:
gameStatus = LOST;
pointField.setText( "" ); // clear point field
break;

// remember point
default:
gameStatus = CONTINUE;
myPoint = sumOfDice;
pointField.setText( Integer.toString( myPoint ) );
firstRoll = false;
break;

} // end switch

} // end if part of if...else

else { // subsequent roll of dice

// determine game status
if ( sumOfDice == myPoint ) // win by making point
gameStatus = WON;
else
if ( sumOfDice == 7 ) // lose by rolling 7
gameStatus = LOST;

} // end else part of if...else

displayMessage(); // display message indicating game status

} // end method actionPerformed

// roll dice, calculate sum and display results
public int rollDice()
{
promptPlayer();
// pick random die values
int die1 = 1 + ( int ) ( Math.random() * 6 );
int die2 = 1 + ( int ) ( Math.random() * 6 );

int sum = die1 + die2; // sum die values

// display results in textfields
die1Field.setText( Integer.toString( die1 ) );
die2Field.setText( Integer.toString( die2 ) );

sumField.setText( Integer.toString( sum ) );

return sum; // return sum of dice

} // end method rollDice

public void promptPlayer()
{
int wager;
wager = Integer.parseInt(wagerField.getText());

if (wager > bankBalance)
{
showStatus("Please enter wager less than $1000.");



}
else
{
rollDice();
}

}

// determine game status; display appropriate message in status bar
public void displayMessage()
{
// game should continue
if ( gameStatus == CONTINUE )
showStatus( "Roll again." );

else { // game won or lost

if ( gameStatus == WON )
showStatus( "Player wins. Click Roll Dice to play again."
);
else
showStatus( "Player loses. Click Roll Dice to play again."
);

firstRoll = true; // next roll is first roll of new game

} // end else part of if...else

} // end method displayMessage



} // end class Craps
 
Ad

Advertisements

A

Andrew Thompson

roohbir said:
I have this applet from Deitel's Java:How to Program
It is a game of craps and I am trying to warn the player if h/she
enters a wager greater than $10000. I wrote a method called
promptPlayer for that. I just wanted to know if I am on the right track
and any tips would be appreciated(since I am new to Java.)

After a short assessment of your code, I advise
that you should not be coding GUI's, and certainly
not applets, at this stage.

The basic problem (first that crops up) in this code
is that two methods call each other. This may not
be obvious in the output you see via showStatus,
but if you add a System.out.println() it becomes
abundantly clear.

So, the fundamental problem with this code is
a problem with flow control - with the decision
making and branching, along with the looping
(or breaks).

And that, flow control, is something which should
be figured out in non GUI'd - command line based
applications.

Andrew T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Segmentation Fault in Craps program 6
Deitel 3.54 5
Random is strange 28
Passing Parameters 40
Applet not inited 7
Applet Help 2
Passing Variables between functions 3
Game of craps giving class error 3

Top