CS117-HW032.zip

CS117-HW03/Suit.java

CS117-HW03/Suit.java

import  java . util . Random ;

/**
 * Represents spades, hearts, clubs, or diamonds.
 * 
 *  @author  
 *  @version  
 */
public  enum  Suit   {
     /** Spades. */
    SPADES ( "Spades" ),
     /** Hearts. */
    HEARTS ( "Hearts" ),
     /** Clubs. */
    CLUBS ( "Clubs" ),
     /** Diamonds. */
    DIAMONDS ( "Diamonds" );
    
     /** The name of this Suit. */
     private   String  name ;
    
     /**
     * Constructs a new Suit.
     * 
     *  @param  theName The name of the new Suit.
     */
     private   Suit ( String  theName )   {
        name  =  theName ;
     }
    
     /**
     * Returns the name of this Suit.
     * 
     *  @return  The name of this Suit.
     */
     public   String  getName ()   {
         return  name ;
     }
    
     /**
     * Gets a randomly-chosen Suit.
     * 
     *  @return  A randomly-chosen Suit.
     */
     public   static   Suit  getRandomSuit ()   {
         // This class makes random numbers for us.  Make sure you import it!
         Random  random  =   new   Random ();
         // Remember, all enumerations have this method automatically.
         Suit []  allSuits  =   Suit . values ();
         // This gets me a random number between 0 and the number of suits.
         int  index  =  random . nextInt ( allSuits . length );
         // This returns whichever suit was randomly chosen.
         return  allSuits [ index ];
     }
}

__MACOSX/CS117-HW03/._Suit.java

CS117-HW03/2018spring-cs117-hw03-assignment.pdf

Name CS117 - Homework 03 – Page 1

Homework 03: Blackjack

Assigned: Friday, February 9, 2018 Due: Friday, February 16, 2018 at 6:00am

1 Background: Playing Cards

There are many games that can be played with a deck of cards. Most playing cards have two features:

• A suit, which is either Spades, Hearts, Clubs, or Diamonds

• A denomination, which is either Ace (A), 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack (J), Queen (Q), or King (K)

We are going to create classes to represent playing cards, and then implement a modified version of the Blackjack game, which is explained later.

2 Preparation

Download the 2018spring-cs117-hw03.zip file from Moodle, and unzip it to find the directory CS117-HW03. Open that project in BlueJ. You will find that the project already contains a Suit

enumeration that I have written.

3 Assigment: Denomination Class

In this part of the assignment, we will create an enumeration that represents the possible denomi- nations on a playing card.

1. (3 points) Create a new enumeration named Denomination. Clear out the auto-generated code and write your own Javadoc for the class.

2. (10 points) Create the 13 objects that can exist of this enumeration. Use names like ACE and TWO.

3. Create two fields:

(a) (4 points) One that stores the one-letter name of the denomination (like "A" or "2")

(b) (4 points) One that stores the value of the card in the game of Blackjack:

• The numbered cards have the same value as their number.

• The Jack, Queen, and King cards have a value of 10.

• The Ace card has a value of 11. (In the real Blackjack game they can also be 1, but we will not do this.)

4. (5 points) Create a private constructor with two parameters to match the fields.

Name CS117 - Homework 03 – Page 2

5. (5 points) Create accessors for the two fields.

6. (5 points) Create a static method that returns a random denomination. See the getRandomSuit method in the Suit class for an example of how to do this.

4 Assignment: PlayingCard Class

In this part of the assignment, we will create a class that represents a playing card.

1. (3 points) Create a new class named PlayingCard. Clear out the auto-generated code and write your own Javadoc for the class.

2. Create two fields:

(a) (4 points) One that stores the suit of the card.

(b) (4 points) One that stores the denomination of the card.

3. (5 points) Create a constructor with two parameters to match the fields.

4. (4 points) Create an accessor for the card’s suit.

5. (4 points) Create an accessor for the card’s denomination.

6. (6 points) Create a method named toString that returns a string representation of the card, like "2 of Spades" or "K of Hearts".

7. (6 points) Create a static method that returns a random playing card. This should use the getRandomSuit and getRandomDenomination methods.

5 Background: Blackjack

The game of Blackjack (also known as 21) is played between one player and one dealer. (There may be other players as well, but they are really irrelevant to each other.) Both the player and the dealer are dealt two cards, with one of the dealer’s cards being hidden from the player.

Each card is worth a certain number of points: the numeric cards are worth the number printed on them, and the J, Q, and K cards are worth 10 points. In the real game an A may be worth either 11 or 1 points, whichever is better for the person who has the card. But to simplify things we will assume it is always worth 11 points.

The goal of the player is to have a higher total score than the dealer without having a score higher than 21. The player plays the game by deciding to either take additional cards or keep only the cards that they already have. If they ever get to a score of 22, they automatically lose. Otherwise, once they decide to stop taking additional cards the dealer does the same thing. The dealer does not get to make decisions on their own, and must continue drawing cards until they have at least 17 points.

Then the game ends, with one of the following outcomes:

Name CS117 - Homework 03 – Page 3

• If the player has exactly 21 points, he wins no matter what the dealer does.

