-4

In class we studied some source code that used random number generation to help play the dice game known as craps. The rules of the basic game we covered in class and may be summarized as follows. The first roll of the dice ends in a win or a loss or a requirement to continue to throw the dice. If the first roll is a 7 or 11 it is a win. If the first roll is a 2, 3, or 12 it is a loss. For any other total (the point as it is called) you must rethrow the dice until you either repeat the point or throw a 7. If a rethrow generates a 7 it is a loss. If a rethrow produces the point it is a win. If neither occurs you must continue to rethrow. The source code is visible on the course web site in the Examples section. The program is titled CrapsGame.java. For this question you are to use the basic game playing code to run a simulation of 10 million games and gather some statistics from the simulation and then display the results.

The statistical information to determine and then to print out is:

  • Average # of rolls per game
  • The maximum number of rolls used in a single game
  • The number of games that needed more than 30 rolls
  • The number of wins
  • The number of losses
  • The probability of a win (displayed to 4 decimal places)

(THIS IS THE QUESTION)

public class CrapsGame{


public static void main(String[] args) {

int time; //number of times the game user want to play
int rolls=0; //number of rolls
int mypoint=0; 
int d1; //number user rolled for first dice
int d2; //number user rolled for second dice
int total=0; //total number of rolls 
int Numwins=0; //number of wins
int Numlosses = 0; //number of losses
double averolls=0;  //average number of rolls per game
double probwin=0; //probability of win
boolean win, rollagain=false;                  

win = false;
System.out.println("Enter the number of times the game you want to play: ");
time=TextIO.getlnInt();   

rolls=0;


for (int i=0; i<time; i++){
      d1=(int)(6*Math.random())+1; //user roll dice(generate a random number)
      d2=(int)(6*Math.random())+1;  
      System.out.printf("%d and %d\n",d1,d2);
      rolls++;
      total = total + rolls;
   switch (d1+d2) {
      case 7: 
      case 11:
        win=true; //when first roll is 7 or 11, player win
         Numwins++;
        break;
      case 2: 
      case 3: 
      case 12:
        win=false; //when first roll is 2,3 or 12, player lose
         Numlosses++;
        break;
      default:   
        mypoint = d1 + d2;
        rollagain=true; //when first roll is 1,4,5,6,8,9,10 or 11, player rethrow dice      
   }
        while(rollagain==true){
          d1=(int)(6*Math.random())+1;
          d2=(int)(6*Math.random())+1;
           System.out.printf("%d and %d\n",d1,d2);

      if (d1+d2 == mypoint ){
        win=true;
        rollagain=false;           
      }
      else if ( d1+d2 == 7){
        win=false;
        rollagain=false;  
      }
    }


 if (win){
  System.out.printf("***WINNER***\n");
 }
  else{
  System.out.printf("YOU LOSE\n");


  }



  }


probwin = Numwins / time;
averolls = total / time ;

System.out.printf("Avg # rolls: %.2f\n",averolls);
System.out.printf("Max # rolls: %d\n",rolls);
System.out.printf("# of wins: %d\n",Numwins);
System.out.printf("# of losses: %d\n",Numlosses);
System.out.printf("The probability of a win: %.2f.\n",probwin);



}
}

Here are the questions sirs! my program can run but the calculation part, every single calculation part ex. average rolls per game, number of wins and losses , etc. , doesnt work well. it always gives me wrong numbers. please help me.

sing
  • 1
  • 2
  • 2
    Welcome to Stack Overflow. When adking for debugging help, please always, always be precise about how the results are different from the desired. "Wrong numbers" is much too vague. – Ole V.V. Mar 08 '17 at 04:02
  • Your code that prints whether you won or lost and updates numWins and numLosses needs to be inside the for loop - otherwise it's only registering 1 win or loss no matter how many games you play. – D M Mar 08 '17 at 04:11
  • You are doing integer divisions. I you want the fraction part of the result too, convert to `double` *before* dividing, for example `(double) total / (double) time`. – Ole V.V. Mar 08 '17 at 04:17
  • i have another question sirs. during the second roll, if user rolled same number as before, they should win this game. although my code says mypoint=d1+d2, my program ignore this. and it only determine user lose when their roll is 7 – sing Mar 08 '17 at 04:23
  • Currently I believe `myPoint` holds the points from your first roll. You want the points from the roll just before this one. Insert an assignment statement at the appropriate point. – Ole V.V. Mar 08 '17 at 04:32
  • how to insert. iam new to java. i know nothing – sing Mar 08 '17 at 04:45
  • You need to set `myPoint` to the latest points you got ( `d1 + d2`) before the next time through your loop, that is, right at the end of it. So just above `} while(rollagain);` type the same assignment you also got in the `default` branch of your `swithc` statement: `mypoint = d1 + d2;`. – Ole V.V. Mar 08 '17 at 05:55
  • well sir add mypoint above while(rollagain) is not helpful, it makes everything u rolled into YOU LOSE. and then i change back to my original one. have same issue. program doesnt care the number u rolled, it just keep finding 7 and says U lose . iam done plz hlep – sing Mar 08 '17 at 06:13

