CIT 590 Assignment 8: Pig
Spring 2013, 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.


You should name your project Pig.

You should create one package, named pig. All your classes should be in this package.

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, the Player class should import java.util.Random, 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 should study this method and understand how it works.

import java.util.Scanner;  // This goes before your class definition
Scanner scanner = new Scanner(; // Do this once, to create a Scanner
/** * Asks the user a yes/no type question. * The question to ask. * @return true if the user answers "yes". */ static boolean askUser(String question) { char answer = ' '; while (answer != 'y' && answer != 'n') { System.out.print(question + " "); String line = scanner.nextLine(); if (line.length() == 0) continue; answer = Character.toLowerCase(line.charAt(0)); } return answer == 'y'; }

Due date:

6am Friday March 22. You or your partner should zip up the entire project folder containing your .java files, your .class files, and any extra files that Eclipse creates. Use Canvas. to turn in one copy of the program with both your names on it. No other form of submission will be accepted. It also wouldn't hurt if you had the program print out both your names when you first start it.