< previous page page_1093 next page >

Page 1093
A linked list is a good choice for representing these three lists (the deck, the cards face up on the table, and the discard pile). The simulation requires a lot of deleting from one list and inserting into another list, and these operations are quite efficient with a linked list.
Using dynamic variables to represent our lists instead of a direct array representation saves memory space. If an array representation were used, three arrays of 52 components each would have to be used. In a dynamic linked representation, we use only 52 components in all, because a card can be in only one list at a time.
In our object-oriented design, we have now identified three objects: a card deck, an on-table pile, and a discard pile. A fourth object we'll use is a player object. This object can be thought of as a managerit is responsible for coordinating the three card pile objects and playing the game according to the rules.
To determine the relationships among these four objects, we observe that the three card pile objects are independent of each other and are not related by inheritance or composition. However, the player object is composed of the other three objectsit has-a deck, an on-table pile, and a discard pile as part or all of its internal data. This relationship is seen more clearly when we design and implement the player object.
The On-Table Pile and Discard Pile Objects We can represent these objects directly using the CardPile class from the previous case study.
The Card Deck Object We could represent this object using the CardPile class, but a full deck of cards is more specialized than an ordinary card pile. For example, the CardPile class constructor creates an empty pile, whereas we would like a new card deck to be created as a list of all 52 cards, arranged in order by suit and rank. Also, we would like to include two more operations that are appropriate for a card deckone to shuffle the deck, the other to recreate the deck at the end of each game by gathering together all 52 cards from the on-table pile and the discard pile.
The easiest way to add these new operations to the CardPile class is to use inheritance. From the CardPile class we can derive a new CardDeck class that inherits the CardPile class members and adds the new functions. Inheritance is appropriate here because a card deck is-a card pile (and more). Here is the class specification:

 
< previous page page_1093 next page >