COMP 14 Spring 2004
Program 5: Blackjack Solitaire
75 points
Assigned: Wednesday, March 30
Due: Friday, April 8 at 11:59pm
Description
This program is the second in a three-part series that will result in
a GUI-based Blackjack game. (A demo of the FINAL project is at http://www.cs.unc.edu/~stough/teaching/COMP14-S05/demo/blackjack_demo.html.)
In Program 5, you will allow the user to play a GUI-based Blackjack
game alone (with no other players or dealer). A demo of Program 5 is at
http://www.cs.unc.edu/~stough/teaching/COMP14-S05/demo/program5demo/prog5demo.html.)
See the Program 4 instructions for a refresher
on the rules of Blackjack.
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 player's hand and the cards and the current point total should be
displayed. The Deal button will change to Hit, and the
Stand
button will become enabled.
If you click Hit, the card from the top of the deck should be
added to the current hand and the card and the new total point value displayed.
If the point total is Blackjack or a bust (over 21) or there are now 5
cards in the hand, the Hit button will change to Deal, the
Stand
button will be grayed out, and the turn will be over.
If you click Stand, the Hit button will change to
Deal,
the Stand button will be grayed out, and the turn will be over.
The user interface logic (graying out the buttons and displaying
the cards) has been provided for you. Your main jobs are to implement
the Deck and Hand classes and to write the logic needed to add cards to
the player's hand and calculate the current point total.
Program 5 Tasks
You will be given an outline of the files needed to complete this assignment
(see "What to Do"). 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 different from the Blackjack class from Program 4). The only change
you need to make to this file is to add your name and recitation section.
-
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 make no changes to this
class.
-
Card - This class is the same as the one you completed in Program
4. Just put a copy into your Program5 folder and change the Assignment
number from 4 to 5.
-
Deck - This class represents a deck of cards. You should have member
variables that hold an array of cards and indicate the index of the card
at the top of the deck. You should implement the following methods:
-
a constructor that instantiates and initializes the deck of cards and initializes
the index of the top of the deck
-
a cardsLeft method that returns the number of cards left in the
deck
-
a shuffle method that shuffles the deck of cards by swapping random
pairs of cards 1000 times and resets the index of the top of the deck
-
a swap method that swaps given pairs of cards
-
a dealFromTop method that returns the card at the top of the deck
and adjusts the top of the deck marker
-
a toString method that returns a String representing the cards
left in the deck -- useful for debugging your program
-
Hand - This class represents a hand of cards. You should have member
variables that hold an array of cards and indicate how many cards are currently
in the hand. You should implement the following methods:
-
a constructor that instantiates the hand with the given maximum number
of cards and initializes the number of cards in the hand -- the hand should
initially be able to hold the maximum number of cards given, but should
contain 0 cards to start
-
an addCard method that adds the given card to the hand after making
sure that there aren't too many cards in the hand
-
a getNumCards method that returns the current number of cards
in the hand
-
a resetHand method that clears the hand by setting the number
of cards in the hand to 0
-
a getCard method that returns the card at the specified position
in the hand
-
a toString method that returns a String representing the entire
hand -- useful for debugging your program
-
BlackjackGame - This class represents the rules for playing Blackjack.
You should start with the version you wrote for Program 4, keeping the
calcPoints
method that calculates the points for a given Card. As with Program 4,
all of the class members (constants, variables, and methods) in this class
should be declared with the static modifier. You should add the
following:
-
a public constant named MAX_CARDS_IN_HAND that represents the maximum number
of cards that can be in a hand (5 cards)
-
a member variable that represents the deck of cards
-
a member variable that represents the player's hand
-
an initGame method that instantiates the deck of cards, shuffles
the deck of cards, and instantiates the player's hand
-
returns: nothing
-
parameters: none
-
called: at the start of the program from the main method
-
a getPlayerHand method that returns the player's hand
-
returns: a Hand object
-
parameters: none
-
called: from various places inside BlackjackUI and BlackjackApplet
-
a setupNewHand method that reshuffles the deck if needed, resets
the player's hand, and deals 2 cards to the player
-
returns: nothing
-
parameters: none
-
called: from inside BlackjackUI and BlackjackApplet when the Deal button
is pressed
-
a addPlayerCard method that deals a card to the player (by dealing
a card from the top of the deck and adding it to the player's hand)
-
returns: nothing
-
parameters: none
-
called: from inside BlackjackUI and BlackjackApplet when the Hit button
is pressed
-
a playerCanHit method that returns true if the player is eligible
to "hit" (hand point total is less than Blackjack and has less than MAX_CARDS_IN_HAND
cards) and false otherwise
-
returns: true or false
-
parameters: none
-
called: from inside BlackjackUI and BlackjackApplet after either the Deal
button or Hit button are pressed to see if the player is eligible to hit
again
-
a calcPoints method that takes a hand and returns the total number
of points for that hand (adjusted for Aces) -- you may want to use your
previous calcPoints method from inside this method
-
returns: an integer representing the total points for the hand
-
parameters: a Hand object
-
called: from inside BlackjackUI and BlackjackApplet after either the Deal
button or Hit button are pressed to update the point total for the hand
What to Turn in
-
Your prog5_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
-
short write-up of questions you had and/or problems you encountered while
doing this assignment
-
the web address of your applet if you completed the extra credit portion
(see below)
What to Do
-
Follow the directions for completing
programming assignments.
-
Since this assignment will contain multiple Java source files (and we will
be using classes with these same names in Program 6), you should create
a new folder to hold the files. Name this folder something like Program5.
-
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.
-
Save and unzip img.zip into your Program5 folder.
After doing this you should see a folder called img in your Program5 folder
that contains files such as 0.gif, 1.gif, ..., 53.gif. Files 1.gif - 52.gif
each contain the image of a different playing card. File 0.gif contains
the image of a face down card, and file 53.gif contains the image of a
blank card (it's a placeholder).
-
Copy Blackjack.java into your Blackjack.java
file in jGRASP. This file already contains your pledge statement, you only
need to add your name and recitation section.
-
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 not make any changes
to this file.
-
Copy your completed Card.java from Program 4 into your Card.java file in
jGRASP. Change the assignment from "Program 4" to "Program 5".
-
Copy Deck.java into your Deck.java
file in jGRASP. Fill in the appropriate places as described above.
-
Copy Hand.java into your Hand.java
file in jGRASP. Fill in the appropriate places as described above.
-
Copy your completed BlackjackGame.java from Program 4 into your BlackjackGame.java
file in jGRASP. Change the assignment from "Program 4" to "Program 5".
Add the constant, variables, and methods as described above.
Extra Credit
If you use the BlackjackApplet class to create an applet version of
your program (applets let you run your programs over the web -- we'll talk
more about them later), you can receive 10 extra credit points. Here's
what to do:
-
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 BlackjackSolitaire.
-
With BlackjackApplet.java viewable in jGRASP, click Compile (the plus icon).
-
Copy all of the .class files from your Program5 folder to your public_html/BlackjackSolitaire
folder.
-
Save BlackjackSolitaire.html to your
public_html/BlackjackSolitaire 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/BlackjackSolitaire/BlackjackSolitaire.html
To get credit for completing the extra credit, put the web address (URL)
of your applet in the Blackboard comments section when you turn in your
assignment.
Requirements
When the TA runs/examines your program, it must satisfy the following
requirements. 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 from the top of the deck
when the Deal button is pressed (i.e., when
setupNewHand
is called).
-
[10] You must determine the current point total for the hand each time
a new card is added (i.e., when calcPoints is called).
This includes adjusting the total for the presence of Aces when necessary.
-
[10] You must add a card from the top of the deck to the player's hand
each time the Hit button is pressed (i.e, when addPlayerCard
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 5 cards).
-
[10] You must correctly determine when the player is eligible to ask for
another card.
-
[10] You must implement all of the methods that have been specified above.
-
[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.