In the game of Craps, a "Pass Line" bet proceeds as follows. Using two six-sided dice, the first roll of the dice in a craps round is called the "Come Out Roll." The bet immediately wins when the come out roll is 7 or 11, and loses when the come out roll is 2, 3, or 12. 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 the player rolls a 7 first, then the player loses.

Write a program that plays the game of Craps using the rules stated above so that it simulates a game without human input. Instead of asking for a wager, the program should just calculate if 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 of Craps, you or the house?

Please help.. i am having trouble figuring this one out

int x=(int)(Math.random()*6);
int y=(int)(Math.random()*6);
int roll = x+y;
for (int i = 1; i<=10000; i++)
    if (roll == 7 || roll == 11)
     numWins = numWins +1;
    else if (roll==2 || roll==3 || roll==12)
      numLosses = numLosses + 1;
    else
{
      x=(int)(Math.random()*6);
      y=(int)(Math.random()*6);
   }
    int point = x+y;
        while (roll !=7 || roll !=11)
{
          x=(int)(Math.random()*6);
          y=(int)(Math.random()*6);
    }
     if (roll==point)
         numWins = numWins+1;
      else numLosses=numLosses+1;

Edited 3 Years Ago by Nick Evan: Fixed formatting

First of all... please use code tags--

[*code=java*]

[*/code*]

without the asteriks.

Secondly, Think about the conditions for winning and losing.

This is a simplified version of the gambling game Craps where you don't get to choose between being on the pass line or dont pass line - you're always on the pass line.

Individuals with a pass-line bet win from one of the given conditions--

-They roll 7 or 11 on the come-out roll.
-They roll a number other than 2, 3 and 12 on the come out roll and reach that number before rolling 7.

Individuals with a pass-line bet lose from one of the following conditions--

-They roll 2, 3 or 12 on the come-out roll.
-They roll 7 before they roll their point number after the come-out roll.


Keep in mind that the 2nd roll will be more then just the 2nd - you have to keep rolling until either the point or 7 is reached.

Basically you need a few data types to do the following --

-compare the number on the come out roll with the come-out roll conditions.
-store the number so that it can be compared to the post come-out roll conditions if the player doesn't win or lose (basically the same as the first)
-have a number that is continuously changing that acts as the next result (a random number).

You really don't need 2 Random.nextInt() declarations. You only need one that determines a number between 0-12, so something like--

Random rgen = new Random();
int comparer = rgen.nextInt(13);

--will be perfectly fine.

int x=(int)(Math.random()*6);
int y=(int)(Math.random()*6);
int roll = x+y;
for (int i = 1; i<=10000; i++)
if (roll == 7 || roll == 11)
numWins = numWins +1;
else if (roll==2 || roll==3 || roll==12)
numLosses = numLosses + 1;
else
{
x=(int)(Math.random()*6);
y=(int)(Math.random()*6);
}
int point = x+y;
while (roll !=7 || roll !=11)
{
x=(int)(Math.random()*6);
y=(int)(Math.random()*6);
}
if (roll==point)
numWins = numWins+1;
else numLosses=numLosses+1;

i see three problems here:
this:

int x=(int)(Math.random()*6);
int y=(int)(Math.random()*6);
int roll = x+y;
for (int i = 1; i<=10000; i++)

this:

else
{
x=(int)(Math.random()*6);
y=(int)(Math.random()*6);
}
int point = x+y;

and this:

while (roll !=7 || roll !=11)
{
x=(int)(Math.random()*6);
y=(int)(Math.random()*6);
}
if (roll==point)
numWins = numWins+1;
else numLosses=numLosses+1;

the problem with the first is that the for line should be first in order to get a random for each play
the second is that the else should not contain a new random set, that is where you would set point the third is that the braces of the while loop should surround the entire rest of your code and should break if roll==point this is how i would write the while loop

boolean keeploop = true
while (keeploop)
{
    x=(int)(Math.random()*6);
    y=(int)(Math.random()*6);
    roll = x + y;
    if (roll==point){
        numWins = numWins+1;
        break;
    }else if(roll !=7 || roll !=11){
        numLosses=numLosses+1;
    }
}

Keep in mind that you won't get any sixes with that code for the random numbers. You'll get 0-5. You need to add one if you wish to work with 1-6 as your range.

probably that's not what you need since you are overwhelmed with the problems in hand, but if I where you I would write a dice class that rolls. checker class that checks if you win or not. and a player class that counts how many times you win. you checker class uses two dice object roll them. and sends back the result of that to your player class. so you player does not even have to see the dices. that way your code will be a lot simpler. and every function you have will be less then 10 lines. I would give it a shot.

what am i doing wrong??

