This page is part of the Blackjack Lab.

Blackjack handles the game mechanics. It does not interact with the user. The UI is completely separate. Both a text based UI and a graphical UI can use the same mechanics class. See Object oriented UI.

Start files:

Blackjack part 1 involves writing the methods in Blackjack.java labeled // TODO implement (part 1).

Solutions are at the bottom of this page.

Provided code

Shoe class

The Shoe is responsible for maintaining a collection of shuffled cards, usually multiple decks. It is also responsible for dealing cards.

The Shoe class is already complete. As an extension, Collections.shuffle can be replaced with a custom shuffle method.

TestShoe class

TestShoe allows Blackjack to be tested with predetermined cards. BlackjackInitialTester assumes that Blackjack uses TestShoe. To test Blackjack with the automated testers, replace the Shoe instance variable with a TestShoe (included as a comment). Update the Blackjack constructor to construct a TestShoe object.

Constants

Most of the Blackjack constants are used only in part 2.

DECKS is used in the Blackjack constructor when constructing the Shoe (and also in part 2).

Instance variables

shoe holds a Shoe, which has the responsibilities discussed above.

playersMoney stores the player’s money. This version of Blackjack allows the player’s money to become negative and allows the player to bet amount in excess of playersMoney. As an extension, bets can be limited based on both the player’s money and table limits.

playersHand stores the player’s hand, or null if the player has not yet bet (and been dealt a hand). Various methods of Blackjack check playersHand against null to determine whether the player has already bet.

playersBet stores the player’s bet for the current round. This version of Blackjack does not attempt to ensure a valid bet. As an extension, bets can be limited in various ways.

dealersHand stores the dealer’s hand, or null if the player has not yet bet.

Exceptions

Several methods of Blackjack throw an IllegalStateException if run out of sequence. For example, getPlayersHand throws this exception if the player has not yet bet (playersHand == null).

The code to check for and throw these exceptions has been provided.

These exceptions make client code (code that uses the Blackjack class) easier to debug if it calls Blackjack methods out of sequence.

Code to be written

Blackjack constructor

The Blackjack constructor should initialize each instance variable.

The instance variables shoe and playersMoney should be initialized to meaningful values.

The instance variables playersHand, playersBet, and dealersHand should be initialized to placeholder values. They will be set to meaningful values in placeBetAndDealCards.

getPlayersMoney and getPlayersBet methods

These are simple accessor methods. The code to throw an IllegalStateException if getPlayersBet is called out of sequence is provided.

placeBetAndDealCards method

The amount of the player’s bet is removed from the player’s money.

Cards are dealt from the shoe in the order: player, dealer, player, dealer. The test code depends on this order, so it is not optional.

getPlayersHand and getDealersHand methods

These are simple accessor methods. The code to throw an IllegalStateException if either method is called out of sequence is provided.

Although it would be safer, these methods do not need to return copies of the hands. Returning references to the actual hands is acceptable.

Hints

Blackjack constructor

shoe should be set to refer to a new Shoe object. The Shoe constructor accepts the number of decks as a parameter.

The instance variable playersMoney and the parameter playersMoney have the same name. See this keyword for how to distinguish between instance variables and parameter or local variables with overlapping scope.

null is a reasonable placeholder value for reference variables such as playersHand and dealersHand. Other methods explicitly check whether playersHand is null to determine that the player has not yet bet.

0 or -1 are reasonable placeholder values for playersBet.

placeBetAndDealCards method

The BlackjackHand constructor accepts 2 BlackjackCard parameters. The Shoe method dealCard returns a BlackjackCard.

Dealing the cards in the correct order (player, dealer, player, dealer) requires storing at least 2 of the cards in variables. Alternatively, all 4 cards can be stored.

All 4 accessor methods

Return the corresponding instance variable

Solution code

Blackjack.java

The Java file above uses the Shoe class. The automated tester requires the TestShoe class.

Blackjack constructor

public Blackjack(double playersMoney)
{
    this.shoe = new Shoe(DECKS);
    this.playersMoney = playersMoney;
    playersHand = null;
    playersBet = 0;
    dealersHand = null;
}

placeBetAndDealCards method

public void placeBetAndDealCards(double amount)
{
    playersMoney -= amount;
    playersBet = amount;

    BlackjackCard first = shoe.dealCard();
    BlackjackCard second = shoe.dealCard();

    playersHand = new BlackjackHand(first, shoe.dealCard());
    dealersHand = new BlackjackHand(second, shoe.dealCard());
  
    // alternate
    //    BlackjackCard[] cards = new BlackjackCard[4];
    //    for(int i = 0; i < cards.length; i++)
    //        cards[i] = shoe.dealCard();
    //    
    //    playersHand = new BlackjackHand(cards[0], cards[2]);
    //    dealersHand = new BlackjackHand(cards[1], cards[3]);
}

All 4 accessor methods

public double getPlayersMoney()
{
    return playersMoney;
}

public double getPlayersBet()
{
    if(playersHand == null)
        throw new IllegalStateException("Player has not yet bet.");
    
    return playersBet;
}

    public BlackjackHand getPlayersHand()
{
    if(playersHand == null)
        throw new IllegalStateException("Player has not yet bet.");
    
    return playersHand;
}

public BlackjackHand getDealersHand()
{
    if(playersHand == null)
        throw new IllegalStateException("Player has not yet bet.");
    
    return dealersHand;
}

Comments

Comment on Blackjack Lab