Home › Forums › Computers, Games and Technology › Help with simple java code
This topic contains 8 replies, has 4 voices, and was last updated by Moderator 4 years ago.
- AuthorPosts
I’m trying to create a basic craps simulator that outputs a total wins and losses based on x number of iterations, as well as outputting the win percentage.
This is what I have 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 < 10)
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 == 10){
System.out.println(“Wins: ” + winCount);
System.out.println(“Losses: ” + lossCount);
System.out.println(“Win Percentage: ” + (winCount/100);
}
}
}
}}I use eclipse and it will run successfully and show Wins: , Losses:, yet other times it doesn’t return anything.
If anyone sees anything that might need to be changed I’d love to know about it. I’ve tried to follow the chain of logic as best as I can.
The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.My biggest problem with helping you here is … I just don’t know craps! Never played the game.
But assuming your math is all correct, check your IF statement.
You have IF…. followed by ELSE IF….. then ELSE…. and then ELSE again.
You need IF…. followed by ELSE IF….. then ELSE IF…. and then ELSE to finish.
Since I don’t know the game, it’s unclear if you need that extra IF statement
if (roll2 == 7){ lossCount++; }
So the problem I spot is with your large IF conditional statement.
But maybe it’s right because of your while loop.A while loop doesn’t use else. It’s a conditional loop all by itself, but then again I never used an else after while. Watch your brackets , because you’re not closing the first IF statements before your while loop.
It looks like your brackets are the problem here. Make sure they are correct. If they are, then let me know and let’s look at it again. According to your brackets, your first else is the problem. It should be else if.
if (condition) { // do something } else if (condition) { // do something else } else if (condition) { // do something else } else { // do this }
yet other times it doesn’t return anything.
That’s probably why. According to the way you wrote it, it will not work all the time.
I’m surprised it doesn’t kick out a syntax error.If you keep doing what you've always done... you're gonna keep getting what you always got.Basically it’s this:
The first roll in craps is the “come out roll”
Roll two dice. If the sum of those two dice is 2, 3 or 12 you lose.
If the sum is 7 or 11 you win.If either of the above situations occur you start with a new come out roll.
If it’s any other number (4,5,6,8,9,10,) you establish a “point”.
After this you roll the dice until you either roll the point number again (in which case you win) or you roll 7 and crap out (you lose).Then you go back to a new come out roll after one of the above happens.
That’s the basics and all that applies to this project.
The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.The establishment of the point via the else if statement here is what creates the need to roll until either one of those conditions is met. Same number rolled again or 7. So that loops is processing it until it’s satisfied.
So I have the loss, win, or point conditions. The first two are easy.
else if (roll == 4 || roll == 5 || roll == 6 || roll == 8 || roll == 9 || roll == 10){ dieOne = (int)(Math.random()* 6) + 1; dieTwo = (int)(Math.random()* 6) + 1; } So if that condition is met it should put me into the loop. The loop runs until condition is met then back to the top to start over, incrementing the win or loss count each time. int roll2 = dieOne + dieTwo; while (roll2 != 7){ if(roll == roll2){ //This roll made the point winCount++; break; } else { dieOne = (int)(Math.random()* 6) + 1; //This is the new roll if the point is not made dieTwo = (int)(Math.random()* 6) + 1; //Keeps going until the point or 7 is rolled roll2 = dieOne + dieTwo; } if (roll2 == 7){ //This roll crapped out for a loss lossCount++; } if (winCount + lossCount == 10000){ System.out.println("Wins: " + winCount); System.out.println("Losses: " + lossCount); System.out.println("Win Percentage: " + (winCount/winPercent) + "%");
The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.OK. But It still looks like your brackets are the problem. You don’t have an opening bracket after your first while either. That’s a problem right there. Your brackets make a huge difference. One out of place can create an entirely different statement. Forget the math and just care that your statements and conditions are all correct first. Note the “//OPENING BRACKET” which I added as a correction to your code at the top……
while (winCount + lossCount < 10) { // OPENING BRACKET 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++; // no brackets required if (winCount + lossCount == 10) { System.out.println("Wins: " + winCount); System.out.println("Losses: " + lossCount); System.out.println("Win Percentage: " + (winCount/100); } // end if } // end if } // end if } // end while } // end if } // end while
If you keep doing what you've always done... you're gonna keep getting what you always got.I’ve got my brackets all matched up but it’s still acting up.
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; double winPercent = 100.00; while (winCount + lossCount < 10000) { 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 == 10000){ System.out.println("Wins: " + winCount); System.out.println("Losses: " + lossCount); System.out.println("Win Percentage: " + (winCount/winPercent) + "%"); } } } } } } }
The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.Your “if (winCount + lossCount == 10000)” was inside the while loop defined as “while (winCount + lossCount < 10000)” Those conditions are mutually exclusive, so the printout you are looking for at the end will never happen. I cleaned it up a little bit and this appears to work (about 48%) winning percentage.
Though running repeatedly sometimes doesnt show results.
Wins: 4833
Losses: 5167
Win Percentage: 48.33%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; double winPercent = 100.00; while (winCount + lossCount < 10000) { 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 == 10000) { System.out.println("Wins: " + winCount); System.out.println("Losses: " + lossCount); System.out.println("Win Percentage: " + (winCount/winPercent) + "%"); } } }
Eek, I had a feeling that was the problem. I just couldn’t figure out where to put the condition that creates the print out so that it would work right. Now it makes a lot more sense since I’ve looked at your code! Thanks a million!
Bunker Mode, I really appreciate you taking the time to pass down some wisdom. I guess the plus side right now is that I find it enjoyable trying to figure out where I f~~~ed up when writing the code. It’s a puzzle of sorts and I end up smacking myself on the forehead in one of those “Eureka” type moments when I see just how difficult I was making things.
I appreciate all the help an input from KM as well!
The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.So it worked out. Good stuff.
- AuthorPosts
You must be logged in to reply to this topic.

921526
921524
919244
916783
915526
915524
915354
915129
914037
909862
908811
908810
908500
908465
908464
908300
907963
907895
907477
902002
901301
901106
901105
901104
901024
901017
900393
900392
900391
900390
899038
898980
896844
896798
896797
895983
895850
895848
893740
893036
891671
891670
891336
891017
890865
889894
889741
889058
888157
887960
887768
886321
886306
885519
884948
883951
881340
881339
880491
878671
878351
877678