Skip to content

Exercises 8 ​

In these exercises, you’ll revisit Blackjack and improve its implementation using structures and enumerations, similar to what you did for Tic-Tac-Toe in Refactoring and Enumerations.

Before you begin, familiarize yourself with the solution project for Blackjack. You’ll use this project as your starting point.

Exercise 8.1 ​

Declare the enumerations Rank and Suit introduced in the previous chapter. Give both a raw type of String and assign each case its corresponding String value.

Exercise 8.2 ​

Add a computed property value to Rank. This property replaces the values dictionary that the solution currently uses.

Exercise 8.3 ​

Declare an enumeration Action to represent the actions a player can take. These are:

  • hit
  • stand
  • double down
  • split
  • answer yes
  • answer no

Exercise 8.4 ​

Add a description property to Action that describes the input expected for each action. For example, you can return "(h)it" to indicate that the player should type either "h" or "hit" to select action hit.

Exercise 8.5 ​

Add an initializer to Action that converts the player’s input to one of the actions. Return nil if the input doesn’t match any of the actions.

Exercise 8.6 ​

Identify and declare any other types you need, then use these types, together with the enumerations you declared in the previous exercises, to give the implementation a complete overhaul.

Also, audit your types for encapsulation. Think about which members a type should expose and mark all others as private.

When you’re done, compare your work with the solutions bundle.