CSC 102 Program Assignment 1

Due Dates:

Design: Wednesday, Jan 17, at midnight
        turnin the file design1.txt to csc10208 as Design1

Program: Friday, Jan 19, at midnight
        turnin the file Blackjack.java to csc10208 as Program1

Remember, NO LATE PROGRAMS!
The Assignment:
For your first 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.  Two cards are dealt, one down and one up.  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.  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.

For this assignment I want you to write a Blackjack class, which contains variables and methods useful for the game.  You also must write a Play program that uses the Blackjack class to play a few hands of the game.  Everything will be done from the DOS or UNIX prompt, and output will be in the DOS or QVT-term window - no fancy applets or graphics to worry about.  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 an array of ints 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.  The deal methods just generate the cards; they DON'T display them.  Display a hand by using the print or println methods to print the card numbers (for numeric cards) or Jack, Queen, King for the face cards.  Here is an example printout:

Your hand contains the cards:
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 display a message depending on whether you've busted or not - "Another card?" if not or "Sorry, you're busted!" if so.  If the fifth card has already been dealt display "Another hand?"

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 display hand method prints anything out in the Blackjack class.  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.
 

Design and Header Information:
 
For each method, I want three things listed: the method's inputs (parameters or user inputs), the method's outputs (return values, modified references, or printouts), and the steps, written in English, of the method's algorithm.  This is what I call the design.  Turn it in the Tuesday before the program is due, and include it as comments in the headers of each method.  Here's an example for the method to evaluate the blackjack hand:

Inputs: none
Outputs: True if the hand totals 21 or less, False if the hand totals more than 21.
Steps:   Set Sum to 0.
            For each card, if the card's value is 10 or less, add the value to Sum.  If the card's value is more than 10, add 10 to Sum.
            If Sum is 21 or less, return True.  If Sum is more than 21, return False.
 

Steps:
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.
3) Once the program runs correctly, transfer it to your Central UNIX account using WS-FTP.
4) Turn in your program to the csc10108 grader account using the following procedure.  The following example shows how to turn in the program:
 
    1. Transfer the files Blackjack.java and Play.java (NOT the files Blackjack.class and Play.class!) to your Unix account using WS_FTP.
    2. Login to your Unix account using QVT_term.
    3. Change to the Unix directory (folder) where you placed the files from WS_FTP.

    4. Do this by typing the Unix command (where you should substitute the directory name you've chosen):
      % cd csc102
    5. Run a turnin script program (you must type EXACTLY as shown, the first character is a tilde):

    6. % ~buckalew/bin/turnin
    7. Follow the prompt messages and turnin all your files.

    8. Here is a sample session. Text in red is what you must type.
      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 > csc10208
      Enter the name of this assignment > Program1

      Enter the name of the first file to be included.
      If no more files, just press RETURN key > Blackjack.java

      Enter the name of the first file to be included.
      If no more files, just press RETURN key > Play.java

      Enter 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 an ISP). 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.