This is a code for the simulation of the game of craps built in the java coding
language. This is to be used for reference, so that the student may gain a bett
er understanding of
the ways in which games of chance may be simulated in computer environments. Th
is is not
a substitute for homework or class notes, and is distributed purely for educaito
nal aid.
import [Link].*;
public class Craps
{
public static void main(String[] args)
{
Random rand = new Random(137329037);
int winCount=0;
int lossCount=0;
for(int i=0;i<100;i++)
{
boolean result=craps(rand);
if(result==true)
{
winCount++;
}
if(result==false)
{
lossCount++;
}
}
[Link]("In total: "+winCount+" wins & "+lossCount+ " losses.");
}
public static int roll(Random rand)
{
int roll=0;
roll=[Link](6)+1;
return roll;
}
public static boolean craps(Random rand)
{
boolean youWin=true;
boolean youLose=false;
boolean result;
int
int
int
int
die1=0;
die2=0;
sum=0;
point=0;
die1=roll(rand);
die2=roll(rand);
sum=die1+die2;
[Link]("["+die1+","+die2+"] ");
if(sum==7 || sum==11)
{
result=youWin;
[Link](sum+" You Win!");
return result;
}
else if(sum==2 || sum==3 || sum==12)
{
result=youLose;
[Link](sum+" You Lose!");
return result;
}
else
{
point=sum;
[Link]("Point="+point+" ");
while(sum!=7)
{
die1=roll(rand);
die2=roll(rand);
sum=die1+die2;
[Link]("["+die1+","+die2+"] ");
if (sum == point)
{
[Link](sum +" You Win!");
result=youWin;
return result;
}
}
[Link](sum+" You Lose!");
result=youLose;
return result;
}
}
}