CSCI 180/181 Lab 7
handin name for today's lab is lab7 The lab is due on 4
March 2008 at 23:59. You must use good style and comments, as usual.
Objectives
- Learn to use random number generators.
- Practice abstraction
In class last week, we looked at Random and created a Dice class. For this lab, we will create a game called Greedy Pig. Here are the rules:
- The game consists of n players, and requires a fair, six-sided die.
- At the start of a round, all players are standing up. The die is rolled; each player gets the
number of points on the die.
- Each player may keep their points by sitting down, or remain standing for another roll. With
each roll, points accumulate for those who choose to remain standing. BUT if a 5 is rolled,
then all players still standing lose their points, ending the round with a score of 0.
- Game continues with a new round. Number of rounds is chosen by the person running the game.
We are only going to create a single player version of this. You'll need to create a class called GreedyPig (name your project lab7) along with the Dice class.
- The Dice class should have a constructor and well as a method called roll that returns the number 1-6 for the current roll. Use a constant for the number of die sides rather than hardcoding the number 6 in your roll method. See the example of GRAVITY in the bouncing ball class.
- The GreedyPig class should have an InputReader field for handling input as well as a Dice field. Include the following methods:
- Constructor that creates the InputReader and Dice objects and initializes any other fields. It should take 1 parameter - the number of rounds to be played.
- welcome - greets the player and asks for name
- playRound - plays 1 round. Each round continues until the player says they want to stop or a 5 is rolled. As with the Dice sides, use a constant for 5 rather than hardcoding it into the method. At the end of the round, return the result for that round.
- playGame - welcomes the player, plays appropriate number of rounds of the game, displaying a message at the end of each round with result of round as well as cumulative result. At end of game, calls goodbye.
- goodbye - displays an appropriate message of goodbye which depends on the player's final score.
Feel free to enhance this to make it more interesting as long as the above aspects are covered. Use handin to turn in Dice.java and GreedyPig.java under lab7.