### CPSC 120B Assignment #3 Blackjack with Classes Due, Friday November 19, 2004 by noon

Write a program that plays the game of Blackjack. The main idea of Blackjack is to get a hand of cards that adds up as close as possible to 21 without going over. We will use the following rules:

• There are two players. One is called the dealer and has very strict rules for how to play; the other we'll call the player.
• At the beginning of the game, each player gets two cards. The player gets to see both of his or her cards. One of the dealer's cards is shown.
• After the hands are dealt, the player goes first. If the player was dealt blackjack (an ace and a card valued at 10 points) but the dealer was not, the player wins and the game is over. If both the dealer and the player are dealt blackjack, the game is a tie. Otherwise, the player gets to decide whether to hit or stick (some people say "stand" and some say "stay"). Hit means get another card; stick means to stop taking cards. The player can keep taking cards (hitting) until he/she decides to stay or busts (goes over 21 points).
• If the player busts, he/she loses and the game is over.
• If the player does not bust, it is the dealer's turn.
• The dealer has strict rules that determine when to hit and when to stick. If the current count (total value of the hand) is 16 or less, the dealer hits. On a count of 17 or more, the dealer sticks. If the dealer busts, the player wins. If the dealer does not bust, the two hand counts are compared and the highest one wins. If the counts are the same it is a tie.

Your program should have the following capabilities:

• The player should be either the human user or the computer. The human user will make his or her own decisions about whether to hit or stick. The computer will use a variation on the dealer's rules. Recall that the dealer always hits on counts of 16 or less and always sticks on counts of 17 or more. The computer will choose to sometimes hit and sometimes stick for certain point counts (the choice will be based on randomness). For example, the computer may hit 100% of the time on a count of 12, 90% of the time on a count of 13, 80% of the time on a count of 14, 70% of the time on a count of 15, 60% of the time on a count of 16, and 50% of the time on a count of 17. The computer would always hit for 11 or less and always stick for 18 or more. Use the following algorithm for determining the computer's strategy:
• The computer will always hit if the current count is less than 12.
• When the game is initialized (details below), the user will input the probability of hitting on a 12 (the probability is the fraction of times the computer should hit on a 12), the highest count the computer will hit on, and the probability the computer will hit on that count. In the example above, the probability of hitting on 12 is 1 (100%), the highest count for a hit is 17 and its probability is 0.5 (50%).
• The computer will always stick on a count above the highest count for a hit.
• Suppose the above values are in the following variables: prob12 contains the probability of hitting on a 12, highHit contains the highest count the computer will ever hit on, probHigh contains the probability of hitting on a count of highHit. Then an increment should be calculated as follows (the increment is the decrease in probability for each point higher in the count):
increment = (prob12 - probHigh)/(highHit - 12);

With the numbers above we get (1 - 0.5)/(17 - 12) = 0.1 so the probabilities decrease .1 (10%) for each point above 12.

• To determine whether or not the computer should hit on a count between 12 and highHit use the following algorithm:
double probHit;  // probability of hitting on the current count
// (between 12 and highHit

probHit = prob12 - increment * (count - 12);

// generate a random number between 0 and 1 - say you store it
// in the variable rand
rand = ________________________________;

if (rand < probHit)
computer hits
else
computer sticks

The Program Structure

Your program should have the following structure:

