CMSI 186: Homework Assignment #3
Learning Outcomes: Students will (1) be familiar with Java classes at a
basic level; (2) understand the purpose of constructors, fields, and methods; (3) understand and be able
to explain the difference between instance (nonstatic) members and class (static) members of a class; (4)
create, fill, and index Java arrays; (5) have a basic understanding of the differences between arrays as
lists in Java; (6) be able to build and use applications with multiple classes; and (7) create simple
textual user interfaces [TUI's].
Problems About Dice [Familiarity with Java Classes]
For this homework:
…you will create a dice rolling game called HighRoll. The
game is to try to make the maximum possible score using a set of dice. Dice in the set can have any number
of sides, with the requirement that all dice in the set have the same number of sides. There can be any
number of dice in the set from one to an unlimited number. Users will play the game using an on-screen
menu from which they will make a selection. After pressing ENTER the program will perform the
user's desired operation. To do this, you must accomplish the following activities:
First, make public class Die which contains at the very least, the following methods:
public Die( int nSides )
// Constructor for a single die with sides numbered 1, 2, …, up to nSides
public int roll()
// Returns a value as a result of randomly rolling this die
public int getValue()
// Returns the curent value of this die which resulted from the last roll
public String toString()
// Instance method that returns a string-y representation of THIS die,
// e.g., [11]
public static String toString()
// Classwide version of the preceding instance method
public static void main( String[] args );
// The built-in test program for this class
// TRY TO TEST AT LEAST 10 DIFFERENT CONFIGURATIONS
Next, make public class DiceSet which contains at the very least, the following methods:
public DiceSet( int k, int n )
// Constructor for a set of k dice each with n-sides (k ≥ 1 and n ≥ four)
public int sum()
// Returns the present sum of this set of dice
public void roll()
// Randomly rolls all of the dice in this set; returns void since it just sets the values
// Use either of the "toString()" methods to get the values in the set
public void rollIndividual( int i )
// Randomly rolls only the ith die in this set [indexed from zero]
public int getIndividual( int i )
// Gets the value of the ith die in this set
public String toString()
// Returns a string-y representation of this set of dice, e.g., [3][9][12][4]
public static String toString( DiceSet ds )
// Classwide version of the preceding instance method
public boolean isIdentical( DiceSet ds )
// Returns true iff this set is identical to the set ds passed as an argument
// with the ordering of the dice in each set is not important
public static void main( String[] args )
// The built-in test program for this class
Finally, make public class file HighRoll.java which uses your dice set to play the game. You
will need to have a main that constructs the dice set, has rolls, and displays scores from each roll.
The rules are as follows:
- Implement a Textual User Interface (TUI) on the command line. This will display a list of options
to the user, and will prompt for input.
- Input MUST be a number from 1 to 5. Based on that input your program will
perform the operation that the user selected.
- Your program MUST display the results of each operation, then skip a blank
line or two, and then finally must re-display the options menu.
- Your program must accept a
Q
character to quit the HighRoll game. Make your program accept
both the uppercase and lowercase character.
- Your TUI menu must appear as follows:
- ROLL ALL THE DICE
- ROLL A SINGLE DIE
- CALCULATE THE SCORE FOR THIS SET
- SAVE THIS SCORE AS HIGH SCORE
- DISPLAY THE HIGH SCORE
- ENTER 'Q' TO QUIT THE PROGRAM
- For option 2, you MUST accept a second number as part of the entry. For
example, your program would read
2 5
to re-roll only die number 5; this method means you must
parse the input to get the die index. [We'll discuss this in class.]
- To run the
HighRoll program you MUST start it using...
java HighRoll <number of dice> <number of sides>
- See the notes below.
Notes:
- There is a minimum number of sides that a die may have. When you think about it, a two-sided die is
just a coin: heads or tails are the only values possible, corresponding to one or two, zero or one,
on or off, light or dark, etc. Since this isn't a
Coin
class, don't use that.
- Likewise, there is no such thing as a
three-sided
die. Such an item is not possible in this 3D
world we inhabit. A case could be made for a hollow prism to be a three-sided die, but we would then
have to define the inside faces of each side as non-existent
or not counted
or some such
lame excuse to prevent confusion with a six-sided die.
- A four-sided die would be a tetrahedron [also known as a pyramid]. You can use that, but since it
will never end up on its point, it is a bit hard to define what it's value would be when
rolled, without coming up with a convention to unequivocably pick a side to which you can refer as its
value
. If you can do that, or you can make a case in which it doesn't matter, go for it.
There are ways of doing this; for example, the four-sided die in D&D…
- A five-sided die is possible, if you take a tetrahedron and chop off the top to make a frustum.
This would look like a pyramid with the top flattened out, or you could have a prism with the
top and bottom [or ends] being solid triangular shapes. You could also have a four-sided pyramid.
- All that being said, this IS a sort of
thought problem
so if you can figure
out a way to use even a one-sided die [pretty boring rolling that one], feel free!
- You might be tempted to say
You know, I'm only getting ONE value no matter what, so it really
doesn't matter HOW many sides the die has as long as I have a random value when I roll it.
However, by convention, each side of a die has a different number, with the default being that
the top face
on the die is the value of the roll. This also means if I have a six-sided
die, there's no way I can get an 11 on that die unless something is drastically
hinky…
- The final thought is, that for ANY DIE, the minimum value would be one, and
the maximum value would be the number of sides that the die has. Think about a six-sided die: the
minimum value of a roll is one, and the maximum value is six, the same as the number of sides.
- Note that you should only allow the user to quit the game by using the QUIT option from the menu.
That means you will implement a
good endless loop
as discussed in class.
Submission Guidelines: Make a sub-directory in your repository as mentioned above,
called homework02 and commit your source code into it. DON'T FORGET TO ADD A COMMIT COMMENT!