CPSC 120 Assignment #4 - Part II
Corrected Part I and Part II Due Friday December 5, 2008 by 4 p.m.
In this part of the assignment you will add two capabilities to
your Blackjack classes. One is to include the "Five card charlie" (also
called the "Five card trick") rule that a player
wins automatically if he/she has 5 cards with a point count of
21 or less. The other capability is for the player to be a computer.
You will also write a client program that plays the game multiple
times with the computer versus the dealer and counts the times the
computer wins, the dealer wins, and the two tied. You will run your
program many times to see if you can find a computer strategy that
is better than the dealer strategy.
The computer as a player:
There would be several different ways you could
add capabilities to your Player class for the player to be a computer
but we will have the computer use
a variation on the dealer's rules. (NOTE: You must implement this
type of computer player. If you wish you can add another player
type to the Player class where the computer is a player with
rules you make up.) 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
- 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 - so that would be 1 to always
hit; it would be 0.8 to hit 80% of the time), 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%).
Linear interpolation will be used to find the probabilities for
the values between 12 and the highest count the computer will hit
- 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. (Note: If
the user says the highest card the computer should hit on is
12 this formula gives a 0 in the denominator! So, to avoid that
you should set the increment to 0 if the highHit equals 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 = Math.random();
if (rand < probHit)
Working Strategy and Requirements
As usual, your program will be graded both on style and correctness.
The document that shows your results
from running the program and gives your conclusion about the
best strategy will be 30% of the part II grade.
- First correct any problems you had with your program in Part I.
If you have questions on doing this you should see your instructor.
Also you may "buy" correct code from your instructor. Doing that
will cost 25% of the grade on this assignment. That is, if you want
to use a version of Part I provided by the instructor, you can
make at most a 75% on Part II.
- Next add the "Five card charlie" capability. This will involve adding
an instance variable to one of the classes (which one?), initializing
and updating it in the appropriate places in the class, and providing
a getter method.
- Update your current program to also check to see
if the player won according to the 5-card rule.
- Now add to the Player class the capability that the player is
a computer. You will need to add the following:
- Some instance variables (think about what is needed).
- A second constructor that takes in at least 3 parameters:
the probability that
the computer hits on a 12, the highest point total the computer will
hit on, and the probability the computer hits on that point total.
The constructor could also take in a name if you want to give the
computer a name (otherwise make the name "Computer").
The constructor will initialize the player to be a player of
type computer and use the values taken in to initialize other
- A method that simulates the computer taking a turn. It
is similar to the other take-turn methods except that it uses the computer's
rules for deciding when to hit and when to stick.
- Finally write a client program (you can reuse much of your other
one but make it a new program with a different name)
that asks the user for the information about
the computer's strategies and uses the new constructor to instantiate
a computer player. Of course a dealer is instantiated as before.
The program should also read in
the number of times to play. After playing that many
times it should print the number of times the computer wins,
the number of times the dealer wins, and the number of ties.
- After things work right run your program many times with the
goal of finding a good strategy (good probabilities) for the computer.
You must at least do the following and record the results (clearly
- First run the program with the computer playing according to
the dealer's rules - this would be always hitting on 12 (probability
of 1), 16 as the highest count for a hit and a probability of
1 for hitting on a 16. When you run the program have it
play at least a 100 times. Repeat this at least 4 times.
- Now run your program with at least 5 different strategies. For each
run have the computer play at least 100 times on a single run.
Each strategy should be repeated at least 4 times.
- Decide which of the strategies you used is best and write a
justification of your choice.
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.
and the results of your runs and your conclusions
for part II.
Tar all of the assignment files (both parts of the
assignment - everything should be in an assign4 directory)
and email the .tgz file
to your instructor with a subject of cpsc120 assn4 part II.