Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 players 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 its 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 True if is a valid number, False * 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

Computer rolls a 6 and a 5, for a total of 11. Computer rolls a 1 and a 2, for a total of 3. Computer rolls a 5 and a 1, for a total of 6. 6 is now the established POINT. 
 Computer rolls a 4 and a 4, for a total of 8. // in my case it would store this value again, which shouldn't be!! Computer rolls a 2 and a 5, for a total of 7. YOU LOSE 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Building The Data Lakehouse

Authors: Bill Inmon ,Mary Levins ,Ranjeet Srivastava

1st Edition

1634629663, 978-1634629669

More Books

Students also viewed these Databases questions

Question

=+3. What impediments deal with customers?

Answered: 1 week ago