CMPS 134 Fall 2019
Prog. Assg. #8: Die Black Jack Competition
Due: 11:59pm, Dec. 11

Background

Die Black Jack is the name given here to the simple game that involves successive rolls of a single (six-sided) die, with the goal being to obtain a total that is as close as possible to, without going over, some agreed upon number (often 21). See this web page for a more detailed explanation. Of course, you will notice the similarity of this game with Blackjack, a favorite among casino goers.


Your Task

The student's task is to develop, from scratch, the class DieBlackJackCompetition, each instance of which represents a competition among players of Die Black Jack.

To aid you in this, you are provided with the (complete) source code files of the following Java classes:

Most of the previous assignments in this course have taken a sort of "fill in the blank" approach, where you were provided with one or more Java classes containing one or more "stubbed" methods. Your task was to complete each such class by supplying appropriate bodies for the stubbed methods.

This assignment is somewhat different in that you are to infer the names and intended behaviors of the public methods in the DieBlackJackCompetition class, based upon the manner in which that class is used by the given client application. And then you are to develop the class yourself, from scratch.

It should not surprise you that your instructor expects your DieBlackJackCompetition class to make use of the DieBlackJack class. Indeed, because each instance of the latter class represents someone playing the game "solo", and a competition involves multiple players, the former class should include an instance variable that is an array of DieBlackJack objects, one for each player in the competition.

Additional/Optional

You will notice the method shouldTakeTurn() in the DieBlackJack class. Its purpose is to advise the client as to whether or not it is a good idea to take another turn (i.e., by rolling the die). The provided version of the method embodies the simplistic rule that another turn should be taken if, and only if, the current roll total (sum of previous dice rolls) is less than the goal by at least four.

The underlying reasoning is that, if the roll total is within three of the goal, the chances of the next roll resulting in the player "going bust" (i.e., having a roll total that exceeds the goal) is at least half. But if the roll total is at least four away from the goal, the probability of going bust on the next roll is at most one-third.

The ambitious student who wishes to impress the instructor is encouraged to take on the additional/optional task of

  1. developing a more sophisticated version of the shouldTakeTurn() method that takes into account not only the player's own roll total but also those of the other competitors. (You will find a stubbed method in the source code provided.)

    The intended logic is this: If the player's roll total is less than any other player's (excluding those who have gone bust), the player is advised to take another turn. The reasoning here is that a player cannot win a competition without finishing with the highest roll total (among those who have not gone bust).

  2. modifying the code in the DieBlackJackCompetition class so as to make use of the more sophisticated version of shouldTakeTurn().

Sample Program Dialogs

For the purpose of illustrating the behavior of the "client" applications that were provided, in this section are several user/program dialogs. The figure below shows what to expect when running the DieBlackJackClient. As pointed out above, the purpose of providing you with that application is to show, by example, how a client program can create and make use of an instance of the DieBlackJack class.

Example Dialogs of DieBlackJackClient
Enter goal:>21
Initially: 0 of 21
Now playing ...
+5 = 5 of 21
+3 = 8 of 21
+1 = 9 of 21
+5 = 14 of 21
+5 = 19 of 21
Short of goal
------------------------
Taking one more turn ...
+4 = 23 of 21
------------------------
Finally: 23 of 21
Exceeded goal
Done!!!
Enter goal:>16
Initially: 0 of 16
Now playing ...
+3 = 3 of 16
+3 = 6 of 16
+5 = 11 of 16
+6 = 17 of 16
Exceeded goal
------------------------
Taking one more turn ...
java.lang.IllegalStateException:
goal already exceeded
------------------------
Finally: 17 of 16
Exceeded goal
Done!!!
Enter goal:>25
Initially: 0 of 25
Now playing ...
+6 = 6 of 25
+4 = 10 of 25
+4 = 14 of 25
+4 = 18 of 25
+4 = 22 of 25
Short of goal
------------------------
Taking one more turn ...
+2 = 24 of 25
------------------------
Finally: 24 of 25
Short of goal
Done!!!
Enter goal:>21
Initially: 0 of 21
Now playing ...
+4 = 4 of 21
+2 = 6 of 21
+1 = 7 of 21
+6 = 13 of 21
+4 = 17 of 21
+4 = 21 of 21
At goal!
------------------------
Taking one more turn ...
java.lang.IllegalStateException:
goal already reached
------------------------
Finally: 21 of 21
At goal!
Done!!!

Next are a few dialogs produced by running the DieBlackJackCompetitionClient application. (Of course, for this program to run at all requires there to be a functioning, even if not perfect, DieBlackJackCompetition class.) The reader should be able to figure out, for the most part, what information is being conveyed by the program's output, but a few clarifications are warranted.

First, during each round of the competition, each player chooses whether or not to "take a turn" (i.e., roll the die) based upon the "advice" provided by the shouldTakeTurn() method, which was discussed above. The competition comes to an end when no player chooses to roll the die during a given round.