• A Card class to represent a playing card.
• A Hand class to represent a Blackjack hand. Cards in a hand should be represented by Card objects though as explained in the post lab handout they do not need to be stored (the first two can).
• A Player class to represent a player. At a minimum a player should have a name (you may need other things but don't get too carried away). The name will determine the type of player: if the name is "Dealer" the player will be the dealer and play by the dealer's rules; if the name is "Computer" the player will be the computer and play by the computer's rules; otherwise the player will be a person and play interactively. At a minimum, the Player class needs the following methods:
• A constructor that has the name of the player as a parameter. If the player is the computer, then the constructor should prompt for and read in the information for the computer's strategy (the probability of hitting on a 12, the highest count for a hit and its probability).
• A public play method. The play method should have the player's hand as a parameter (that is, the hand of cards is passed to the player object). The play method should call private methods to actually do the playing based on which type of player. So, there would be a method that plays by the dealer's rules, one that plays by the computer's rules, and one that plays interactively (by asking the player what he/she wants to do).

You may need other methods but don't get too carried away.

• Your main program will use the Hand class and the Player class. You will need a hand for each player and you will need two players. One of the players must be the dealer. For the other player ask for a name. If the user enters Computer as the name, the player will be the computer otherwise it will be the human user. The main program should have a loop that lets the play continue (with the same players - ask for the name just once before the loop and continue playing with that player). The program should count the number of player wins, the number of dealer wins, and the number of ties. Those counts should be printed when the user decides to quit.

Sample output (when the player is the human user):

Welcome to the Blackjack Casino!
===============================

Please enter the player's name (or enter Computer): Bozo

Dealing Bozo's hand...
Cards are 8 of Diamonds and 5 of Hearts
Count is 13

Dealing Dealer's hand...
Top card is 10 of Clubs

Bozo's turn...
Do you want to hit or stick (h or s)? h

New card is 6 of Clubs
Count is 19

Do you want to hit or stick (h or s)? s

Dealer's turn...
Bottom card is 4 of Diamonds
Count is 14

Dealer hits...

New card is 3 of Spades
Count is 17

Dealer sticks...

Bozo's count is 19
Dealer's count is 17

Bozo wins!

Play again?

....

Sample output when the player is the computer:

Welcome to the Blackjack Casino!
===============================

Please enter the player's name (or enter Computer): Computer

Enter the probability of hitting on a count of 12: 1
Enter the highest count for a hit: 17
Enter the probability of hitting on a count of 17: 0.5

Dealing Computer's hand...
Cards are 6 of Diamonds and 5 of Hearts
Count is 11

Dealing Dealer's hand...
Top card is 10 of Clubs

Computers's turn...

Computer hits...

New card is 5 of Clubs
Count is 16

Computer hits...

New card is 9 of Spades
Count is 25

Computer busts

Dealer wins!

Play again?

....

Counting the Ace as 1 or 11

In real Blackjack, the ace can have a point value of 11 or 1. Generally it is an 11 unless that makes the total count go over 21. When the total count includes an ace counted as an 11, the count is called soft. If a new card is dealt that puts the count over 21 if the current count is soft 10 points are deducted and the count is no longer soft. In most games the dealer always uses a soft count; that is, the ace is always valued at 11.

You should get your program working correctly always counting the ace as a 1 before getting it to work for 1 or 11 (see grading below).

• Use good programming style -- meaningful variable names, named constants, use of white space, proper indentation (note the proper style for indenting if statements). See the requirements for the last assignment.
• Document your program. Have header documentation at the top of each file that gives your name, the date, the file name, and a description of the purpose of the program or class. Have internal documentation that delineates and succinctly (and clearly) indicates what each method is doing.

Grading Your program will be graded both on style and correctness. Style includes adherence to rules for naming of identifiers and use of white space; it also includes coding style such as the absence of unnecessary code (extra comparisons in if's or extra assignments); it includes good class design (appropriate choice of instance variables and methods); it includes appropriate documentation. Style will be 20% of the grade on the assignment. Correctness includes meeting all the specifications of the assignment. A program that does not compile has an automatic deduction of 70%; one that does not run has an automatic deduction of 40% no matter how "close" it is to compiling or running. Come see me if you have problems with the assignment. It is important that you get in the habit of producing correct programs!

Working Strategy

• First get a basic Card class and Hand class working (this is the Postlab substitute due Friday).
• Next get the Player class and main program working for a human player and the dealer. This much working and correct will be worth 70%.
• Add the computer player capabilities. (Worth 15 points)
• Add the soft count capabilities (ace worth 1 or 11). (Worth 15 points)

Academic Integrity Reminder!!! Programming assignments are to be your own work. You may get help on the specifics of the assignment from no one except the instructor. You may not show your program to anyone or look at anyone else's program or share ideas with anyone about how to write the program.

Hand IN: A printed copy of your program files. Tar the .class, .html, and .java files and email the .tgz file to ingram@roanoke.edu with a subject of cpsc120 project3.