Due Dates:
Design: Tuesday, May 14, at midnight
turnin to Design5 the file design5.txt to cpe10104 (8-9:30 section) or to cpe10105 (12-1:30 section)Program: Sunday, May 19, at midnight
turnin to Program5 the files Blackjack.java and Play.java to cpe10104 (8-9:30 section) or to cpe10105 (12-1:30 section)
Remember, NO LATE PROGRAMS!The Assignment:
For this assignment I want you to create a class for playing a blackjack hand. In blackjack, the object is to get a hand which adds up to as close as possible to 21, without going over. Initially two cards are dealt. Face cards have value 10, numeric cards have their number value, and an ace can be counted as either a 1 or an 11, your choice. If the two cards are, for instance, a king and an ace, congratulations - you've won (except in the simplified version we'll write). If they are a king and a 3, you'll probably ask the dealer to "hit" you with another card. If that card is a 9, you've busted - you're over 21. If it's a two, you have a tough choice - you can get another card but anything over a 6 will bust you.Steps:For this assignment I want you to write a Blackjack class, which contains variables and methods related to the game. You also must write a Play program that uses the Blackjack class to play a few hands of the game. Also, we'll simplify the game a bit by making an ace count 1 no matter what, and we'll assume that there will be no more than five cards in a hand. Also we won't worry about making sure that the cards come from a legal deck - in other words, the random number generator could deal five 2's in a row, even though a real deck only has four. Don't worry about that.
First, the Blackjack class. I suggest five integer variables to represent the maximum five cards. You'll probably need another member variable that keeps track of how many cards have been dealt. Here are the methods you'll need (I'll leave the names up to you):
1) a method to deal the initial two cards. Use the random function to generate two random integers between 1 and 13 (11 for jack, 12 for queen, 13 for king).
2) a method to deal a single card.
3) a method to display the hand. Call this method toString( ), which is a standard Java method. The deal methods just generate the cards; they DON'T display them. Display a hand by using the print or println methods to call the toString( ) method of Blackjack - don't do the printout directly within the toString( ) method. The string should contain the card numbers (for numeric cards) or Jack, Queen, King for the face cards. Here is an example:
4, Jack, 6
4) a method to evaluate the hand. This method adds up the card values and returns True if the hand is 21 or under or False if it's over 21.
5) a method to return a value that indicates whether no more cards can be dealt - if the fifth card has been dealt it returns True; otherwise it returns False. A True value indicates that the user does not have the choice of taking another card.
Now, this may look like a lot of work. Do it in stages as you develop your Play.java program. Here's how I recommend to do it:
Step 1) Write the Blackjack class with the member variables and two methods - the initial deal method and the display hand method. Write the Play program that instantiates a Blackjack object, calls the initial deal method, and calls the display hand method. Compile them both, just like the Triangle class and the Driver class in lab, and run the Play program. Make sure that the result is two random cards. Run it several times to ensure this. Here's what your output might look like at this stage:
> java Play
Feeling lucky?
Your hand contains the cards 1, King
Thanks for playing!
>Step 2) Add the method to deal another card to the Blackjack class. Add the code to the Play program that asks the user to type in a 'y' if he or she wants another card and calls this method after it displays the initial hand. Display the hand after the card is dealt. Here is what your output might look like:
> java Play
Feeling lucky?
Your hand contains the cards Queen, 3
Do you want another card (y or n)? y
Your hand contains the cards Queen, 3, King
Thanks for playing!
>Note that ONLY the toString( ) method in the Blackjack class creates a string to be printed out (but print( ) or println( ) isn't actually called from within the toString( ) method). The Feeling Lucky, Do you want another card, and Thanks for playing! print statements are in the Play program, and the readChar( ) that reads the y or n keystroke is also in the Play program.
Step 3) Now add a loop in the Play program that lets the user take as many cards as desired (up to five total). Note that you're not checking to see if the hand is busted or not - that method hasn't yet been added to the Blackjack class. Now your printout might look like this:
> java Play
Feeling lucky?
Your hand contains the cards Queen, 3
Do you want another card (y or n)? y
Your hand contains the cards Queen, 3, King
Do you want another card (y or n)? y
Your hand contains the cards Queen, 3, King, 7
Do you want another card (y or n)? n
Thanks for playing!
>Step 4) Now add the evaluation method to Blackjack and call the method from Play after each deal. If the hand is legal, just print Do you want another card as usual. If it's over 21 print the Busted! message. Now your printout might look like this:
> java Play
Feeling lucky?
Your hand contains the cards Queen, 3
Do you want another card (y or n)? y
Your hand contains the cards Queen, 3, King
Sorry, you busted!
Thanks for playing!
>Step 5) In the Play program, add another loop around everything that asks the player if he or she wants to play another hand after busting or being dealt five cards. Now you might see:
> java Play
Feeling lucky?
Your hand contains the cards Queen, 3
Do you want another card (y or n)? y
Your hand contains the cards Queen, 3, King
Sorry, you busted!
Would you like to play another hand (y or n) y
Your hand contains the cards 10, 5
Do you want another card (y or n)? y
Your hand contains the cards 10, 5, 2
Do you want another card (y or n)? n
Congratulations - you didn't bust!
Would you like to play another hand (y or n) n
Thanks for playing!
>Each of these five stages is worth 20% of the total points.
0) DO NOT EMAIL your program to me or the grader account! Use the turnin procedure outlined below.
1) Create your program using PFE or any other text editor such as Notepad on a PC, either yours at home or in the lab.
2) Compile, test, and modify your program as described above with appletviewer or your browser.
3) Once the program runs correctly, transfer it to your Central UNIX account using WS-FTP.
4) Turn in your program to the appropriate grader account using the following procedure. If you are in the 8-9:30 section turn it into the grader account cpe10104. If you are in the 12-1:30 section turn it into the grader account cpe10105. The following example shows how to turn in the program:
Transfer the files Blackjack.java and Play.java (NOT the files Blackjack.class and Play.class!) to your Unix account using WS_FTP. Login to your Unix account using QVT_term. Change to the Unix directory (folder) where you placed the files from WS_FTP.
Do this by typing the Unix command (where you should substitute the directory name you've chosen):
% cd cpe101Run a turnin script program (you must type EXACTLY as shown, the first character is a tilde):
% ~buckalew/bin/turninFollow the prompt messages and turnin all your files.
Here is a sample session. Text in red is what you must type. Where it shows the 101 section, you should enter csc10104 or csc10105 depending on which section you are in.
You must type EXACTLY as shown here, including capitalization and your correct section number. Failure to do so may result in not getting credit for this programming exercise.% ~buckalew/bin/turnin
Enter the e-mail address of the instructor or class account that
you wish to mail this assignment to > cpe10104 (or cpe10105 )
Enter the name of this assignment > Program5Enter the name of the first file to be included.
If no more files, just press RETURN key > Blackjack.javaEnter the name of the first file to be included.
If no more files, just press RETURN key > Play.javaEnter the name of the next file to be included.
If no more files, just press RETURN key >Your assignment has been sent. If you do not receive a confirmation
in the next few minutes, please contact your instructor
%Within a short time, you should receive an email message confirming that your files were successfully received. Usually this takes 1 or 2 minutes, though when Unix system is very busy, this can take several minutes! NOTE: the email will be sent to your Cal Poly UNIX email address, not your personal email (e.g., at a service bureau). You will have to read your email on Cal Poly Unix (or wherever you have set that mail to be auto-forwarded to). Auto-forward is set by the contents of a file named .forward in your Unix home directory (note file name begins with "dot"). For most of you, this is set to forward to your Cal Poly OpenMail account unless you have changed or removed your .forward file.