# Craps is a popular dice game played in casinos. Write a program to play a variation of the game.

• February 28th, 2013, 11:35 PM
XP360
Craps is a popular dice game played in casinos. Write a program to play a variation of the game.
Craps is a popular dice game played in casinos. Write a program to play a variation of the game, as follows: Roll two dice. Each die has six faces representing values 1, 2, . . ., and 6, respectively. Check the sum of the two dice. If the sum is 2, 3, or 12 ( called craps), you lose; if the sum is 7 or 11 ( called natural), you win; if the sum is another value ( i. e., 4, 5, 6, 8, 9, or 10), a point is established. Continue to roll the dice until either a 7 or the same point value is rolled. If 7 is rolled, you lose. Otherwise, you win. Your program acts as a single player.

import java.util.Random;
public class Dices {
public static void main(String[] args) {

int point;
int dice1;
int dice2;

dice1=(int)(Math.random()*6+1);
dice2=(int)(Math.random()*6+1);
int sum = dice1 + dice2;

System.out.println("You rolled " +dice1+ " + " +dice2+ " = " +sum);

if(sum==2 || sum==3 || sum==12)
System.out.println("You lose");
else if(sum==7 || sum==11)
System.out.println("You win");
else if(sum==4 ||sum==5 ||sum==6 ||sum==8 ||sum==9 ||sum==10)
System.out.println("Point is " +sum);
else if()
System.out.println("You win");
else
System.out.println("You lose");

}
}

I am stuck at if the sum is another value ( i. e., 4, 5, 6, 8, 9, or 10), a point is established. Continue to roll the dice until either a 7 or the same point value is rolled. If 7 is rolled, you lose. Otherwise, you win. Your program acts as a single player. Can someone give me a hint of how to fix it? I just can't get the logic of how to set it up.
• February 28th, 2013, 11:38 PM
Zyrion
Re: Craps is a popular dice game played in casinos. Write a program to play a variation of the game.
First thing people are going to tell you is to wrap your code in tags
• March 1st, 2013, 12:33 PM
Zaphod_b
Re: Craps is a popular dice game played in casinos. Write a program to play a variation of the game.
First of all, I would define and implement a method named rollTwoDice(), or some such thing, that returns the sum of two random die tosses. I would do this because the program will involve rolling the dice in more than one place, and I don't like to write duplicate code in multiple places in the program.

Anyhow...

Here's pseudo-code for the main game flow, as I see it:

Code :

```Declare an int named value.   Set value equal to the result from a call to rollTwoDice().   IF value is equal to 2 or value is equal to 3 or value is equal to 12 THEN You lose. Game Over. ELSE IF value is equal to 7 or value is equal to 11 THEN You Win. Game Over. ELSE Declare an int named point. Set point equal to value. Repeat the following loop as long as Game is not Over: BEGIN LOOP Set value equal to the result from a call to rollTwoDice(); IF value is equal to point THEN You Win. Game Over. ELSE IF value is equal to 7 THEN You Lose. Game Over. END IF END LOOP END IF```

If that flow makes sense to you and if you think it satisfies the rules of the game as spelled out in your assignment, then try implementing it in Java. Any questions? Post again.

Now, if you want to practice using real dice so that you can compare with your program, come by my place, and I'll give you some lessons. We will be using my dice.

Bring cash. Checks and credit cards not accepted.

Cheers!

Z
• March 1st, 2013, 10:01 PM
XP360
Re: Craps is a popular dice game played in casinos. Write a program to play a variation of the game.
Quote:

Originally Posted by Zaphod_b
First of all, I would define and implement a method named rollTwoDice(), or some such thing, that returns the sum of two random die tosses. I would do this because the program will involve rolling the dice in more than one place, and I don't like to write duplicate code in multiple places in the program.

Anyhow...

Here's pseudo-code for the main game flow, as I see it:

Code :

```Declare an int named value.   Set value equal to the result from a call to rollTwoDice().   IF value is equal to 2 or value is equal to 3 or value is equal to 12 THEN You lose. Game Over. ELSE IF value is equal to 7 or value is equal to 11 THEN You Win. Game Over. ELSE Declare an int named point. Set point equal to value. Repeat the following loop as long as Game is not Over: BEGIN LOOP Set value equal to the result from a call to rollTwoDice(); IF value is equal to point THEN You Win. Game Over. ELSE IF value is equal to 7 THEN You Lose. Game Over. END IF END LOOP END IF```

If that flow makes sense to you and if you think it satisfies the rules of the game as spelled out in your assignment, then try implementing it in Java. Any questions? Post again.

Now, if you want to practice using real dice so that you can compare with your program, come by my place, and I'll give you some lessons. We will be using my dice.

Bring cash. Checks and credit cards not accepted.

Cheers!

Z

ahahhaaha Thanks dude but no thank for your dice lessons =).