0

This is my code I am supposed to be simulating the game of craps: I am getting the right win and losses turn out but I can't get the probability correct. any suggestions?

please help the instructions are: 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 by 6 and converting to an integer results in an integer that is between 0 and 5.

public class Dice 
{
    public static void main(String[]args)
    {
        //declaring variables
        int comeOutRoll1, comeOutRoll2;
        int roll1, roll2;
        int numW, numL;
        int sum, sum2 = 0;
        int thePoint = 0;
        double probability; 


        //initializing variables
        comeOutRoll1 = (int)(Math.random()*5);
        comeOutRoll2 = (int)(Math.random()*5);
        sum = comeOutRoll1 + comeOutRoll2;
        numW = 0;
        numL = 0;


        for(int timesPlayed = 0; timesPlayed <= 10000; timesPlayed++)
        {

            switch(sum)
            {
            //adds how many wins and losses
            case 2:
                numL = numL +1;
                break;
            case 3:
                numL = numL + 1;
                break;
            case 12:
                numL = numL + 1;
            break;
            case 7:
                numW = numW +1;
                break;
            case 11:
                numW = numW +1;
                break;
            case 4:
                thePoint = sum;
                break;
            case 5:
                thePoint = sum;
                break;
            case 6:
                thePoint = sum;
                break;
            case 8:
                thePoint = sum;
                break;
            case 9:
                thePoint = sum;
                break;
            case 10:
                thePoint = sum;
            break;
            //if not any of these cases roll again
            default:

                roll1 = (int)(Math.random()*5);
                roll2 = (int)(Math.random()*5);
                sum2 = roll1 + roll2;
                break;
            }

            if(sum2 == thePoint)
            {
                numW = numW +1;
            }
            else if(sum2 == 7)
            {
                numL = numL +1;
            }
        }


        probability = (numW/(numW+numL));

        System.out.println("Number of Wins: " + numW);
        System.out.println("Number of Losses: " + numL);
        System.out.println("The probability of winning is: " + probability + " percent");   


    }

}
Olivia
  • 11
  • 1
  • 6
  • 1
    Side note: you can avoid retyping the same code for different cases by writing multiple `case` statements without a `break`, like: `case 4: case 5: case 6: case 8: case 9: case 10: thePoint = sum; break;`. – BackSlash Nov 12 '16 at 22:36
  • Sooooo? What output do you get, what output do you expect? I.e., what is the actual question? – Gumbo Nov 12 '16 at 22:36
  • Very stimulating this simulation... ;) – martijnn2008 Nov 12 '16 at 22:36
  • @BackSlash my output is: Number of Wins: 369 Number of Losses: 813 The probability of winning is: 0.0 percent – Olivia Nov 12 '16 at 22:44
  • Possible duplicate of [Why does the division of two integers return 0.0 in Java?](http://stackoverflow.com/questions/4931892/why-does-the-division-of-two-integers-return-0-0-in-java) – BackSlash Nov 12 '16 at 22:47
  • @Olivia Check the duplicate question – BackSlash Nov 12 '16 at 22:47
  • @BackSlash everytime I run the program I get an error every third time – Olivia Nov 12 '16 at 22:53
  • You mind telling us what the error is or do we have to guess? – takendarkk Nov 12 '16 at 23:21
  • @Gumbo my output is: Number of Wins: 369 Number of Losses: 813 The probability of winning is: 0.0 percent and every couple of runs it outputs in error Exception in thread "main" java.lang.ArithmeticException: / by zero at Dice.main(Dice.java:86 – Olivia Nov 12 '16 at 23:23
  • @takendarkk sorry my output is: Number of Wins: 369 Number of Losses: 813 The probability of winning is: 0.0 percent and every couple of runs it outputs in error Exception in thread "main" java.lang.ArithmeticException: / by zero at Dice.main(Dice.java:86 – Olivia Nov 12 '16 at 23:30

2 Answers2

1

2 problems...

  1. You only analyse 1 (random) come out roll. You need to loop 10000 times over the whole game, not just the secondary rolls, which should be only looped until you get a result.
  2. You're doing integer division (which truncates to leave a whole number). Use floating point math instead by casting one of the numbers to float or double. IE probability = (float)numW/(numW+numL);

In pseudo code, using helper methods:

// returns the random integer between 1 and 6 inclusive
method rollDie()

// returns the sum of a random roll of 2 dice
method rollDice()
    return rollDie() + rollDie() 

// return true if the player won given the point
method won(point)
    roll = rollDice()
    if roll == 7 return false
    if roll == point return true
    return won(point)

// main
define wins variable (you don't need a losses variable. losses = 10000 - wins 
loop 10000 times {
    comeOut = rollDice()
    if comeOut in (7, 11) or (comeOut not in (2, 3 or 12) and won(comeOut))
        wins++
}
probability = (float)wins/10000

Convert the above to java and you should be good to go (and you'll hopefully learn something - see DRY).

Bohemian
  • 412,405
  • 93
  • 575
  • 722
-1

Edited Working Solution

public static void main(String[]args)
{
    //declaring variables
    int roll1, roll2;
    int numW = 0;
    int numL = 0;
    int sum = 0;
    int thePoint = 0;
    double probability; 


    // Loop will run 1001 time due to <=
    for(int timesPlayed = 0; timesPlayed <= 1000; timesPlayed++)
    {
        roll1 = (int)(Math.random()*5)+1;
        roll2 = (int)(Math.random()*5)+1;
        sum = roll1 + roll2;

        switch(sum)
        {
            //adds how many wins and losses
            case 2:
            case 3:
                numL = numL + 1;
                break;
            case 12:
                numL = numL + 1;
            break;
            case 7:
            case 11:
                numW = numW +1;
                break;
            case 4:
            case 5:
            case 6:
            case 8:
            case 9:
            case 10:
                thePoint = sum;
                break;
            default:
                // You should never logically reach here, so could remove.
        }

        if(thePoint!=0){
            do{
                roll1 = (int)(Math.random()*5)+1;
                roll2 = (int)(Math.random()*5)+1;
                sum = roll1 + roll2;
            }while(sum!=thePoint & sum!=7);

            if(sum == thePoint)
            {
                numW = numW +1;
            }else{
                numL = numL +1;
            }
        }
        thePoint = 0;
    }

    probability = (double)numW/(numW+numL); // (numW + numL) could just be total number of games if made into a variable and used as for loop condition aswell.

    System.out.println("Number of Wins: " + numW);
    System.out.println("Number of Losses: " + numL);
    System.out.println("The probability of winning is: " + probability + " percent");   

    }

You had to:

  • Include re-rolls when looping otherwise it would have used the same repeated values.
  • If it was the case of 4,5,6,8,9, or 10, then you had to keep rolling until new roll equalled 7 or thePoint.
    • When working out the probability, you was doing integer division, which could result in rounding of 0.

Working according to specification. Some tidying up of variables.

Tim
  • 561
  • 2
  • 13