Sign in to follow this  
Followers 0
ChaosDecides

Basic java code for craps simulator

8 posts in this topic

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.

Share this post


Link to post
Share on other sites

Your first while loop seems to be missing an opening curly brace.

 

Have you tried single-stepping through your code? That is a good way to find logic errors.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

If you would carefully indent your code you might see your problem.

Share this post


Link to post
Share on other sites

moving this to the java forum

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

I don't know the rules for the game Craps. Shouldn't the value of roll change inside the while loops?

 

As I mentioned single-stepping is usually helpful.

Share this post


Link to post
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?

Share this post


Link to post
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
Sign in to follow this  
Followers 0