Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

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

1. ## 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.  Reply With Quote

3. ## 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  Reply With Quote

4. ## 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:

```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  Reply With Quote

5. ## Re: Craps is a popular dice game played in casinos. Write a program to play a variation of the game. 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:

```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 =).  Reply With Quote