CIT 591 Assignment 3: Pig Fall 2005, David Matuszek

# Purposes of this assignment

• To give you more experience with basic Java
• To familiarize you with:
• Simple input
• Random numbers
• Use of a method
• Use of classes and inheritance

# Overview

"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:

• Alice rolls 3, 5, 3, 6, and stops. Her score is now 19.
• Bob rolls 5, 4, 6, 6, 2, and stops. His score is now 23.
• Alice rolls 5, 3, 3, 5, 4, and stops. Her score is now 39 (19 + 20).
• Bob rolls 4, 6, 1. He has to stop, and his score is still 23 (23 + 0).
• Etc.

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.

# Details

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:

• An `int score `variable to keep track of this player's current score
• An `int getScore() `method, so that other objects can find out this player's score, and
• `int rollDie() `method to roll a die and return its result.

## 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:

•  `void takeTurn()` -- to roll the die some number of times and keep score
•  `void printScore()` -- to print this player's score

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.