Results 1 to 6 of 6
 01212013, 09:06 PM #1Member
 Join Date
 Jan 2013
 Posts
 3
 Rep Power
 0
setting counter in craps game....
I am trying to write a craps game wit no human input that counts the # of wins and losses and displays the probability of winning. I can not figure out why it is not counting right. Any help would be appreciated. Thanks
public class Craps
{
public static void main(String[] args)
{
int roll1 = (int)((Math.random()*6)+ 1);
int roll2 = (int)((Math.random()*6)+ 1);
int sum = roll1 + roll2;
int point = sum;
int win = 0;
int loss = 0;
for (int i = 0; i<=99; i++)
if ((sum == 7)  (sum == 11))
{
win++;
}
else if ((sum == 2)(sum == 3)(sum == 12))
{
loss++;
}
while ((sum != 7)&&(sum != point))
{
roll1 = (int)((Math.random()*6)+ 1);
roll2 = (int)((Math.random()*6)+ 1);
sum = roll1 + roll2;
}
if (sum == point)
{
win++;
}
else
{
loss++;
}
int probability = win/(win + loss);
System.out.println("The probability of winning is: " + probability);
}
}
 01212013, 09:39 PM #2
Re: setting counter in craps game....
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
 01212013, 11:15 PM #3Member
 Join Date
 Jan 2013
 Posts
 3
 Rep Power
 0
Re: setting counter in craps game....
Sorry I did not post that correctly
Java Code:public class Craps { public static void main(String[] args) { int roll1 = (int)((Math.random()*6)+ 1); int roll2 = (int)((Math.random()*6)+ 1); int sum = roll1 + roll2; int point = sum; int win = 0; int loss = 0; for (int i = 0; i<=99; i++) if ((sum == 7)  (sum == 11)) { win++; } else if ((sum == 2)(sum == 3)(sum == 12)) { loss++; } while ((sum != 7)&&(sum != point)) { roll1 = (int)((Math.random()*6)+ 1); roll2 = (int)((Math.random()*6)+ 1); sum = roll1 + roll2; } if (sum == point) { win++; } else { loss++; } int probability = win/(win + loss); System.out.println("The probability of winning is: " + probability); } }
 01222013, 06:25 AM #4
Re: setting counter in craps game....
Do you really write code without indentation? Go through Code Conventions for the Java Programming Language: Contents
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
 01222013, 08:13 AM #5Member
 Join Date
 Jan 2013
 Posts
 3
 Rep Power
 0
Re: setting counter in craps game....
Java Code:public class Craps { public static void main(String[] args) { int roll1 = (int)((Math.random()*6)+ 1); int roll2 = (int)((Math.random()*6)+ 1); int sum = roll1 + roll2; int point = sum; int win = 0; int loss = 0; for (int i = 0; i<=99; i++) if ((sum == 7)  (sum == 11)) { win++; } else if ((sum == 2)(sum == 3)(sum == 12)) { loss++; } while ((sum != 7)&&(sum != point)) { roll1 = (int)((Math.random()*6)+ 1); roll2 = (int)((Math.random()*6)+ 1); sum = roll1 + roll2; } if (sum == point) { win++; } else { loss++; } int probability = win/(win + loss); System.out.println("The probability of winning is: " + probability); } }
 01222013, 09:14 AM #6
Re: setting counter in craps game....
Thanks, that looks so much better, and is now readable. Now to your problem:
You need to tell us what result you expected, and what you got. And you could add some more System.out.println(...) calls to see the values of intermediate variables at various points in the program.
The java.util.Random class might be a better choice for generating a random int as it has a method that does exactly that, leading to more code clarity.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads

Need help setting up collisions for a Java game
By sur4j in forum New To JavaReplies: 5Last Post: 12082012, 03:12 PM 
Help with setting up a GUI for a Rock, Paper, Scissors game
By thorobred in forum New To JavaReplies: 3Last Post: 02172011, 03:42 AM 
Problem with Ending Craps game
By Ebayer in forum New To JavaReplies: 4Last Post: 02112011, 08:17 PM 
How do you go about setting boundaries in a game?
By robertbob in forum New To JavaReplies: 6Last Post: 05112010, 12:24 AM 
Basic counter timer for a game?
By Laura in forum New To JavaReplies: 1Last Post: 04182010, 02:46 AM
Bookmarks