This program will be a Java class entitled: DiceGame

Write code that plays a simple dice game between you and the computer. The dice you will be simulating will have 12 sides instead of the usual 6. When writing the rand.nextInt(n) statement, (with n being a number), be sure you put the correct number in there for a 12-sided die, and be sure to add a 1 to it. rand.nextInt(n) will give you a number between 0 and (n - 1), so you need to add a 1 to the result to get a number between 1 and n.

Each player has in his/her possession an unbiased, 12-sided die. Be CAREFUL! When the program runs, a loop should repeat 25 times. (You must use a for loop for this to get credit!) Each iteration of the loop should do the following:

  • Computer rolls its dice: Generate a random number in the range of 1 through 12. Display this number to the screen.
  • Now YOU get to roll your die: Generate a random number in the range of 1 through 12 and display this number to the screen.
  • Display the difference between the two rolls. This can be done by subtracting one value from the other and calling Math.abs(value) to get the absolute difference.
  • Display who won the round, you or the computer (or whether there's a tie). The highest roll wins.

Here is an example of ONE of the TWENTY-FIVE (25) rounds total that will be played:

Computer Turn Round #1
- - - - - - - - - - - - - - - -
Roll: 6
User Turn Round #1
- - - - - - - - - - - - - - - -
Roll: 10
The difference between the rolls is 4.
The user wins this round!
  • As the loop iterates, the program should keep count of the totals: The total number of times the computer wins, the total number of times the user wins, and the total number of "tie" games. In other words, your program should have three separate counters!
  • After the loop iterates all twenty-five (25) iterations, the program should display who was the grand winner: the computer OR the user, OR if there was an "overall tie". The program will also display all of the totals: total number of wins the computer had, the total number of wins the user had, and the total number of ties. In the event of a tie, the program will state: Tie game!

Here is an example of how the program could finish: (You can make it look prettier if you like.)

Total Number of Computer Wins: 11
Total Number of User Wins: 13
Total Number of Tie Games: 1

The User wins the game!

The purpose of this assignment:

  • To use the Java Random class that operates as a random number generator.
  • To write code using for loops.
  • To use the concept of probability and the random number generator to simulate chance.

Roadmap:

1. Create a class called DiceGame with a main method. All your code (except imports) will be in the main method.

2. Declare variables to count user wins, computer wins, and ties.

3. Declare and initialize a random number generator. You will need to import java.util.Random.

4. Make a for loop that will iterate 25 times. Steps 5 through 8 will be in this for loop.

5. Calculate the computer's roll and store it in a variable by calling rand.nextInt(12) and adding 1 to it, and print it out.

6. Calculate the user's roll of the 12-sided dice like you did the computers, and print it out.

7. Print out the difference between the two rolls using the Math.abs function to make sure you don't print a negative number.

8. Print out who won the round.

9. Print out how many rounds the computer won, how many the user won, and how many ties there were, and appropriate message for who won overall. (Or if it was a tie.)

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.