Second, the players' status strings show the status of each player, in order from player #0 to the last player. Each player's status is given by either 'A', 'W', or 'L', where 'A' means ACTIVE, 'W' means WINNER, and 'L' means LOSER.

Example Dialogs of DieBlackJackCompetitionClient
Enter number of players:>5
Enter goal:>15
Initial State: (0,0,0,0,0 of 15)
Players' status:AAAAA
--------------------------
Playing round #1 ...
(0,0,0,0,0 of 15) #0 rolls 4
(4,0,0,0,0 of 15) #1 rolls 4
(4,4,0,0,0 of 15) #2 rolls 4
(4,4,4,0,0 of 15) #3 rolls 5
(4,4,4,5,0 of 15) #4 rolls 3
(4,4,4,5,3 of 15)
Players' status:AAAAA
--------------------------
Playing round #2 ...
(4,4,4,5,3 of 15) #0 rolls 2
(6,4,4,5,3 of 15) #1 rolls 2
(6,6,4,5,3 of 15) #2 rolls 2
(6,6,6,5,3 of 15) #3 rolls 6
(6,6,6,11,3 of 15) #4 rolls 1
(6,6,6,11,4 of 15)
Players' status:AAAAA
--------------------------
Playing round #3 ...
(6,6,6,11,4 of 15) #0 rolls 2
(8,6,6,11,4 of 15) #1 rolls 1
(8,7,6,11,4 of 15) #2 rolls 5
(8,7,11,11,4 of 15) #3 rolls 5
(8,7,11,16,4 of 15) #4 rolls 1
(8,7,11,16,5 of 15)
Players' status:AAALA
--------------------------
Playing round #4 ...
(8,7,11,16,5 of 15) #0 rolls 3
(11,7,11,16,5 of 15) #1 rolls 3
(11,10,11,16,5 of 15) #2 rolls 5
(11,10,16,16,5 of 15) #4 rolls 6
(11,10,16,16,11 of 15)
Players' status:AALLA
--------------------------
Playing round #5 ...
(11,10,16,16,11 of 15) #0 rolls 1
(12,10,16,16,11 of 15) #1 rolls 4
(12,14,16,16,11 of 15) #4 rolls 6
(12,14,16,16,17 of 15)
Players' status:AALLL
--------------------------
Playing round #6 ...
... No one played, so game is over ...
(12,14,16,16,17 of 15)
Players' status:LWLLL
Done!!!
Enter number of players:>4
Enter goal:>21
Initial State: (0,0,0,0 of 21)
Players' status:AAAA
--------------------------
Playing round #1 ...
(0,0,0,0 of 21) #0 rolls 3
(3,0,0,0 of 21) #1 rolls 5
(3,5,0,0 of 21) #2 rolls 1
(3,5,1,0 of 21) #3 rolls 2
(3,5,1,2 of 21)
Players' status:AAAA
--------------------------
Playing round #2 ...
(3,5,1,2 of 21) #0 rolls 2
(5,5,1,2 of 21) #1 rolls 5
(5,10,1,2 of 21) #2 rolls 3
(5,10,4,2 of 21) #3 rolls 3
(5,10,4,5 of 21)
Players' status:AAAA
--------------------------
Playing round #3 ...
(5,10,4,5 of 21) #0 rolls 6
(11,10,4,5 of 21) #1 rolls 2
(11,12,4,5 of 21) #2 rolls 1
(11,12,5,5 of 21) #3 rolls 2
(11,12,5,7 of 21)
Players' status:AAAA
--------------------------
Playing round #4 ...
(11,12,5,7 of 21) #0 rolls 1
(12,12,5,7 of 21) #1 rolls 6
(12,18,5,7 of 21) #2 rolls 1
(12,18,6,7 of 21) #3 rolls 6
(12,18,6,13 of 21)
Players' status:AAAA
--------------------------
Playing round #5 ...
(12,18,6,13 of 21) #0 rolls 2
(14,18,6,13 of 21) #2 rolls 4
(14,18,10,13 of 21) #3 rolls 6
(14,18,10,19 of 21)
Players' status:AAAA
--------------------------
Playing round #6 ...
(14,18,10,19 of 21) #0 rolls 2
(16,18,10,19 of 21) #2 rolls 6
(16,18,16,19 of 21)
Players' status:AAAA
--------------------------
Playing round #7 ...
(16,18,16,19 of 21) #0 rolls 4
(20,18,16,19 of 21) #2 rolls 4
(20,18,20,19 of 21)
Players' status:AAAA
--------------------------
Playing round #8 ...
... No one played, so game is over ...
(20,18,20,19 of 21)
Players' status:WLWL
Done!!!


Program Submission

Using the familiar file submission system, you are to submit only the source code of the DieBlackJackCompetition class, unless you have attempted the "Additional/Optional" tasks, in which case you should also submit DieBlackJack.java.

As usual, your source code file(s) are expected to include comments listing your name, the names of persons with whom you collaborated, and a description of any defects in your work.