CS 51 Test Programs #1
Due: Friday, October 18 at 5 p.m.

A test program is a laboratory that you complete on your own, without the help of others. It is a form of take-home exam. You may consult your text, your notes, your lab work, or our on-line examples and web pages, but use of any other source for code is forbidden. You may not discuss these problems with anyone aside from the course instructors. You may only ask the TA's for help with hardware problems or difficulties in retrieving your program from a disk or network.

Complete each of the following problems, documenting your code well. You will find a link to demonstration programs for all three problems. The starter code for all 3 programs can be found as usual in the CS51/labs/TestProgram1 folder. You will want to look at this code before starting the design of your programs, as we provide useful information in many of the startup files.

You are encouraged to reuse the code from your labs or our class examples. Submit your code in the usual way by dragging it into the Dropoff folder for your section. Please do not submit three separate folders. Instead, place the folders for all three of your complete programs into one folder, make sure that your name appears in the title of the main folder and each of the subfolders, and then place the main folder in our dropoff folder.

If you are claiming extra credit for any of the questions, please indicate what you have done clearly in your comments for that question. If you do not claim it, we will likely miss it and you will not get the extra credit.

Problem 1: You're Sooooo Hot

I can remember when I was a kid playing "Hot and Cold." You would hide something in the house somewhere while the "it" person was out. When they came back in, they would search for the hidden treasure to shouts of "You're getting hot." when they when they got close, or "You're getting cold." if they wandered in the wrong direction. The enthusiastic might get creative and say "You're on a block of ice." or "You are burning up." The basic idea, however, was to give the person some clue how well they were doing.

For your first test program, we want you to write a simple, computer version of this game. The hidden item will be a point on the program's window picked at random by the computer. The program won't draw anything on the screen at that point. That would make it too easy to find. Instead, the program's window will be completely blank except for ...

The program will draw a small colored circle on the screen to represent the "it" that is searching for the hidden point. The player will be able to drag this circle around the window using the mouse (as in the laundry program, the basketball programs, etc.). The program will vary the color of this circle to give the player clues about where the hidden point is. When the circle is close to the hidden point it should be bright red. When it is far away, it should be blue. In between, the red should fade from red through various shades of purple until it finally becomes blue.

Each time the player releases the mouse, the program should check to see if the distance between the current position of the mouse and the hidden point is less than the radius of the circle. If it is, the program should display a message congratulating the player's success and then immediately pick a new random point so the player can try again. Otherwise, the computer should reassure (or insult) the player and tell the player to try again without selecting a new point.

There are two "hints" you may need to write this program. First, here is a method that we have not yet used in class that you will want to use for this program. It is named distanceTo and it is an accessor method associated with Locations. If the names point1 and point2 are associated with Locations, then

     point1.distanceTo(point2)

will compute and return the distance (measured in pixels) between the two points.

You will want to use the distance between your hidden point and the mouse location to determine the color of the ball displayed on the screen. Unfortunately, when you make a new Color, the numbers given for the amount of red, green and blue to use must be integers and distanceTo returns a double. Our second hint is how to turn a double into an int. It's simple. You just type "(int)" in front of any expression that describes a double. Java will drop the fractional part of the double to produce an int. For example,

     (int) point1.distanceTo(point2)

will produce an integer approximating the distance between two points.

A demo version should appear below if your web browser supports Java.



Possible extra credit might include having there be a timer to limit the amount of time the user has to find the spot. Another option might be to have the hidden point move to a new location every once in a while (with some sort of warning to the user).

Problem 2: BlackJack

Please implement a simplified version of the card game Blackjack (rules below) by creating a card class whose instances represent a single playing card from a standard deck of 52 cards.

