Playing craps, asking and printing
Posted
by
Angelo Mejia
on Stack Overflow
See other posts from Stack Overflow
or by Angelo Mejia
Published on 2013-10-23T03:12:06Z
Indexed on
2013/10/23
3:53 UTC
Read the original article
Hit count: 210
How do I ask the amount of games of craps someone wants to play and print the number of wins as a result of n number of games? Also How do I make a table in the main method using the previous methods I have? A table like this but shows the results:
Percent Wins
Games Experiment Number
1 2 3 4 5 6 7 8 9 10
100
1000
10000
100000
1000000
public class CrapsAnalysis
{
public static int rollDie( int n) {
return (int)(Math.random()*n) + 1 ;
}
public static int rollDice( ) {
int die1 ;
int die2 ;
die1 = rollDie(6) ;
die2 = rollDie(6) ;
return die1 + die2 ;
}
public static boolean playOneGame( ) {
int newDice ; //repeated dice rolls
int roll ; //first roll of the dice
int playerPoint = 0 ; //player point if no win or loss on first roll
newDice = rollDice() ;
roll = rollDice() ;
if (roll == 7 || roll == 11)
return true;
else if (roll == 2 || roll == 3 || roll == 12)
return false;
else {
playerPoint = roll;
newDice = rollDice();
do {
newDice = rollDice();
} while (newDice != playerPoint || newDice != 7) ;
if (newDice == 7)
return false;
else
return true;
}
}
public static int playGames ( int n ) {
int numWon = 0;
for (int i = 1; i <= n; i++)
if(playOneGame())
numWon++;
return numWon;
}
public static void main(String[] args) {
//Don't know how to ask and print out the results of the number of wins depending on the n number of games played
}
}
© Stack Overflow or respective owner