Skip to end of metadata
Go to start of metadata


This week's project was an extension to the last week's project.  New element of the project was Java Swing, which was used to build a GUI (Graphic User Interface?).  Before creating GUI for Blackjack game, I needed to update my Card, Deck, Hand, and Blackjack classes.  Lastly, I wrote BlackjackApp which brings all those classes together through Blackjack class, builds a main frame, and adds various elements such as labels, buttons, and cards to the main frame.  Build a GUI was a similar experience with building Blackjack game in a sense that I needed to put different objects or classes together.

Main Code:

The Card Class)

The major modification for the Card class was to enable it to read in the information from a card, such as its value and suit.

Below are the fields of my Card class.  With the additional fields, I was able to use more information about card.

After I made all the necessary changes in the Card class, I ran and below is the screen shot of it.

The Deck Class)

The major modification for the deck class was to change the build method so that a deck is built of cards with numerical value and suit.

I also made a change in deck's deal method so that it doesn't remove a card the deck size is zero.

Below is the screen shot of

The Hand Class)

The major modification for the Hand class was the getTotalValue so that a card with a value over 10 would be counted as 10.

Below is the screenshot of

The Blackjack Class)

In the Blackjack class, I needed to create new methods that were to be used in the BlackjackApp class to run a game.

For Blackjack's draw method, I simply called deck, player, and dealer's draw method.  Essentially, I used Card and Deck classes' draw methods.

The BlackjackApp Class)

Below is the image of my design before building my GUI.  My initial plan was to draw deck in the center, player in the left, and dealer in the right.  Then, I wanted to add player's score right underneath player's hand, add dealer's score right underneath dealer's hand, and the result of the game below the deck.

However, I realized the layout of JPanel was not as flexible as I thought.  Thus, the final design of my BlackjackApp looks a lot different from my initial design.

Below is the image of my

(just for a reference, player's score says 15 because the Ace is counted as 1 here.)

As the image shows, my GUI ended up having 9 labels and 4 buttons.

The labels and buttons represent the followings, respectively: 1) player's score, 2) number of games player has won, 3) the result of the game, 4) dealer's score, 5) number of games dealer has won, 6) number of cards left in the deck, 7) amount of money player has won from betting, 8) amount of money dealer has won from betting, 9) decision rule for dealerTurn() method, 10) rule button that enables user to type in an input, 11) deal button to deal a card, 12) hit button for player, and 13) hold button for player.

Below is a gif of playing my BlackjackApp.



  1. Add in more of the rules for Blackjack. For example, an Ace (value 1 in the default case above) can have the value 1 or 11, whichever is more advantageous. If you add in the Ace rule, then you will also want to take into account a real Blackjack (2 cards totaling 21) when evaluating the winning hand. A Blackjack beats a 21 with more than 2 cards.

    In order to add in the Ace rule, I changed Blackjack class's hitPlayer() method so that if the card's value is equal to 1, the user will be asked for input either 1 or 11.  Once user chooses 1 or 11, the program creates a new card with the chosen value and saved suit.
    To enable the user to type an input, I imported java's JOptionPane package as well.
    To add in the real Blackjack rule, I needed to make changes in BlackjackApp class's HoldActionListener.  

  2. Make your game use 6 decks, and reshuffle if only 1 deck is left. Do this by modifying the deck class so its constructor knows how many decks to build.

    : I approached this extension by building a deck of 312 cards, which is equivalent to 6 decks.  To do so, I needed to make changes in Deck, Blackjack, and BlackjackApp class.
    First, I added a parameter to deck class constructor and build() method so that deck constructor knows which parameter to add in for build() method.  If deck constructor has 1 as parameter, it would call build(1), which will build a deck of 52 cards.  If deck constructor has 2, it will call build(2), which would build a deck of 312 cards.
    Next change I made was in Blackjack class.  Similar to the changes in Deck class, I (1) added a parameter to Blackjack constructor, (3) checkDeck() method, and (2) added a conditional statement to reset() method.
    (1)    (2)      (3)  .
    Lastly, I modified the initialization for Blackjack object and added a parameter to the checkDeck method in BlackjackApp class.
    In order to play a blackjack game with 6 decks, please go to line 49 of and change the parameter inside new Blackjack(1) to 2.
    Then, go to line 306 of and change the parameter of checkDeck(1) to 2 as well.
    Once those changes are made, an image of BlackjackApp will look like below.
    Note that the label Cards Left now says 312 instead of 52.  Because I set the size of DrawPanel as (800, 400) default, the deck is slightly out of the panel.  Everything else regarding game features should work fine.  The deck is shuffled when the size of the deck gets below 52 cards.

  3. Run the simulation with different decision rules for the dealer. Can you make this an option controllable by a menu?

    In order to make decision rule an option controllable by a menu, I first created an additional instance variable, int rule in Blackjack class.  Then, I made changes in dealerTurn() method.
    In addition, I made more methods such as changeRule() and getRule() in Blackjack class so that ruleButton can call those methods when needed.
    To enable user's input, I imported Java's JOptionPane's package.
    In, I made a private class that inherits ActionListener and attached it to the ruleButton.  Below is what it does.
    I can track the changes in decision rule through a label called Decision rule.  Another way to keep track of decision rule is in the terminal.  Whenever dealerTurn() method is used, value of this.rule will be printed.  Thus, I can check if ruleButton actually changes the decision rule.

  4. Keep track of how many games the player/dealer have won so far.

    In order to keep track of how games the player/dealer have won, I created two additional JLabel fields and two additional int variables.
    recordP and recordD is incremented by 1 whenever player and dealer wins.  I used setText() method to update the pRecord and dRecord labels when the player/dealer wins.
    To see how the changes are made, take a look at my codes below HitActionListener or HoldActionListener.  
    Below is an example.

  5. Add betting and keep track of money for the player.

    This extension was really similar to the extension above.  I created two additional JLabels and two additional int variables to update them throughout the game.

    When player decided to hit and then bust, dealer wins $50.  If player decides to hold, he bets the double amount $100.  Thus, player earns $100 if wins.  If lost, dealer gets $100 instead.  Below is the code of how I implemented this.




After this project, I can now say I feel comfortable (not confident though) coding in java.  I also learned how to build GUI using java swings, which I found very useful.

For this project, I received help from professor Maxwell and Layton.