18. The “BlackJack” class

Okay, we are getting there, but the final few steps are a little fiddly. We’ve essentially created the main objects required in the game but what we now need is a class that will provide the structure for the game itself, we’re going to call this one “BlackJack” and it needs to look exactly like this:

package source;

import java.io.IOException;

public class BlackJack {

   // MEMBER VARIABLES
   // ================
   private BlackJackGame game;

   // CONSTRUCTOR
   // ===========
   public BlackJack() throws IOException {
      game = new BlackJackGame();
      game.playGame();
   }

   // METHODS
   // =======
   public static void main(String[] args) throws IOException {
      new BlackJack();
   }
}

This one is a little different to our other classes, because now we’re starting to allow some interactivity from the user. We want them to be able to tell us whether they’re going to hit or stay for instance and this is why we have that fancy looking “main” method. Now that the user is able to provide input we have to deal with what’s called an “IOException” which basically just means, “did they type something wrong“. In the end, don’t let the new things bog you down, they’ll become clearer as we finalise this class.

You’re also going to notice that Eclipse doesn’t like a few things about our code. We haven’t actually got any problems with it except that we don’t have a class called “BlackJackGame” and perhaps the need to import some libraries (which you should be able to do now). This leads us to inner classes.

Inner Classes
An inner class is basically a class that exists inside another one (pretty obvious I guess). There’s lots of reasons you may want to do this but in our case it’s simply because it allows us to separate the game logic from the static main method. So underneath our last method in “BlackJack” we’re going to add our new class (make sure it goes before that last parenthesis though!)

//####################################################
// Inner Class: "BlackJackGame"
// - - - - - - - - - - - - - - - - - - - - - - - - - -
public class BlackJackGame {

   private final Deck deck;
   private final Hand player;
   private final Hand dealer;
   private BufferedReader keyboard;
   private char input;

   // CONSTRUCTOR
   // ===========
   public BlackJackGame() {
      // code goes here...
   }

   // METHODS
   // =======
   public void playGame() throws IOException {
      // code goes here...
   }

   public void initialDeal() {
      // code goes here...
   }

   public void dealersTurn() {
      // code goes here...
   }

   public void showResults() {
      // code goes here...
   }
}

I’m going to create a final UML diagram once this is all complete so don’t fret if we’re moving quickly through all this but you may find it beneficial to walk through the fields and methods listed in this skeleton on your own before we move to their implementation. You’re hopefully pretty familiar with the game of Blackjack now and these class properties shouldn’t be too hard to understand.

This is the final class we require for our Blackjack game and while it may look quite large, our previous class creation is going to put us in good stead to create a clear and concise set of methods now. So let’s crack them out…

Leave a Comment

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>