Exercises 9
With these exercises, you’ll deepen your understanding of value types and reference types by drawing memory diagrams like the ones you’ve seen in the previous chapter.
You’ll compare the solution projects for Exercises 8 and Classes to see what changed when you used classes for some of the types.
Exercise 9.1
Using the solution project for Exercises 8, draw a memory diagram that shows the contents of the variable game
from main.swift. Assume the game has just started, the player has drawn K♥️ 7♠️, the dealer has 10♣ 3♥️, and the player hasn’t taken any actions.
Recall that this implementation uses only value types. Therefore, everything on your diagram should be contained within game
as one large value. Make your diagram as detailed as possible by showing the contents of every nested value. The only exception is deck.cards
. For this array, drawing one or two cards suffices. You can add an ellipsis (...
) to indicate that you’ve omitted the remaining elements from the array.
Exercise 9.2
Draw an equivalent diagram using the solution project for Classes .
Types Deck
, Hand
, Player
, and Game
are now classes. Therefore, your diagram will have less nested values, and many variables will contain references to instances that are stored elsewhere in memory.