1 Answers1

0
  • Instead of rolls = 0; you should do rolls = 1; since you have just made your first roll at this point.
  • Your do loop is always executed at least once, that is, even when a win or loss has been determined in the first roll before the loop. Use a while loop instead.
  • You are doing total = total + rolls; inside your inner loop, that is, way too often, so you end up with a too high total. Instead do it after the while loop (the do loop in your code).
  • I mean what I said in the comments: mypoint holds the points from your first roll. You want the points from the roll just before this one. As the last statement inside the while loop insert mypoint = d1 + d2; to pick up the points from the roll you just made.
  • You are incrementing Numwins or Numlosses only after your for loop so only the last game is counted. Move this inside your for loop instead.
  • Finally, as I said in the other comment, convert your numbers to double before dividing to obtain floating-point division, for example probwin = (double) Numwins / (double) time; (technically it suffices to put (double) before one of the numbers, but I think it’s clearer what is going on if you put it in both places).

General tips:

  • Don’t declare a variable until the point where you need it. It makes for more readable code and fewer errors. For example, if you had declared win inside the for loop, where you need it, you could not have made the bug of only testing it outside the for loop.
  • Keep your indentation correct. Then it’s much easier to make sure whether a certain statement is inside or outside a certain loop and spot errors where a statement is in the wrong place. Your IDE can do your indentation for you at a keypress.

And finally, you didn’t ask about this, but allow me to mention anyway: Your program contains many correct things, the bugs are really details. It was easy to follow your intention, this is important in programming and certainly not always the case. Keep it that way and you will be doing fine.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • `while (rollagain) {`…`}` is fine (with or without `== true`; conventionally this is left out). You still have `rollagain = false;` in two places inside the loop, right? That ought to stop the loop sooner or later, or something else is wrong. – Ole V.V. Mar 08 '17 at 14:06
  • sir another question !! u helped me a lot and it works one more question sir – sing Mar 08 '17 at 14:07
  • i updated my code and questions are its is not counting number of wins and losses, which means Numwins++ and Numlosses++ is not working . and it is not counting probability of a win idk why – sing Mar 08 '17 at 14:08
  • In your updated program you are incrementing `Numwins` and `Numlosses` only if a win or loss is determined in the first roll. You should also do it in your `if (win)`-`else` statement so you catch the cases where it takes more rolls to decide a win or loss. – Ole V.V. Mar 08 '17 at 14:14
  • omg thank you. another problemXD. i played two games, one won , one lost, the probability of a win should be 50% right? but it gave me 0% how????????? – sing Mar 08 '17 at 14:19
  • Because of Java `int` division. `1 / 2` yields 0 since the remainder in the division is discarded. Go back and read again what I told you about converting to `double` before dividing. Then you will be able to get `0.50`. – Ole V.V. Mar 08 '17 at 14:29
  • See [Integer division: How do you produce a double?](http://stackoverflow.com/questions/3144610/integer-division-how-do-you-produce-a-double) – Ole V.V. Mar 08 '17 at 14:35
  • ty it works. and...........NEW PROBLEM. i played three games, 1 losse 2 wons,pro of a win is 100%...... and number of wins it said 3 . plz help. – sing Mar 08 '17 at 14:40
  • This may be an error on my part: I said “You should also do it in your `if (win)`-`else` statement”. If you increment in both places, wins and losses in the first roll are counted double, this is a possible explanation. You should *only* increment in `if (win)`-`else`. – Ole V.V. Mar 08 '17 at 14:50
  • should i do the same thing to averolls=(double)total/(double)time?? – sing Mar 08 '17 at 15:58
  • cause right now counting average sometimes have wrong numbers, for example i got total 24 rolls in three games my average roll is 18.tahts pretty weird – sing Mar 08 '17 at 16:00
  • i believe something wrong with total. u said put it after while loop, can u tell me the specific place? i am year 1 student, know nothing – sing Mar 08 '17 at 16:19
  • Will revert tomorrow (no promises how cleverly I may be able to guide you). – Ole V.V. Mar 08 '17 at 17:16
  • plz sir it is due in the next 3 hours i am dying sir – sing Mar 08 '17 at 17:19
  • They ought to let you pass with what you have. It's so close. My notes and my working version of your program are in my workplace, no way I can get them until tomorrow morning local time. – Ole V.V. Mar 08 '17 at 17:25
  • Yes, @sing, you should `averolls=(double)total/(double)time` for a result with the correct fraction (decimals). For the wrong count of rolls, you should reset `rolls` to 1 after the first roll in each game. Remember to increment inside the `while` loop (wasn’t this in your previous version?) Finally `total = total + rolls;` should go after the end of the wile loop when you know the game is over, but still inside your `for` loop. Hope you’ll make it! – Ole V.V. Mar 08 '17 at 19:39