//Evaluate the justice of Craps game
//Java how to program, 5/e, Exercise 7.17
import java.awt.*; // Container, FlowLayout
import java.awt.event.*; // ActionEvent, ActionListener

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


public class CrapsProbability extends JApplet implements ActionListener {

// constant variables for game status
final int WON = 0, LOST = 1, CONTINUE = 2;
boolean firstRoll; // true if first roll of dice
boolean notCompleted;
int sumOfDice; // sum of the dice
int myPoint; // point if no win or loss on first roll
int gameStatus; // game not over yet
int winTimes[ ];
int loseTimes[ ];
int rollCounter[ ];
int counter;
double winningProbability, losingProbability,averageLength;

// graphical user interface components
JLabel judgeLabel;
JButton startButton;
JTextArea output;
JScrollPane scroller;

// set up GUI components
public void init()

{
// obtain content pane and change its layout to FlowLayout
Container container = getContentPane();
container.setLayout( new FlowLayout() );

judgeLabel = new JLabel( "Press "Start" button to play Craps 1000 times" );
container.add( judgeLabel );
startButton = new JButton( "Start" );
startButton.addActionListener( this );
container.add( startButton );

output= new JTextArea(30,60);
scroller=new JScrollPane(output);
container.add( scroller );
} // end method init

// process 1000 times of rolling of two dice
public void actionPerformed( ActionEvent actionEvent )

{
output.setText("");
winTimes=new int[22];
loseTimes=new int[22];
rollCounter=new int[1001];

for (int i=1;i<=1000;i++){ // run Craps for 1000 times
craps(i);}
displayMessage();

} // end method actionPerformed

public void craps(int i)

{
rollCounter[i]=0;
notCompleted = true;
firstRoll = true;
sumOfDice = 0;
myPoint = 0;
do

{

if ( firstRoll ) {
sumOfDice = rollDice();

switch ( sumOfDice ) {

// win on first roll
case 7:
case 11:
rollCounter[i]++;
winTimes[1]++;
notCompleted= false;
break;

// lose on first roll
case 2:
case 3:
case 12:
gameStatus = LOST;
rollCounter[i]++;
loseTimes[1]++;
notCompleted= false;
break;

// remember point
default:
myPoint = sumOfDice;
firstRoll = false;
rollCounter[i]++;
notCompleted= true;
break;

} // end switch

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


else { // subsequent roll of dice
sumOfDice = rollDice();
// determine game status
if ( sumOfDice == myPoint ) // win by making point

{rollCounter[i]++;
if (rollCounter[i]<=20)
winTimes[rollCounter[i]]++;
else
winTimes[21]++;
notCompleted= false;
}
else if ( sumOfDice == 7 ) // lose by rolling 7

{rollCounter[i]++;
if (rollCounter[i]<=20)
loseTimes[rollCounter[i]]++;
else
loseTimes[21]++;
notCompleted= false;
}
else //continue to roll

{rollCounter[i]++;
}
} // end else part of if...else
}
while (notCompleted);

}
public int rollDice()

{
// 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

return sum; // return sum of dice

}
public void displayMessage()

{
output.append("Total roll times before winning\t\tFrequency\n");
int winTotal=0;
for (int i=1;i<=21;i++)

{
output.append(Integer.toString(i)+" time (s)\t\t\t"+Integer.toString(winTimes[i])+"\n");
winTotal+=winTimes[i];
}
output.append("Total win times: \t\t"+Integer.toString(winTotal)+"\n\n");
output.append("Total roll times before losing\t\tFrequency\n");
int loseTotal=0;
for (int i=1;i<=21;i++)

{
output.append(Integer.toString(i)+" ttime (s)\t\\t "+Integer.toString(loseTimes[i])+"\n");
loseTotal+=loseTimes[i];
}
output.append("Total lose times:\t\t\t"+Integer.toString(loseTotal)+"\n");
output.append("Total Craps times:\t\t\t"+Integer.toString(loseTotal+winTotal)+"\n\n");
int rollTimesTotal=0;
for (int i=1;i<=1000;i++)

{
rollTimesTotal+=rollCounter[i];
}
output.append("Total roll times: \t\t\t"+Integer.toString(rollTimesTotal)+"\n\n");
output.append("Winning Probability: \t\t"+Double.toString((double)winTotal/10)+" %\n");
output.append("Losing Probability: \t\t"+Double.toString((double)loseTotal/10)+" % ");
output.append("Craps' Average Length:\t\t "+Double.toString((double)rollTimesTotal/1000)+" ");
output.append("***NOTE*** Try to Press "Start" button several times. \n" +
"As what you have seen, probability of wining and losing is almsot randomly the same! \n" +
"So, Craps is generally a fair play, isn't it?");
}

}
Snapshot of execution result:

发表于 @ 2008年04月18日 17:36:00|评论(loading...)|编辑