CMPS 134 Fall 2023
Prog. Assg. #6: Nearly (A Dice Game Played with a Single Die)
Deadline: 9:00am, Tuesday, November 28

Image of a pair of dice
You are probably familiar with games that involving rolling six-sided dice, as pictured to the right. For this assignment, you are to develop, from scratch, a Java application named Nearly that plays a game of the same name. This game involves repeatedly rolling a single six-sided die (singular of "dice").

To play Nearly, you choose a starting value, which is a positive integer no greater than 100. That number becomes your current value, which changes each time you take a turn. Taking a turn involves rolling the die and then subtracting the result of that roll (i.e., the number of pips/dots facing upwards, which is an integer between one and six) from the current value, unless that would make the current value negative, in which case you instead increase the current value by that number of pips.
Enter a seed:>456
Enter starting value:>35
Roll of [3] decreases to 32
Roll of [5] decreases to 27
Roll of [6] decreases to 21
Roll of [6] decreases to 15
Roll of [5] decreases to 10
Roll of [4] decreases to 6
Roll of [2] decreases to 4
Roll of [1] decreases to 3
Roll of [2] decreases to 1
Roll of [4] INCREASES to 5
Roll of [1] decreases to 4
Roll of [5] INCREASES to 9
Roll of [4] decreases to 5
Roll of [3] decreases to 2
Roll of [4] INCREASES to 6
Roll of [6] decreases to 0
It took 16 turns!
Nearly, a simple game involving one die...
------------------------------------------
Enter a seed:>5
Enter starting value:>17
Roll of [6] decreases to 11
Roll of [5] decreases to 6
Roll of [3] decreases to 3
Roll of [3] decreases to 0
It took 4 turns for a direct win!
Nearly, a simple game involving one die...
------------------------------------------
Enter a seed:>5
Enter starting value:>258
Retry; response must be in range [1..100]
Enter starting value:>0
Retry; response must be in range [1..100]
Enter starting value:>8
Roll of [6] decreases to 2
Roll of [5] INCREASES to 7
Roll of [3] decreases to 4
Roll of [3] decreases to 1
Roll of [1] decreases to 0
It took 5 turns!

The game ends when the current value becomes zero. A measure of how well you played the game (i.e., how lucky you were) is given by the number of die rolls you made before it ended, the fewer the better.1  A player is said to have achieved a direct win if every turn resulted in the current value being decreased.

Provided to you is the Java class Die, each instance of which represents —surprise!— a six-sided die. To illustrate how to make use of that class, the Java application DieClient is also provided. Your Java application Nearly is required to be —like DieClient— a client of Die.

The behavioral requirements of the application should become clear from examining the sample program/user dialogs to the right, which represent three separate executions of the program. (User input is shown in boldface. Everything else is produced by the program.) Notice that the program asks the user not only to enter a starting value but also a seed. The seed is for the purpose of initializing the pseudo-random number generator —an instance of the java.util.Random class— that is used by a Die object to simulate the rolling of a die. Two Die objects that are seeded with the same value will produce the same sequence of die rolls.

Your program is expected to produce output whose format matches exactly what is shown. In particular, the third dialog illustrates how the application must react when the user enters an out-of-range starting value. (See the DieClient program's getIntFromUser() method.)

As usual, your Java class is required to include comments identifying persons with whom you collaborated and listing any defects of which you are aware. (Being aware of defects and mentioning them is better than not being aware of them, as the latter suggests that either you cannot distinguish correct from incorrect behavior or that you didn't bother to perform tests to discover faulty behavior.)

It is also expected that your program be well-commented, meaning not only that each method should be preceded by a comment describing its purpose but also that there should be a comment above the class heading describing the purpose of the application. (You are free to use phrases or sentences appearing within this document.)


Program Submission

Submit your Java source code file (Nearly.java) using the standard procedure.

Footnote

[1] On the other hand, one might reasonably strive to maximize the number of die rolls during a game rather than to minimize it. This idea gives rise to an alternative version of the game!