Programming Homework Help

Writing a Dice Game Usiing Blue J Exercise

 

Task #1 The Dice Game

  1. Write a DiceYI class with one instance variable, value.
    1. Constructor with a parameter for the initial face value. Validate that it allows only 1..6.
    2. Accessors getValue( ) which returns an integer.
    3. Mutators setValue( ) to set value. Validate it only sets 1..6
    4. Mutator roll( ) which randomize the instance variable to 1..6
    5. toString( ) method to return a string like “3” or “6”
    6. printMe( ) method for DEBUG. This should simply print the value in toString( ).
  2. Write a class (NOT A SUBCLASS) DicePairYI with a 2 element array of type DiceYI.
    1. Default constructor: Use Random class methods to initialize each DiceYI
    2. Constructor with a parameter for the initial face values. Validate that it allows only 1..6.
    3. Accessor getValue( i ) which returns the value of the first or second DiceYI.
    4. Accessor getTotal( ) which returns the sum the array DiceYI face values.
    5. Mutators setValue( i ) to set value. Let DiceYI validate it only sets 1..6
    6. Mutator roll( ) which randomizes each DiceYI in the array
    7. toString( ) method to return a string like “3, 4” or “6, 5”
    8. equals( ) method to compare a pair of dice with another pair of dice. Return true if they have the same face values. For example if one DicePairYI object has 4,6 and the other has 6,4 they are equal.
    9. printMe( ) method for DEBUG. This should simply print the value in toString( ).
  3. Write a client DiceGame1YOURNAME.
    1. Instantiate and initialize a pair of dice
    2. Instantiate score, a 20 element integer array of integers and initialize it to all 0.
    3. Write a play( ) method for a new game, it should reset the game and then continuously roll the dice until you win or lose or the array is full.
    4. Each roll is entered in the array until you win, lose, or the array is full.
    5. Write a printArray( ) method to print the score array on ONE line. Make it easy to read by adding a ” – ” after every 5 scores e.g.

4 8 9 8 5 – 7 0 0 0 0 – 0 0 0 0 0 – 0 0 0 0 0

The rules of the game: A player rolls the two dice and adds the number shown on them.

  1. Roll the two dice up to 20 times and save the total in the array.
  2. If the total is 7 or 11, you win. Put this in the array, report the results and exit the game.
  3. If the total is 2, 3, or 12, you lose. Put this in the array, report the results and exit the game.
  4. If the total is anything else, save the total as the “goal”. put it in the array, and you get to roll again.
  5. The new objective is to roll the same total as the “goal”.
    • You keep rolling until you either get “goal” or a 7. Show the array.
    • If you roll “goal”, you win. Put this in the array, report the results and exit the game.
    • If you roll a 7, you lose. Put this in the array, report the results and exit the game.
    • Sample Terminal:

You win: 10 10 0 0 0 – 0 0 0 0 0 – 0 0 0 0 0 – 0 0 0 0 0
You win: 7 0 0 0 0 – 0 0 0 0 0 – 0 0 0 0 0 – 0 0 0 0 0
You win: 8 11 8 0 0 – 0 0 0 0 0 – 0 0 0 0 0 – 0 0 0 0 0
You lose: 6 3 3 7 0 – 0 0 0 0 0 – 0 0 0 0 0 – 0 0 0 0 0
You lose: 4 5 5 12 9 – 6 6 7 0 0 – 0 0 0 0 0 – 0 0 0 0 0
You win: 8 8 0 0 0 – 0 0 0 0 0 – 0 0 0 0 0 – 0 0 0 0 0