CIT 597 Assignment 3: 21 in JavaScript
Fall 2004, David Matuszek

Purposes of this assignment:

General idea of the assignment:

Implement the "21" card game (similar to Blackjack) in JavaScript. This is a two-person game, human vs. computer.

"Cards" are simple integers, in the range 1 to 10, inclusive.

At the beginning of the game, both the computer player and the human player are dealt a single card, face up. That is, the computer knows what card the human got (though it will probably ignore this information), and the human knows what card the computer got.

Next, the computer is given a chance to get another card, or to pass. The human is told which it did, but not which card it got. Then, the human is given a chance to get another card, or to pass. The computer is told which it did, but not which card it got (the computer player probably ignores this information, but that's up to you, the programmer).

Play continues until there are two passes in a row--that is, either the computer passes and then the human passes, or else the human passes and then the computer passes. Play doesn't stop just because each player has passed--if player A passes but player B does not, then A may still take another card. The goal is to try to get cards that add up to 21, or as close to 21 as possible, without going over 21.

When the game is over, the winner is the player who card sum is closest to 21. If both players go over 21, both lose; if both are under 21 but equal, the game is a tie.

Details:

The game should be in an HTML <form> in a web page, with JavaScript doing all the work. It is up to you where to put the JavaScript (in the web page, or on a separate file). You should have at least:

The program should ask the player for his/her name, and use this to label some of the fields. This should happen only once, not once per game.

Clicking "Play" should clear the general text area that shows what is happening in the game, and start a new game by giving each player a card.

Use a random number generator to deal "cards." Each card is an integer in the range 1 to 10, inclusive, but there are four times as many 10 cards as there are of any other number. (Hint: Get a random number in the range 1 to 13, and if it is greater than 10, reduce it to 10.) Assume that you are drawing cards from a very large deck, so that you don't have to keep track of which cards have been dealt previously.

The computer player can know how many cards the human has taken, and what the first card is, but it doesn't have to use that information. A simple strategy based on the computer's card sum is good enough.

At the end of the game, display (in the text area) the cards that each player has, in the order that they received them. Also display the sums and who won the game, if anyone. Also, update the statistics, which should include:

Again, try for a "reasonable" level of neatness. Using a <table> will probably help.

Due date:

Monday, October 11, before midnight. Probably I'll ask you to turn in your assignment by putting it on the Web, but I haven't decided yet.