100% found this document useful (2 votes)
292 views

Java Craps Game

This document contains code for a Java simulation of the dice game craps. The code simulates rolling dice, tracking wins and losses over multiple rounds, and implementing the basic rules of craps. It runs 100 simulations and reports the total number of wins and losses. The code is intended to help students understand how games of chance can be modeled in computer programs.

Uploaded by

Michael Davis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
292 views

Java Craps Game

This document contains code for a Java simulation of the dice game craps. The code simulates rolling dice, tracking wins and losses over multiple rounds, and implementing the basic rules of craps. It runs 100 simulations and reports the total number of wins and losses. The code is intended to help students understand how games of chance can be modeled in computer programs.

Uploaded by

Michael Davis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

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 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;
}

else if(sum==2 || sum==3 || sum==12)


{
result=youLose;
System.out.println(sum+" You Lose!");
return result;
}
else
{
point=sum;
System.out.print("Point="+point+" ");
while(sum!=7)
{
die1=roll(rand);
die2=roll(rand);
sum=die1+die2;
System.out.print("["+die1+","+die2+"] ");
if (sum == point)
{
System.out.println(sum +" You Win!");
result=youWin;
return result;
}
}
System.out.println(sum+" You Lose!");
result=youLose;
return result;
}
}
}

You might also like