Programming Craps - First Stage
Write a program to play Craps, a gambling game using dice. For this first stage, you will only program the first roll of the dice.
The rules
These are the rules of Craps.
- A player rolls two dice.
- There are three possibilities:
- 7 or 11 wins. If the total of the dice is 7 or 11 then the player wins.
- 2, 3, or 12 loses. If the total of the first roll is 2, 3, or 12 then the shooter loses.
- Others become the point. If the total is any other number (4, 5, 6, 8, 9, 10) then this number becomes the point. The player keeps rolling until one of two things happen. Either the player makes the point and wins, or the player rolls a 7 and loses (craps out). Any number other than the point or 7 is of no consequence.
- As long as the player continues to win, they continue to play. When they lose, the play passes to the next player.
Roll the dice
You can "roll the dice" in a program by calling on a random number generator,
Math.random()
, which returns a double number in the range 0.0 to 0.9999999....
To turn this into an integer in the range 1 to 6, you can use code like the following:
int roll = (int)(Math.random() * 6.0) + 1;
Output
Use JOptionPane to ask them if they're ready to roll the dice. When they click "OK", "roll" the dice and display the results: the two values on the dice, and one of three possible messages:
- They won.
- They lost.
- Tell them which "point" they have to play for.
Next
After finishing this, you're ready for Programming Craps Stage 2 - Making the Point.
Rolling to make the "Point"
In the preceding stage, the program implemented only the first roll, which results in a win, lose, or in a "point" value. This second stage is to implement rolling the dice to make the point determined by the first roll.
Roll until the point (win) or 7 (lose). You will write a loop to roll the dice until either the point value is rolled, in which case the player wins, or until a 7 is rolled, in which case the player loses.