[public class Kharel_Ch3_2 {



public static int NUM_GAMES = 10000;


public static void main(String[] args) {


int numWins = 0;
int numLosses = 0;


int x;
int y;



x = (int)(Math.random()*6) + 1;
y = (int)(Math.random()*6) + 1;


System.out.println("The roll of first Dice is " + x);
System.out.println("The roll of Second Dice is " + y);


int roll = (x + y);



System.out.println("The sum of two dice is " +  (roll));


int point = (4 & 5 & 6 & 8 & 9 & 10);



for (int i = 1; i<=10; i++)
if (roll == 7 || roll == 11)
{
System.out.println("You win!");


numWins = numWins +1;


}


else if (roll==2 || roll==3 || roll==12)
{
System.out.println("You Lose!");


numLosses = numLosses + 1;
}


else if (point == roll)


{
x = (int)(Math.random()*6) + 1;
y = (int)(Math.random()*6) + 1;


System.out.println(roll);


while (roll !=7 || roll !=point)


x = (int)(Math.random()*6) + 1;
y = (int)(Math.random()*6) + 1;


System.out.println("Youdfsdfsdfsd Lose!");


if (roll == point)
numWins = numWins+1;
else
numLosses=numLosses+1;
}



{
System.out.println("In the simulation, we won " + numWins +
" times and lost " + numLosses + " times, ");
System.out.println("for a probability of " +
(double)(numWins)/(double)(numWins + numLosses));
}


}
}
][/public class Kharel_Ch3_2 {



public static int NUM_GAMES = 10000;


public static void main(String[] args) {


int numWins = 0;
int numLosses = 0;


int x;
int y;



x = (int)(Math.random()*6) + 1;
y = (int)(Math.random()*6) + 1;


System.out.println("The roll of first Dice is " + x);
System.out.println("The roll of Second Dice is " + y);


int roll = (x + y);



System.out.println("The sum of two dice is " +  (roll));


int point = (4 & 5 & 6 & 8 & 9 & 10);



for (int i = 1; i<=10; i++)
if (roll == 7 || roll == 11)
{
System.out.println("You win!");


numWins = numWins +1;


}


else if (roll==2 || roll==3 || roll==12)
{
System.out.println("You Lose!");


numLosses = numLosses + 1;
}


else if (point == roll)


{
x = (int)(Math.random()*6) + 1;
y = (int)(Math.random()*6) + 1;


System.out.println(roll);


while (roll !=7 || roll !=point)


x = (int)(Math.random()*6) + 1;
y = (int)(Math.random()*6) + 1;


System.out.println("Youdfsdfsdfsd Lose!");


if (roll == point)
numWins = numWins+1;
else
numLosses=numLosses+1;
}



{
System.out.println("In the simulation, we won " + numWins +
" times and lost " + numLosses + " times, ");
System.out.println("for a probability of " +
(double)(numWins)/(double)(numWins + numLosses));
}


}
}
]

Edited 3 Years Ago by happygeek: fixed formatting

public class Kharel_Ch3_2 {
    
    
    public static int NUM_GAMES = 10000;
   
    public static void main(String[] args) {
      
        int numWins = 0;
        int numLosses = 0;
      
      int x;
      int y;
      
      
      x = (int)(Math.random()*6) + 1;
      y = (int)(Math.random()*6) + 1;
     
     System.out.println("The roll of first Dice is " + x);
     System.out.println("The roll of Second Dice is " + y);
     
      int roll = (x + y);
      
     
          System.out.println("The sum of two dice is " +  (roll));
          
         
          
         int point = (4 & 5 & 6 & 8 & 9 & 10);
          

for (int i = 1; i<=10; i++)
    if (roll == 7 || roll == 11)
    { 
    System.out.println("You win!");
    
    numWins = numWins +1;
    
    }
    
    else if (roll==2 || roll==3 || roll==12)
    {
    System.out.println("You Lose!");
    
     numLosses = numLosses + 1; 
    } 
       
    else if (point == roll)
      
    {
      x = (int)(Math.random()*6) + 1;
      y = (int)(Math.random()*6) + 1;
    
     System.out.println(roll);
    
      while (roll !=7 || roll !=point)
        
      
     
      x = (int)(Math.random()*6) + 1;
      y = (int)(Math.random()*6) + 1;
    
     
        
      System.out.println("Youdfsdfsdfsd Lose!");
        
       
        
      if (roll == point)
        numWins = numWins+1;
      else
         numLosses=numLosses+1;
}




      {
    System.out.println("In the simulation, we won " + numWins +
            " times and lost " + numLosses + " times, ");
        System.out.println("for a probability of " +
            (double)(numWins)/(double)(numWins + numLosses));
}

    }
}

start quote:

while (roll !=7 || roll !=point)



x = (int)(Math.random()*6) + 1;
y = (int)(Math.random()*6) + 1;



System.out.println("Youdfsdfsdfsd Lose!");



if (roll == point)
numWins = numWins+1;
else
numLosses=numLosses+1;
}

end quote.

1) why are you printing Youdfsdfsdfsd Lose! every iteration;
2) this

if (roll == point)
numWins = numWins+1;
else
numLosses=numLosses+1;

is saying that if you don't roll the point you lose
you should also add || roll!=11 to the while

Edited 3 Years Ago by mike_2000_17: Fixed formatting

And this

int point = (4 & 5 & 6 & 8 & 9 & 10);

equals zero - so comparing that against roll with == isn't going to do whatever it is that you think it will.

This article has been dead for over six months. Start a new discussion instead.