LMU ☀️ CMSI 186
PROGRAMMING LABORATORY
LAB 3: HIGH ROLLER Due: 2020-02-20

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.

Reading

Read (and interact with) my notes on Java Basics from the section on “Classes” to the end.

Activities

You’ve become really good at video games but have started to wonder what games were like before Pong. Did people play games on...the...command line? What was it like to play a game just with text? You decide to try writing such a game in Java, complete with old-fashioned text menus and words you type in to make things happen. And what a great idea this is! You get to practice with Java classes.

Initialize a repository

As in the previous labs, go to GitHub, select Import Repository. Then:

For Your old repository’s clone URL
Enter https://github.com/rtoal/cmsi-186-lab-3-starter-code
For Your new repository details
Enter the repository name cmsi-186-lab-3.

The import might take a few minutes. When done, clone the repo and get ready to work.

Implement your classes

The starter code has three classes for you to fill in.

Die

dice.jpg

This class represents a single die, with an immutable number of sides (at least 4) and a mutable current value (between 1 and the number of sides). The interface of the class is to be as follows:

  • SIX_SIDED_DIE_EMOJI
  • Die(int sides, int value)
  • int roll()
  • int getSides()
  • int getValue()
  • String toString()

See the comments in the starter code, and the tests, for the complete specification of each of these constructors and methods.

DiceSet

A collection of at least two Die objects, all with the same number of sides. You are not allowed to add or remove die objects from a DiceSet once constructed. But you are allowed to roll the dice in the set. The interface is to be:

  • DiceSet(int sidesOnEachDie, int numberOfDice)
  • DiceSet(int sidesOnEachDie, int... values)
  • String descriptor()
  • int sum()
  • void rollAll()
  • void rollIndividual(int i)
  • int getIndividual(int i)
  • List<Integer> values()
  • boolean isIdenticalTo(DiceSet diceSet)
  • String toString()

See the comments in the starter code, and the tests, for the complete specification of each of these constructors and methods.

HighRollerGame

This is to be a console-based application run from the command line. The program should begin by printing a welcome message. Then it repeatedly asks the user to enter a command and carries it out. Each command will either print a response or an error message, followed by a blank line. If the command is q, the program will cleanly exit.

The commands to support are:

h or help
Print a help message, showing all the commands and what they do
q or quit
Quits the program, but prints a nice message before just before quitting, saying something nice to the user, like they have nice hair, or that you were glad they played the game today.
use <s> <n>
Obtain a new set of dice. Here <n> is the number of dice, which must be between 2 and 99; and <s> is the number of sides for each die in the set, which must be between 4 and 99. Prints the descriptor of dice set just obtained and the dice set too.
roll all
Rolls all the dice, then prints the dice set.
roll <i>
Rolls the ith die in the set, then prints the dice set.
high or highest
Prints the highest roll so far
Note that not every method form the Die and DiceSet class are necessarily used in this game. That’s perfectly fine. When you write classes like Die and DiceSet, you want them to be used widely by many people, not just by you.

Here is an example run of the program, to give you a better feel for what you are to write:

$ javac HighRollerGame.java && java HighRollerGame
Welcome 🎲🎲🎲🎲🎲

Enter a command (h for help): high
No high score registered yet

Enter a command (h for help): roll all
You don't have any dice yet

Enter a command (h for help): asfhsdkjlfh
I don't understand

Enter a command (h for help): use 5 6
You are now using a 6d5
[1][1][1][1][1][1]

Enter a command (h for help): high
Highest score so far is 6

Enter a command (h for help): roll 3  
[1][1][1][5][1][1]

Enter a command (h for help): high
Highest score so far is 10

Enter a command (h for help): roll all
[3][1][1][2][5][5]

Enter a command (h for help): highest
Highest score so far is 17

Enter a command (h for help): use 20 5
You are now using a 5d20
[1][1][1][1][1]

Enter a command (h for help): roll all
[5][18][6][7][20]

Enter a command (h for help): high
Highest score so far is 56

Enter a command (h for help): use 1 20
Dice must have at least four sides

Enter a command (h for help): use 5 1
At least two dice required

Enter a command (h for help): h
h or help       : Prints this message
q or quit       : Quits the program
use <n> <s>     : Get a new dice set with n dice of s sides each
roll all        : Roll all the dice in your current dice set
roll <i>        : Roll the ith die of your current dice set
high or highest : Prints the highest roll so far

Enter a command (h for help): use 5 2
You are now using a 2d5
[1][1]

Enter a command (h for help): roll
I don't understand

Enter a command (h for help): roll all
[1][3]

Enter a command (h for help): q
I'm glad you played today. You look great!

Notice from the output above, and make sure to implement in your solution, that:

Test as you write your code

Unit test suites for Die and DiceSet have been provided for you. While developing your code, run these as follows:

$ javac DieTest.java && java DieTest
$ javac DiceSetTest.java && java DiceSetTest

No tests have been provided for the actual Game class. You are encouraged to try your hand at writing some, though.

What to turn in

Online:

On hardcopy or email:

  1. When processing the use command in the HighRoller game, did you allow an arbitrary number of spaces after the word use and between the two operands? If so, how did you do this?
  2. When processing the roll command in the HighRoller game, did you allow an arbitrary number of spaces after the word roll? If so, how did you do this?
  3. Number of hours spent working on this lab.
  4. (Optional) Feel free to let me know what you liked/disliked about this lab, what you learned, etc.