COMP 14 Spring 2004
Program 6: Blackjack
100 points
Assigned: Wednesday, April 6
Due: Wednesday, April 20 at 11:59pm
Goal
This is our final program of the semester. By completing this assignment,
you will have demonstrated your ability to design and implement a solution
to a problem using the programming constructs that you have learned throughout
the semester. Additionally, because one of the requirements of this assignment
is to create an applet version of Blackjack, you will be able to share
your finished project with friends and family via the Internet.
Description
This program is the third in a three-part series resulting in a GUI-based
Blackjack game that a user can play against the dealer. A demo of the final
project (Program 6) is at http://www.cs.unc.edu/~stough/teaching/COMP14-S05/demo/blackjack_demo.html.
Report
25 points of this assignment will be given based on a written report.
Your report should be written in HTML, put in your webspace, and contain
a description of the Card, Deck, and Hand classes and how these classes
work together. Your report should also describe the logic used in scoring
the Blackjack hand. Finally, your report should contain questions, comments,
or interesting things you learned while working on this project (including
Programs 4-6).
Maximum length: 300 words
Blackjack
In Blackjack, the points for numerical face values are the same as the
face value (e.g., the 5 of Diamonds is worth 5 points). Jack, Queen, and
King cards are worth 10 points each. Ace cards are worth either 11 points
or 1 point, depending on the situation.
Blackjack is usually played against a dealer. The object of the game
is to have the group of cards (called a "hand") which has the closest point
total to 21 (called "Blackjack") without going over (called a "bust").
In a game against the dealer, the player is initially dealt two cards
face-up and the dealer is dealt two cards, the 2nd of which is dealt face-down.
The player can then decide to ask for additional cards one at a time (called
a "hit") or stop receiving cards (called a "stand").
If the player "busts", the dealer automatically wins. Otherwise, once
the player has decided to "stand", the dealer plays out the hand. If the
dealer's hand point total is less than 17, the dealer must "hit". If the
dealer's hand point total is 17 or greater, the dealer must "stand".
If the dealer "busts", the player automatically wins. Otherwise, once
the dealer has decided to "stand", the winner is determined. The winner
has the hand closest to Blackjack (21). A tie results if both the player
and the dealer have the same number of points.
In our game, there are a maximum of 5 cards that can be dealt to either
the player or the dealer. This is due to limitations in the user interface.
Once the player or the dealer has 5 cards, they are not allowed to "hit".
Sample Output
When you start the program, a blank table with buttons will be displayed:
After clicking Deal, the top 2 cards from the deck will be added
to the dealer's hand, and the cards will be displayed (the 2nd card will
be shown face-down), along with the point total of the visible card. Then,
the next 2 cards from the deck will be added to the player's hand, and
the cards and the current point total of the player's hand will be displayed.
The Deal button will change to Hit, and the Stand
button will become enabled.
If the player clicks Hit, the card from the top of the deck will
be added to the player's hand, and the card and the new total point value
will be displayed. If the point total is Blackjack or a bust (over 21)
or there are now 5 cards in the player's hand, the Hit button will
change to Deal, the Stand button will be grayed out, and
the player's turn will be over. If the player did not bust, the dealer
will play out the hand according to the rules given above. The dealer's
cards and final point total will be revealed. Finally, the tally of player
wins, losses, and ties will be updated depending on who won the game.
If the player clicks Stand, the Hit button will change
to
Deal, the Stand button will be grayed out, and the player's
turn will be over. The dealer will then play out the hand according to
the rules given above. The dealer's cards and final point total will be
revealed. Finally, the tally of player wins, losses, and ties will be updated
depending on who won the game.
The user interface logic (graying out the buttons, displaying the cards,
point value, and tally) has been provided for you. Your main jobs are to
control the dealer's hand, player's hand, calculate the point value for
a hand, and tally the total wins, losses, and ties.
User Interface Operation
When the Deal button is pressed, the user interface will call
the following methods from your code (see the dealButtonPressed
method in BlackjackUI.java for details):
-
BlackjackGame.setupNewGame
-
BlackjackGame.getPlayerHand
-
getNumCards from Hand class
-
getCard from Hand class
-
BlackjackGame.calcPoints
-
BlackjackGame.getDealerHand
-
BlackjackGame.playerCanHit
When the Hit button is pressed, the user interface will call the
following methods from your code (see the hitButtonPressed method
in BlackjackUI.java for details):
-
BlackjackGame.addPlayerCard
-
BlackjackGame.getPlayerHand
-
getNumCards from Hand class
-
getCard from Hand class
-
BlackjackGame.calcPoints
-
BlackjackGame.playerCanHit
When the Stand button is pressed, the user interface will call the
following methods from your code (see the standButtonPressed method
in BlackjackUI.java for details):
-
BlackjackGame.addDealerCards
When the player is done with their hand, the playerDone method in BlackjackUI
will be called. This method will call the following methods from your code
(see the playerDone method in BlackjackUI.java for details):
-
BlackjackGame.getDealerHand
-
getNumCards from Hand class
-
getCard from Hand class
-
BlackjackGame.calcPoints
-
BlackjackGame.decideWinner
-
BlackjackGame.getWins
-
BlackjackGame.getLosses
-
BlackjackGame.getTies
In addition to these, BlackjackGame.initGame is called from the
main
method in Blackjack.java and from the init method in BlackjackApplet.java.
Program 6 Tasks
As with Program 5, you will have a total of 7 classes in this program.
Each one is described below:
-
Blackjack - This class contains only the main method and
is the same as in Program 5. The only changes you need to make to this
file are to change the description of the program, input, and output.
-
BlackjackUI - This class contains all of the code to display the
user interface. You should make no changes to this class.
-
BlackjackApplet - This class contains all of the code to display
the user interface as an applet. You should change only the IMG_URL
constant (as described below in "Making an Applet").
-
Card - This class is the same as the one you completed in Program
5. Just put a copy into your Program6 folder and change the Assignment
number from 5 to 6.
-
Deck - This class is the same as the one you completed in Program
5. Just put a copy into your Program6 folder and change the Assignment
number from 5 to 6.
-
Hand - This class is the same as the one you completed in Program
5. Just put a copy into your Program6 folder and change the Assignment
number from 5 to 6.
-
BlackjackGame - This class represents the rules for playing Blackjack.
You should start with the version you wrote for Program 5. As with Program
5, all of the class members (constants, variables, and methods) in this
class should be declared with the static modifier. You should
add the following:
-
three public constant integers named DEALER, PLAYER, and TIE -- all set
to different values (to distinguish who wins the hand)
-
a class variable that represents the dealer's hand (a class variable is
just a member variable that's declared as static)
-
class variables to keep track of the number of wins, losses, and ties
-
a getDealerHand method that takes no parameters and returns the
dealer's hand
-
a getWins method that takes no parameters and returns the number
of wins
-
a getLosses method that takes no parameters and returns the number
of losses
-
a getTies method that takes no parameters and returns the number
of ties
-
an addDealerCards method that takes no parameters, returns nothing,
and adds cards to the dealer's hand as long as the dealer must hit (according
to the rules given above)
-
a decideWinner method that takes no parameters, returns an integer
(DEALER, PLAYER, or TIE constant) indicating if the dealer won, the player
won, or the dealer and player tied -- this method should also update the
number of wins, losses, and ties
You should make the following changes:
-
initGame
-
instantiate the dealer's hand, as well as the player's hand
-
initialize the number of wins, losses, and ties
-
setupNewHand
-
make sure that the deck can deal out 2 full hands (one for the player and
one for the dealer) without needing to be shuffled
-
reset the dealer's hand, as well as the player's hand
-
deal two cards to the dealer, as well as two cards to the player
Making an Applet
-
First, you will need to setup your UNC web space (see http://onyen.unc.edu).
-
Once you have setup your web space, you will have a folder named public_html
in your AFS space (your H: drive). Make a new folder inside public_html
called Blackjack.
Note: If you do not have the AFS client and wish to access AFS
space away from the lab, you can download the WS_FTP program from shareware.unc.edu.
This program will allow you to transfer files from your computer to your
AFS space. Once you have WS_FTP installed, you can access your AFS space
by using isis.unc.edu as your remote host and your Onyen as your remote
username.
-
Copy the img folder (from Program 5) to your public_html folder.
-
In BlackjackApplet.java, change the IMG_URL constant to the location of
the img folder, likely:
private final static String IMG_URL = "http://www.unc.edu/~onyen/img";
where you replace onyen with your Onyen.
-
With BlackjackApplet.java viewable in jGRASP, click Compile (the plus icon).
-
Copy all of the .class files from your Program6 folder to your public_html/Blackjack
folder.
-
Save Blackjack.html to your public_html/Blackjack
folder. Note: You'll need to right-click on the link and choose
"Save Target As..." instead of just clicking on the link.
-
To view your applet, go to http://www.unc.edu/~onyen/Blackjack/Blackjack.html
What to Do
-
Follow the directions for completing programming
assignments.
-
Since this assignment will contain multiple Java source files (and we will
be used classes with these same names in Program 5), you should create
a new folder to hold the files. Name this folder something like Program6.
-
For this assignment, you should have seven classes (which means
seven Java source files): Card.java, Deck.java, Hand.java, BlackjackGame.java,
BlackjackUI.java, BlackjackApplet.java, and Blackjack.java.
-
Copy your img folder from your Program5 folder into your Program6 folder.
-
Copy your completed Card.java from Program 5 into your Card.java file in
jGRASP. Change the assignment from "Program 5" to "Program 6".
-
Copy your completed Deck.java from Program 5 into your Deck.java file in
jGRASP. Change the assignment from "Program 5" to "Program 6".
-
Copy your completed Hand.java from Program 5 into your Hand.java file in
jGRASP. Change the assignment from "Program 5" to "Program 6".
-
Copy your completed Blackjack.java from Program 5 into your Blackjack.java
file in jGRASP. Change the assignment number, due date, description, input,
and output to reflect the current assignment.
-
Copy BlackjackUI.java into your BlackjackUI.java
file in jGRASP. You should not make any changes to this file.
-
Copy BlackjackApplet.java into
your BlackjackApplet.java file in jGRASP. You should change only
the IMG_URL constant (as described above in "Making an Applet").
-
Copy your completed BlackjackGame.java from Program 5 into your BlackjackGame.java
file in jGRASP. Change the assignment from "Program 5" to "Program 6".
Make the changes described above.
-
Once your program is working from jGRASP, follow the instructions for making
an applet that are given above. Put the web address (URL) of your applet
in the Blackboard comments section when you turn in your assignment.
-
Using Notepad, write your report in HTML and save it as "report.html".
Copy this file to your web space in your public_html/Blackjack folder in
AFS space. Put the web address (URL) of your report in the Blackboard comments
section when you turn in your assignment. Do not use any web design
software (including Microsoft Word) to write your report. Check
your report by going to http://www.unc.edu/~onyen/Blackjack/report.html
What to Turn in
-
Your prog6_onyen.jar file (where onyen is your Onyen), which
includes your Card.java, Hand.java, Deck.java, BlackjackGame.java, BlackjackUI.java,
BlackjackApplet.java, and Blackjack.java source files
-
the web address (URL) of your applet (in the "Comments" section in Blackboard
when you're submitting your assignment)
-
the web address (URL) of your report (in the "Comments" section in Blackboard
when you're submitting your assignment)
Requirements
When the TA runs/examines your program, it must satisfy the following
requirements (which are in addition to the requirements from Program
5). The maximum point value for each is shown in brackets.
-
[5] Your Java source files, classes, and Jar file must be appropriately
named (as specified above).
-
[10] You must add two cards to the player's hand and two cards to the dealer's
hand from the top of the deck when the Deal button is pressed (i.e.,
when setupNewHand is called).
-
[10] You must re-shuffle the deck when there are not enough cards available
for an entire turn to be played (an entire turn may require dealing at
most 10 cards).
-
[10] You must correctly play out the dealer's hand after the player has
decided to stand.
-
[10] You must correctly determine the winner.
-
[10] You must keep track of the total number of wins, losses, and ties.
-
[10] You must create an applet version of your program and provide the
URL when submitting your assignment.
-
[25] You must write a report in HTML (without using web design or authoring
software) and provide the URL when submitting your assignment.
-
[5] You must use variables and methods with meaningful names.
-
[5] You must comment your code, including block-like multi-line comments
and single-line comments where appropriate. In addition, your code must
be neatly and clearly formatted using appropriate "white space."
Notes:
-
Remember that an implicit requirement for all assignments is that your
code must include (at the top) the standard
program header with pledge, and your signed pledge must be on file.