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.