• Otherwise, if the player went over 21 points, he loses no matter what the dealer does.

• Otherwise, if the dealer went over 21 points, the player wins.

• Otherwise, if the player has a higher score than the dealer, the player wins.

• Otherwise, if the player and dealer have the same score, the game is a tie.

• Otherwise, the player loses.

(There are some more variations and obscure rules, but these are the only rules we will follow.)

6 Assignment: Blackjack

In this part we will write a program that uses our classes to play the game.

1. (3 points) Create a new class named Main. Clear out the auto-generated code and write your own Javadoc for the class.

2. (25 points) Write a main that allows the user to play the game of Blackjack. It will need to show the user what cards they received and which non-hidden card the dealer received. Then it will need to repeatedly ask the user whether they would like an additional card or not. While they say yes, it should show them the new card they received. When the player declines another card or their score gets to at least 21, it should reveal the dealer’s hidden card, and then give the dealer additional cards until he has at least 17 points. Finally, it should tell the player whether the game was a win, tie, or loss.

7 Finishing

Make sure to run Checkstyle on your program, because any complaints that it has will lead to deductions in your grade.

Also be sure to thoroughly test your program and make sure that it works correctly. I have included several samples of my program running below. Yours does not need to look exactly the same, but it should provide the same kind of information and work the same way.

When you are sure that your program is perfect, create an archive file of your CS117-HW03

folder and upload it to Moodle.

8 Sample Sessions

The next page has the contents of the terminal after I played the game four times.

Name CS117 - Homework 03 – Page 4

Welcome to my Blackjack game!

The dealer is showing a 5 of Hearts

You receive a 5 of Diamonds

You receive a 9 of Diamonds

Would you like another card? (y/n): y

You receive a 3 of Spades

Would you like another card? (y/n): n

The dealer’s hidden card was a 7 of Hearts

The dealer receives a 10 of Clubs

Good news! The dealer went over 21, so you win!

Welcome to my Blackjack game!

The dealer is showing a 2 of Hearts

You receive a 3 of Diamonds

You receive a 3 of Diamonds

Would you like another card? (y/n): y

You receive a K of Clubs

Would you like another card? (y/n): y

You receive a J of Clubs

The dealer’s hidden card was a K of Spades

The dealer receives a 10 of Hearts

Sorry, you lost by going over 21.

Welcome to my Blackjack game!

The dealer is showing a J of Diamonds

You receive a A of Spades

You receive a K of Spades

The dealer’s hidden card was a 8 of Clubs

Congratulations, you won by getting exactly 21!

Welcome to my Blackjack game!

The dealer is showing a A of Diamonds

You receive a 4 of Spades

You receive a 4 of Spades

Would you like another card? (y/n): y

You receive a K of Diamonds

Would you like another card? (y/n): n

The dealer’s hidden card was a 3 of Diamonds

The dealer receives a 5 of Clubs

Sorry, the dealer’s score is higher, so you lose.

__MACOSX/CS117-HW03/._2018spring-cs117-hw03-assignment.pdf

CS117-HW03/Suit.ctxt

#BlueJ class context comment0.params=theName comment0.target=Suit(java.lang.String) comment0.text=\n\ Constructs\ a\ new\ Suit.\n\ \n\ @param\ theName\ The\ name\ of\ the\ new\ Suit.\n comment1.params= comment1.target=java.lang.String\ getName() comment1.text=\n\ Returns\ the\ name\ of\ this\ Suit.\n\ \n\ @return\ The\ name\ of\ this\ Suit.\n comment2.params= comment2.target=Suit\ getRandomSuit() comment2.text=\n\ Gets\ a\ randomly-chosen\ Suit.\n\ \n\ @return\ A\ randomly-chosen\ Suit.\n numComments=3

__MACOSX/CS117-HW03/._Suit.ctxt

CS117-HW03/Suit.class

                public 
                final 
                synchronized 
                enum Suit {
    
                public 
                static 
                final Suit 
                SPADES;
    
                public 
                static 
                final Suit 
                HEARTS;
    
                public 
                static 
                final Suit 
                CLUBS;
    
                public 
                static 
                final Suit 
                DIAMONDS;
    
                private String 
                name;
    
                public 
                static Suit[] 
                values();
    
                public 
                static Suit 
                valueOf(String);
    
                private void Suit(String, int, String);
    
                public String 
                getName();
    
                public 
                static Suit 
                getRandomSuit();
    
                static void 
                <clinit>();
}

            

__MACOSX/CS117-HW03/._Suit.class

CS117-HW03/package.bluej

#BlueJ package file objectbench.height=76 objectbench.width=686 package.editor.height=400 package.editor.width=560 package.editor.x=106 package.editor.y=106 package.numDependencies=0 package.numTargets=1 package.showExtends=true package.showUses=true project.charset=UTF-8 target1.editor.height=738 target1.editor.width=1046 target1.editor.x=65 target1.editor.y=62 target1.height=50 target1.name=Suit target1.naviview.expanded=true target1.showInterface=false target1.type=EnumTarget target1.typeParameters= target1.width=80 target1.x=90 target1.y=20

__MACOSX/CS117-HW03/._package.bluej

__MACOSX/._CS117-HW03