V22.0101 Homework Assignment 3 Fall 1997
Blackjack

Assigned: Tue Oct 14
Due: Thu Oct 30
Midterm Exam: Tue Oct 21

Blackjack

Blackjack is a card game, where you are the player, and the computer is the dealer. The rules are simplified for this assignment. The goal of the game is to get as high a score as possible without going over 21 (going ``bust"). The computer draws each card using two calls to the random number generator: once for the rank of the card (a number 1-13), and once for the suit (1-4). The value of the suit is irrelevant to the game. Ranks 2 through 10 count 2 through 10 points, face cards (11, 12, 13) count 10 points, and an ace (1) counts 11. (In the real game aces count 1 or 11, but that makes things too complicated, so we assume here aces count only 11.) Notice that each card seems to be drawn from a fresh deck: it is possible for a card to repeat any number of times.

The computer begins by dealing two cards to you, the player, and then two cards to itself. It shows you both of your cards, but it only shows you one of its cards. It then repeatedly gives you the option of drawing another card, until either you decline, or you go bust. It then deals cards to itself. The computer must draw until it reaches at least 17 points, and must not draw any cards once its total reaches 17 (it ``hits" 16 and ``stands on'' 17).

If neither you nor the computer go bust, the winner of the game is the one with the most points. If both point totals are the same, the game is a tie. You lose immediately if you go bust. This is the "house advantage", and is why casinos make money. In this case no more cards are dealt. If you do not go bust but the computer does, then you win.

The program should prompt you (the user) for various responses as the game is played. You start with a certain amount of money and are not allowed to bet more than you currently have. If the game is a tie, you get your money back, i.e. finish with the same amount you started with.

A big difference between this assignment and the previous assignment is that the structure of the program is left entirely to you. You should make good use of procedures, using variable parameters when necessary. Be sure that each procedure carries out a specific task, and that this task is clearly explained by the comments. Avoid accessing variables as "global variables": procedures should use only their parameters and their own local variables. Try not to make any procedure too long; if it is getting complicated, it can probably be broken up into smaller components. Think carefully about how to organize the program before you start coding.

Note: the randomize procedure, which resets the random number generator using the computer's clock, should be called only if you want the program to generate different random numbers each time you run it, and then it should only be called once. On the other hand, you may want to have the program generate the same random numbers every time you run it: this can be accomplished by setting a value to the pre-declared variable randseed, like this: read(randseed). The first thing your program should do is ask the user whether he/she wants to input a value for randseed, or wants randomize to be called, initializing the random number generator with the clock. This way the user can decide whether to run the program with the same random numbers generated every time, for debugging and grading purposes, or with different random numbers each time (for further experimentation).

Here is some typical output: you can use any format you like.

WELCOME TO TURBO BLACKJACK!

(some introductory remarks or instructions)

Would you like to enter a value for the random generator seed (y/n) ? n
Since you said no, would you like to initialize the random generator with the clock instead (y/n) ? y

You have \$100 dollars. How much would you like to bet? \$40

Press return key to deal cards

You are dealt:
Nine of Clubs
Ace of Clubs

Computer is dealt:
first card hidden
Two of Diamonds

Your score so far is 20
Do you want another card (y/n) ? n

Computer's hidden card was Ten of Spades
Computer's score so far is 12
Computer draws another card:
Jack of Hearts
Computer went bust.

Congratulations! You won the game.
You won \$40.
You now have \$140.

Would you like to play again(y/n)? y

==================================================================

You have \$140 dollars. How much would you like to bet? \$100
Press return key to deal the cards.

You are dealt:
Three of Hearts
Four of Diamonds

Computer is dealt:
first card hidden

Your score so far is 7
Do you want another card (y/n) ? y

You are dealt:
Two of Clubs
Your score so far is 9
Do you want another card (y/n) ? y

You are dealt:
Ace of Clubs
Your score so far is 20
Do you want another card (y/n) ? n

Computer's hidden card was Seven of Hearts
Computer's score so far is 15
Computer draws another card:
Five of Hearts
Computer's score so far is 20

The computer stands (no more cards)

TIE GAME ! !

You get your money back since it was a tie.
You now have \$140.

Would you like to play again(y/n)? n

==================================================================

Bye. You won \$40.

Make certain that your program is well-documented both within your program in comments, and on the screen when it is run.

Michael Overton
Sun Oct 12 12:39:46 EDT 1997