Question: Write a complete Java program that plays a simplified version of the c
ID: 3723957 • Letter: Q
Question
Question:
Write a complete Java program that plays a simplified version of the casino game “Craps.” This game is played with a set of two perfectly balanced dice, each one a cube that has one side containing 1, 2, 3, 4, 5, or 6 black dots.
Your program will “roll” two dice until the total value is 4, 5, 6, 8, 9, or 10. This number becomes the player’s “point.” After the point value has been obtained, the program continues to roll the dice until the total value of the dice is either: 7 (in which case you lose), or it’s the “point” that was previously established (in which case you win).
so far I've got:
class crap4
{
public static void main(String[] args) {
boolean flag = true;
int prevTotal = 0;
while (flag) {
int point = doRoll();
if (point == 7) {
flag = false;
}
else if (prevTotal == point) {
flag = false;
System.out.println("You Win!");
} else {
prevTotal = point;
}
}
}
//Performs a roll of two dices. @return The sum of the two rolled dices.
static int doRoll() {
int points = 0;
int dice1;
int dice2;
while (!isValidNumber(points)) {
dice1 = rint(1, 6);
dice2 = rint(1, 6);
points = dice1 + dice2;
System.out.println("Computer rolls a " + dice1 + " and a " + dice2 + ", for a total of " + points);
}
System.out.println(points + " is now your established POINT");
return points;
}
//Generates a random number of range [a-b].
static int rint(int a, int b) {
return a + (int) ((b - a + 1) * Math.random());
}
// Checks if a number is valid.
*
* @param number
* The given number to be checked.
* @return <code>True</code> if is a valid number, <code>False</code>
* otherwise.
*/
static boolean isValidNumber(int number) {
return (number == 4 || number == 5 || number == 6 || number == 8 || number == 9 || number == 10);
}
}
The problem is now: after a valid number has been roled, the computer should store it and keep roling but should not store any new valid number. It should simply check if it is a 7 ( you loose ) or the same number as stored (you win). In my case the program keeps establishing points (storing new numbers) that are valid and stores them. the solution should look like this
Explanation / Answer
Hi.. I have introduced array list of integer to store the valid numbers. Please check below code.
Crap4.java
import java.util.ArrayList;
import java.util.List;
class Crap4
{
static List<Integer> valids = new ArrayList<Integer>();
public static void main(String[] args) {
valids.add(4);valids.add(5);valids.add(6);valids.add(8);valids.add(9);valids.add(10);
boolean flag = true;
int prevTotal = 0;
while (flag) {
int point = doRoll();
if (point == 7) {
flag = false;
}
else if (prevTotal == point) {
flag = false;
System.out.println("You Win!");
} else {
prevTotal = point;
}
}
}
//Performs a roll of two dices. @return The sum of the two rolled dices.
static int doRoll() {
int points = 0;
int dice1;
int dice2;
while (!isValidNumber(points)) {
dice1 = rint(1, 6);
dice2 = rint(1, 6);
points = dice1 + dice2;
valids.add(points);
System.out.println("Computer rolls a " + dice1 + " and a " + dice2 + ", for a total of " + points);
}
System.out.println(points + " is now your established POINT");
return points;
}
//Generates a random number of range [a-b].
static int rint(int a, int b) {
return a + (int) ((b - a + 1) * Math.random());
}
// Checks if a number is valid.
static boolean isValidNumber(int number) {
return valids.contains(number);
}
}
Output:
Computer rolls a 6 and a 3, for a total of 9
9 is now your established POINT
Computer rolls a 1 and a 2, for a total of 3
3 is now your established POINT
Computer rolls a 1 and a 2, for a total of 3
3 is now your established POINT
You Win!
Please test the code and let me know any issues. Thank you. All the best.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.