CIT 597 Assignment 4: Pig Fall 2006, David Matuszek

## Purposes of this assignment:

• To get you started with JavaScript
• To get you familiar with HTML forms

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

• 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.)

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.