Results 1 to 6 of 6
- 01-21-2013, 08:06 PM #1
Member
- 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);
}
}
- 01-21-2013, 08:39 PM #2
Re: setting counter in craps game....
Why do they call it rush hour when nothing moves? - Robin Williams
- 01-21-2013, 10:15 PM #3
Member
- 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); } }
- 01-22-2013, 05: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
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-22-2013, 07:13 AM #5
Member
- 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); } }
- 01-22-2013, 08: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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Need help setting up collisions for a Java game
By sur4j in forum New To JavaReplies: 5Last Post: 12-08-2012, 02:12 PM -
Help with setting up a GUI for a Rock, Paper, Scissors game
By thorobred in forum New To JavaReplies: 3Last Post: 02-17-2011, 02:42 AM -
Problem with Ending Craps game
By Ebayer in forum New To JavaReplies: 4Last Post: 02-11-2011, 07:17 PM -
How do you go about setting boundaries in a game?
By robertbob in forum New To JavaReplies: 6Last Post: 05-10-2010, 11:24 PM -
Basic counter timer for a game?
By Laura in forum New To JavaReplies: 1Last Post: 04-18-2010, 01:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks