Connecting Tech Pros Worldwide Help | Site Map

Question about my craps game program

Newbie
 
Join Date: Sep 2009
Posts: 3
#1: Sep 17 '09
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.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#2: Sep 21 '09

re: Question about my craps game program


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?
Reply


Similar Java bytes