Jump to content

Basic java code for craps simulator


ChaosDecides

Recommended Posts

I'm currently working on a basic craps simulator that needs to ouput the total wins and losses based on x number of interations, as well as output a win percentage. I haven't added the percentage part yet.

 

I'm running it in eclipse and sometimes I'll get the output I want and other times I'll get no output at all.

 

This is the code I've written so far:

 

public class craps {

public static void main(String[] args)
{
int dieOne = (int)(Math.random()* 6) + 1;
int dieTwo = (int)(Math.random()* 6) + 1;
int roll = dieOne + dieTwo;
int winCount = 0;
int lossCount = 0;

while (winCount + lossCount < 100)

if (roll == 2 || roll == 3 || roll == 12){
lossCount++;
}
else if (roll == 7 || roll == 11){
winCount++;
}
else {
dieOne = (int)(Math.random()* 6) + 1;
dieTwo = (int)(Math.random()* 6) + 1;

int roll2 = dieOne + dieTwo;

while (roll2 != 7){

if(roll == roll2){
winCount++;
break;
}
else {


dieOne = (int)(Math.random()* 6) + 1;
dieTwo = (int)(Math.random()* 6) + 1;
roll2 = dieOne + dieTwo;

}
if (roll2 == 7){
lossCount++;

}

if (winCount + lossCount == 100){
System.out.println("Wins: " + winCount);
System.out.println("Losses: " + lossCount);
}
}
}
}}

I'm not seeing what part of the code might return zero ouput, unless eclipse is to blame. The second while loop is supposed to be if you roll a point and are rolling until you make the point or crap out.

 

I'd appreciate any advice. Thanks.

Link to comment
Share on other sites

Thanks for pointing that out! Fixed. :)

 

The output part at the end is where I think the issue is happening. Basically once the while statement is no longer true I need the output. I don't know if the loop is checking that last if statement on each pass through.

 

When I just do the come out roll and either win or crap out it works fine. I put in a print function for each line to see that numbers were indeed being generated. However some results just didn't look right. I'd get all 7's once followed by all 11's, then I'd have several in a row with a pretty wide range of rolls.

Edited by ChaosDecides
Link to comment
Share on other sites

I went through and indented everything but I'm still not seeing the problem. I cut and pasted it from eclipse and it messed up the indentation. The original looks a lot better. I added the output for percentage and it works also.

 

Looks like a case of hiding in plain site. My arch nemesis.

Edited by ChaosDecides
Link to comment
Share on other sites

what kind of output are you expecting? what kind of debugging are you doing to make sure values are what you expect them to be?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...