Skip to content

Exercises 8 ​

In this exercise series, you’ll revisit Blackjack and declare three enumerated types you can use to improve its implementation.

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 ​

Improve the solution project for Blackjack using structures and enumerations, similar to what you did for Tic-Tac-Toe in Programming with Structures and Enumerations.

Use the enumerations you declared in the previous exercises, identify and declare any other types you need, and 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.