469,341 Members | 7,542 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,341 developers. It's quick & easy.

Question about my craps game program

Hi there I need to make a Craps game for my class. Here are the parameters for it.

In the game of craps, a pass line bet proceeds as follows. Two six-sided dice are rolled; the first roll of the dice in a craps round is called the “come out roll.” A come out roll of 7 or 11 automatically wins, and a come out roll of 2, 3, or 12 automatically loses. If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll, that number becomes “the point.” The player keeps rolling the dice until either 7 or the point is rolled. If the point is rolled first, then the player wins the bet. If a 7 is rolled first, then the player loses.

Write a program that simulates a game of craps using these rules without human input. Instead of asking for a wager, the program should calculate whether the player would win or lose. The program should simulate rolling the two dice and calculate the sum. Add a loop so that the program plays 10,000 games. Add counters that count how many times the player wins, and how many times the player loses. At the end of the 10,000 games, compute the probability of winning [i.e., Wins / (Wins + Losses)] and output this value. Over the long run, who is going to win the most games, you or the house?

Note: To generate a random number x, where 0 < x <= 1, use x = Math.random( );. For example, multiplying Math.random( ) by 6 and converting to an integer results in a random integer that is between 0 and 5.

And heres my code I have been looking at it for 2 hours and i just cannot seem to find why it does not run heres the code

Expand|Select|Wrap|Line Numbers
  1. public class CrapsGame {
  2.     public static void main(String[]args)
  3.     {
  4.         final int GAMES = 10000;
  5.         int num1,num2,num3,win = 0,lose=0;
  6.         double prob;
  7.  
  8.         die die1 = new die();
  9.         die die2 = new die();
  10.  
  11.         for(int roll = 1; roll<= GAMES;roll++)
  12.         {
  13.             die1.roll();
  14.             die1.roll();
  15.             num1 = die1.getFaceValue()+ die2.getFaceValue();
  16.  
  17.             if (num1 == 7 || num1==11)
  18.                 {
  19.                 win++;
  20.                 }
  21.             else if (num1==2 || num1==3 || num1 == 12)
  22.                 {
  23.                 lose++;
  24.                 }
  25.             else {
  26.                 die1.roll();
  27.                 die2.roll();
  28.                 num2 = die1.getFaceValue() + die2.getFaceValue();
  29.                     while(num2!=num1 || num2!=7)
  30.                     {
  31.                         die1.roll();
  32.                         die2.roll();
  33.                         num3 = die1.getFaceValue() + die2.getFaceValue();
  34.                     }
  35.                     if (num3==num1)    
  36.                     {
  37.                         win++;
  38.                     }
  39.                     else if (num2==7)
  40.                     {
  41.                         lose++;
  42.                     }
  43.  
  44.  
  45.                 }
  46.  
  47.  
  48.  
  49.  
  50.         }
  51.         prob = (win/(win+lose));
  52.         if(prob>50)
  53.         {
  54.         System.out.println("The probability of you winning is more than the houses");
  55.         }
  56.         else if (prob <50)
  57.         {
  58.             System.out.println("The probability of you winning is less than the houses");
  59.         }
  60.         else
  61.         {
  62.             System.out.println("The probability of you winning is the same as the houses");
  63.         }
  64.     }
  65. }
also i have another class for roll and die and i know that there is no error there because i used it with another program. any help would be greatly appreciated.
Sep 17 '09 #1
1 8053
Frinavale
9,735 Expert Mod 8TB
Have you tried stepping through the application with a debugger tool to see what is going wrong?

If you don't have a tool to step through the application, have you tried to print out lines of text to the screen to see where the application stops?
Sep 21 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by tigrfire | last post: by
1 post views Thread by nemesis | last post: by
73 posts views Thread by JoeC | last post: by
2 posts views Thread by ar30067 | last post: by
3 posts views Thread by whitehatmiracle | last post: by
18 posts views Thread by Rhodge09 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.