CIT 597 Assignment 4: Pig
Fall 2006, David Matuszek

Purposes of this assignment:

General idea of the assignment:

Play a game of "Pig" against the computer, using HTML forms and JavaScript.

The game:

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

Write a web page to implement the game of Pig. You will play against the computer. The computer always goes first.

The page should include a description of the game (you can borrow from my description above). Design a reasonable, self-explanatory GUI, and implement it with HTML forms. At each stage, all information about the current game should be available to the human player--what has just been rolled, what previous rolls were, and what the current scores are. If you can, you should put a brief pause before each time the computer rolls the die; in that case, you may need to indicate whose turn it is.

When a game has been won, the program should tell who won, and offer the human another chance to play. If the human decides to play another game, the information from the previous game should be erased. Keep track of the number of games won by each player (not permanently, just for as long as the player stays on this page).

The computer should play reasonably well (it shouldn't always lose), but you don't need to come up with an optimal strategy for it.

All the game logic should be done using JavaScript. Put all necessary functions in the <head> of your page--don't use a separate .js file.

Due date:

October 10, before midnight. Email the URL to both me and my grader.