CIT 591 Assignment 3: Pig
Fall 2005, David Matuszek

Purposes of this assignment


"Pig" is a very simple game. Two players take turns; on each turn, a player rolls a six-sided die ("die" is the singular of "dice") as many times as she wishes, or until she rolls a 1. Each number she rolls, except a 1, is added to her score this turn; but if she rolls a 1, her score for this turn is zero, and her turn ends. At the end of each turn, the score for that turn is added to the player's total score. The first player to reach or exceed 100 wins.

For example:

As defined above, the first player has an advantage. To make the game more fair, we will say that if the first player reaches or exceeds 100, the second player gets one additional turn. (If the second player is the first to reach 100, the first player does not get an additional turn.)

Your assignment is to implement the game of Pig. You will play against the computer. The computer always goes first.


Your program should have four classes: Pig, Player, Human, and Computer.

The main method is in the Pig class; it will create a Human and a Computer (each of which is a kind of Player), alternate turns between them (the Computer goes first in each game), and decides when a game is over, and who won. At the end of each game, it will ask the human whether she wants to play again.

public class Pig { 
    // Your variable declarations go here...
    public static void main(String[] args) {
        new Pig().playGame();
    void playGame() {
        // Your code goes in this area...


The Player class will hold variables and methods that are common to each of the two players. Specifically, these should be:

Random numbers

To simulate rolling a die, import java.util.Random (into the Player class), declare a static variable of type Random, and create a Random object to assign to it. (It should be static because we only need one die.) The declaration looks exactly like this:

static Random random = new Random();

(You can use some variable name other than random if you like.)

Now you can get a new random number from random by sending it the message nextInt. You also need to provide a positive number as a parameter. When you do this, random will return a randomly chosen integer greater than or equal to zero and less than the number you give it as a parameter. For example, random.nextInt(3) will return one of the numbers 0, 1, or 2. You can use this result in an expression. For example, you can say

dieRoll = random.nextInt(6) + 1;

Both the Human and the Computer class should extend Player (hence inheriting the variables and methods defined in Player), and should have the following additional methods:

The Human class will also need to ask the "actual" human some yes/no questions, such as whether she wants to roll the die again. Here is some code that you can use for this purpose; just provide it with a question to ask the user, and get back a boolean (yes/no) response. You don't need to understand how the method works.

import java.util.Scanner;  // This lets you get answers from the user

    boolean askUser(String question) {
        char answer = ' ';
        while (answer != 'y' && answer != 'n') {
            System.out.print(question + "  ");
            String line = scanner.nextLine();
            answer = Character.toLowerCase(line.charAt(0));
        return answer == 'y';

Due date:

Thursday, September 29, before midnight. You or your partner should zip up the entire folder containing your .java files, your .class files, and the few extra files that BlueJ creates. Use Blackboard to turn in one copy of the program with both your names on it. It also wouldn't hurt if you had the program print out both your names when you first start it.