Java Craps Game
Java Craps Game
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 java.util.*;
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++;
}
}
System.out.println("In total: "+winCount+" wins & "+lossCount+ " losses.");
}
public static int roll(Random rand)
{
int roll=0;
roll=rand.nextInt(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;
System.out.print("["+die1+","+die2+"] ");
if(sum==7 || sum==11)
{
result=youWin;
System.out.println(sum+" You Win!");
return result;
}