PA3 - The Game of Pig

Objectives

The following skills are addressed in completing this assignment.

Deliverables

This assignment requires you to modify and submit a program. The program must be submitted to the Linux submit site by midnight (11:59), Tuesday, October 30. In addition, you must turn in the PDF copy of the report at the begining of class on Wednesday, October 31. Multiple pages must be stapled.

Specifications

Design and implement a program which plays a game of Pig. Your program must be built using the following four classes:

The PigDice and PigPlayer classes have already been written, and the class files are provided at the links above. Documentation for these classes is available at this link. Make sure that these class files are in the same directory as the Pig.java and PigGame.java files. I have provided the Pig.java class as source code so that you can see what it does. You will not need to make any changes to this.

For this assignment, you will complete the PigGame class. This class handles the logic for the "Game of Pig". The PigGame class uses 2 instances of PigPlayer, and each of these uses its own instance of PigDice. Study the documentation for these 2 classes before you begin your design of PigGame.java. You will need to add private helper methods to this class. You are allowed to add additional classes, if you feel they are needed.

The “Game of Pig” is a well-known game that is often used to teach issues related to probability in Mathematics and Computer Science. The game consists of 2 players and one set of dice (2 die). The goal is to be the first player to accumulate 100 points. Points are accumulated based on the values of the dice. If you roll a 1 and a 5, you will have accumulated 6 points. If you roll doubles, then you get double the number of points shown on the dice. Double 6s, for example, would give you 24 points rather than 12 (2 x 12). If you roll double 1s (snake-eyes), though, you lose the points that you have accumulated during the current turn, and you must forfeit the rest of your turn.  You may "hold" at any time durng your turn.  If you hold, the points that you have accumulated during the current turn are safe from loss in future rounds.

In our version of the game, a “human player" will play against a “computer player.” The human player can roll as many times in a row as he or she wishes. At each turn, the human player will decide whether to roll again or to hold. The computer player will roll until it accumulates 20 or more points in that turn. If either player rolls a double 1, then that player must turn the dice over to the other and they lose that turns points.

  1. The game must begin with a welcome message along with a brief explanation of the game.
  2. The user player must be named "Human". The computer player must be named "Computer."
  3. The first player to move must be the human.
  4. A turn for both the human and computer player consists of one or more rolls (of the dice). If either player rolls two "ones" (1, 1), the turn ends, and all turn points are reset to 0.  Play passes to the next player.
  5. At the start of each players turn (both the human and the computer), a blank line should be printed followed by a line containing the player's name and score. The line should be in the following form (but with the appropriate name and score filled in).

      Player: Human, Total Points = 11

  6. At the end of a player's turn, a line identical to the one above should again be printed, although this time not preceded by a blank line.
  7. Before each human roll, the human should be prompted with the following string (not terminated with a newline).

      Hold dice (Y/N?) ?

  8. The turn should end if the user responds with the character ‘y’ (upper- OR lower-case). If the user enter 'n' (upper- OR lower-case), the turn should continue.
  9. You must validate the input above. That is, if the user enters anything other than a 'y' or 'n', you must print an error message and force them to enter the value again (and keep entering until they get it right!).
  10. There should be no prompting before each roll of the computer player. The computer player's turn ends after it has accumulated 20 or more points in that turn.
  11. After each roll of the dice (for either player), the program should output a line of the following form (terminated by a newline).

      Roll=(2,2), Roll Points = 8, Turn Points = 11, Total Points = 21

    (IIn the example above, the user would have earned 8 points in the current roll, 3 points in previous rolls during this turn, and 10 points in previous turns.)
  12. When a player wins, the program should print out a line of the following form (preceded by a blank line).

    The winner is: Computer

  13. If you complete and submit a working program at least 2 days before the deadline, an additional option will also be available. Once you have successfully submitted a working PigGame class that adheres to the requirements listed above, you will be given access to the shells of the PigDice and PigPlayer classes. If you can complete these 2 classes so that they work equivalently to the class files provided, you can earn an additional 10 points. In other words, your PigGame class must work with both the classes I provided and with those that you wrote.
  14. An example session that shows shows what the output should look like (you can write your own greeting!) is provided here.

In addition to these operational requirements, your program must adhere to the Style Guide for CS139 (see the links section of this website).

Procedure

The PigGame class is where the game is actually played. The main method in the Pig creates 2 PigPlayer objects and a PigGame object (sending to it the 2 PigPlayer's. It then calls the play() method of the PigGame. This will get the game started. This method should only contain the most general logic for the game. Detailed logic for various aspects of the game should be handled in separate (and private) methods. Each method should only be concerned with the details of each individual action. We will talk more about this in class and in lab.

Hints:

  1. START EARLY - This may look easy, but it will be easy to let it get away from you.
  2. Spend time reading the documentation provided.  The better you know what the classes provide to you, the less time you will spend writing code that is already done for you.  
  3. Spend time designing the solution before you code.  
  4. Implement your solution in parts.  Don't try to write the whole thing and then test.  Write small pieces and test each one.  Add print statements so that you can see what is happening at each stage.  These should be removed before final submission.
  5. Submit will do no testing.  Make sure that you are testing your solution thoroughly.  That will mean making lots of runs to make sure that every situation is covered.  My test plan is going to test many combinations of scoring patterns.