CPS108: Pig

Spring 1999

Due Monday, March 29

For thousands of years people have enjoyed playing dice games. Now you have the chance to make the computer into a champion dice player -- but be careful not to turn your computer player into a pig!  The goal of this assignment is to get your feet wet in Java and to have fun.  At the end, there will be an in-class tournament.

Creating a Dice class

In CPS6, 100, and/or 100E, you have undoubtedly seen the Dice class. The first part of this assignment is to create a Dice class in Java with the same functionality as the one listed below. Below is the C++ class declaration from the book "A Computer Science Tapestry" by Owen Astrachan:
class Dice
{
  public:
    Dice(int sides);           // constructor
    int Roll();                // return the random roll
    int NumSides();            // how many sides this die has
    int NumRolls();            // # times this die rolled    
  private:
    RandGen myGenerator;       // random number generator
    int myRollCount;           // # times die rolled
    int mySides;               // # sides on die
};
Look at the Java class Math for details on how to make random numbers.

Playing Pig

You should write a class that implements a player in a game called Pig.  The rules of the game are as follows: So, there are four cases in which your turn will end:
  1. You roll a one on one of the dice and lose your points for this round.
    For example, suppose you have accumulated 67 points so far in the game, and now it is your turn. Suppose you roll and gets a one and a five. The turn ends and your score is still  67.
  2. You roll a one on both of the dice, and lose all your points. 
    Suppose you again start a turn with 67 points. Suppose you roll a one on both dice. The turn ends and your score is reduced to 0.
  3. You accumulate 100 points and win. 
    Suppose you start a turn with 90 points, and roll a six and a five. Your turn is over because you have won with a score of 101.
  4. You decide you should not be a pig. 
    That is, you have rolled and accumulated some points, and you stop your turn voluntarily instead of risk another roll in which you may lose points. Again, suppose you start with a score of 67, roll a 13, roll again and get 7, and then decide that is enough. The turn should end and your new score is 87.
So you can see why it is called pig.  Each turn, you can roll the two dice as many times as you want but, if you get too greedy you could lose everything. The key to winning the game is to know when to stop rolling the dice. Remember the risks of rolling too many times and see if you can make a player that is smart enough to regularly increase its score. A player that decides to only roll once, no matter what the result, will probably not reach 100 points very quickly. On the other hand, a player that wants to roll 100 times each turn will probably lose its points every turn. So to make your player a winner, try to figure out a heuristic for a good time to stop rolling on your turn.

You are to write a Java class that implements the Player interface given below.

interface PigPlayer
{
  /**
   *  return the name of your player
   *  (either your name, login id, or make something up)
   */
  public string getName ();
  /**
   *  return the player's current score
   */
  public int getScore ();
  /**
   *  return the number of rounds player has played
   */
  public int getNumRounds ();
  /**
   *  play a round of the game pig using whatever strategy you want
   *  @param difference difference between your score and the leading player
   */
  public void playRound (int difference);
  /**
   *  get the player ready to play a game
   *  (reset score and number of rounds played to 0)
   */
  public void reset ();
}

Extra Credit

Are there different strategies that players can use? For example, players might be aggressive, conservative, reckless or boring. The strategy might change based on the score. If your opponent rolls snake-eyes, maybe you become much more conservative.  You can earn extra credit if you create a hierarchy of classes that implement a variety of strategies for the game.  The job of you player is then to determine which strategy to delegate to at which time.  Note, no extra credit will be given for solutions that implement all the different strategies in a single class.