A playing card has two pieces of information: the suit and the rank. The suit is either Hearts, Diamonds, Clubs, or Spades. The rank of the card is a number between 2 and 11. (Note that we've simplified the playing card by removing Aces, Kings, Queens, and Jacks. Instead, a card simply has a number between 2 and 11). The value of a card is just equal to its rank.

The goal of the game is for the player's cards to sum up to more than the dealer's cards but still be less than or equal to 21. If the player's cards total more than 21, then the player has busted (which is bad!) and has lost.

The dealer is dealt one card face up and the other face down. The player is dealt two cards, both face up. The player can ask to be "hit" and get an extra card or to "hold". After this action the game determines who won. If the player's cards total more than 21 then the play loses. Otherwise, if the dealer's cards total more than 21 then the dealer loses. If both totals are less than or equal to 21 then they are compared. If the player's cards total more than the dealer's cards then the player has won. If the player's cards and the dealer's cards have the same total, then there is a draw. Otherwise, the dealer wins.

Your applet should draw a rectangle representing the card table. At the top of the card table there should be displayed two playing cards. These are the dealer's cards. One of the playing cards should be face up (i.e. the suit and rank of the card are showing). The other playing card should be face down (i.e. the suit and rank of the card are not showing). Below this on the table the player should be dealt two cards, face up.

At the very bottom of the card table there should be three buttons: one labeled "Hit Me", one labeled "Hold", and one labelled "Play again". If the player clicks the "Hit me" button then a third playing card is displayed next to their other two cards and the game ends. If the player clicks the "Hold" button , then the game ends without dealing a new card. Ending the game involves (1) revealing the dealer's hidden card and (2) displaying a text message that tells the player whether they won, lost, or drew the game.

After the game ends, your applet should not respond to any clicks the player makes until they press the "Play again" button. When they press play again after the games is over, the existing cards should be erased and new cards displayed for the new game.

Note that the cards dealt (both the dealer's and the player's) should be selected at random. Also, you may assume that you're playing with multiple decks of cards so it's okay for the same card to be dealt more than once.

Please see the sample program below to see how the program should behave.



To help you out, the starter code provided includes declarations of all public methods needed for the Card and Blackjack classes. We have also included the body of the begin method for the Blackjack game as well as the private method initializeCards that is called by the begin method.

Extra credit can be obtained by allowing the player to draw more than one card before selecting hold, allowing the dealer to (automatically) draw extra cards according to the rules of Blackjack, or making the cards more realistic. Another option is to make the frequencies of cards more realistic. That is, the cards with values 10, J, Q, and K in a real deck all give the value of 10. Also, you can use the Ace to have value 1 or 11.

Problem 3: Attack of the Daleks

Doctor Who, the last of the time lords, is an intrepid intergalactic hero. He explores the universe in his Tardis, a time-traveling spaceship disguised as a blue British police box. His arch enemies, the Daleks, are a terrifying and tragic race of aliens mutated into cold, calculating machines known for their battle cry: "Exterminate!"

You will make a game where the player must help the Tardis to escape the attacking Daleks. The game will begin when the player clicks in the applet. Daleks will launch from their mothership, descending from the top of the applet. The Tardis, controlled by the player by moving the mouse, must avoid the Daleks at any cost. If the Tardis should get too close to a Dalek and overlap it, it will be destroyed. Upon destruction, the text "Tardis Down" should appear on the screen and the offending Dalek and the Tardis should freeze on the screen, and blink 3 times. For extra credit, you may play the Dalek battle cry (which is in audio file exterminate.au. To play again, the applet must be reloaded.

Images can be found in files Tardis.jpg and Dalek.jpg.

When the Tardis is destroyed, no more Daleks should be generated, but any Daleks that are falling at the time of the Tardis destruction should continue falling to the bottom of the screen.

To build this game, you will need to define 4 classes. The first, the DalekAttack class will extend WindowController. The second, the DalekMothership, extends ActiveObject and will generate the Daleks very similar to the way the tree generated leaves in our falling leaves example. The third class, the Dalek itself, will also extend ActiveObject and march across the screen looking for the Tardis. The fourth and final class, the Tardis, is the time-traveling blue spaceship that will follow the user's mouse movements.

As with the other test programs, we will be providing you with start up files. You should notice several similarities to programs like the falling leaves and Frogger. Recognizing those similarities will make this program much simpler to do.

Hint: Much like the vehicles and frog in your frogger lab, the Daleks will need access to the Tardis.



Extra credit can be obtained by adding sound effects when the Tardis is hit.

Tentative Grading Point Allocations

Value

Feature
Code Quality (16 pts for each of 3 programs)
2 pts. use of boolean conditions
2 pts. ifs/whiles
2 pts. appropriate vble (instance/local, public/private)
2 pts. Descriptive comments
2 pts. Good names
2 pts. Good use of constants
2 pts. Appropriate formatting (indenting, white space, etc.)
2 pts. Parameters used appropriately
Correctness (16 pts for each of 3 programs)
Hot and Cold
4 pts. Drawing the screen initially
4 pts. Dragging the colored circle
4 pts. Color changes correctly
4 pts. Displays correct message when mouse released
BlackJack
4 pts. Clicking "Hit Me" deals a 3rd card to the player and ends the game
2 pts. Clicking "Hold" immediately ends the game
4 pts. The winner is calculated correctly
4 pts. Clicking "Play again" resets the cards only if a game has already ended
2 pts. Clicking "Hit Me" or "Hold" after the game ends does nothing
Daleks Attack!
4 pts. Tardis moves and is destroyed correctly
4 pts. Daleks move down the screen and collide with Tarkis
4 pts. Continuous creation of Daleks at random positions until Tardis is destroyed
4 pts. Dalek and Tardis stop and blink 3 times upon collision, displaying the "game over" text
Miscellaneous (4 pts total)
Extra Credit (4 pts maximum)