< previous page page_1083 next page >

Page 1083
Problem-Solving Case Study Simulated Playing Cards
1083-01.gif
Problem: As an avid card player, you plan to write a program to play solitaire once you have become thoroughly comfortable with dynamic data structures. As a prelude to that program, you decide to design a C++ class that models a pile of playing cards. The pile could be a discard pile, a pile of cards face up on the table, or even a full deck of unshuffled cards. The card pile will be structured as a dynamic linked list.
In this case study, we omit the Input and Output sections because we are developing only a C++ class, not a complete program. Instead, we include two sections entitled Specification of the Class and Implementation of the Class.
Discussion: Thinking of a card pile as an ADT, what kinds of operations would we like to perform on this ADT? You might have come up with a different list, but here are some operations we have chosen:
Create an empty pile
Put a new card onto the pile
Take a card from the pile
Determine the current length of the pile
Inspect the nth card in the pile
We can base our design roughly on the OrdList class of this chapter, but there are some differences. First, we should consider a card pile to be an unordered list, not an ordered list, because the cards can be in random order in the pile. Second, the last two operations listed above were not present in OrdList. These operations allow more flexibility in asking questions about the list. If we use a private variable to keep track of the current length of the list, we can ask at any time how many cards there are in a pile. We also can simulate looking at a face-up pile of cards by using the last operation to inspect any card in the pile without removing it.
Before we write the class specification, we must decide how to represent an individual playing card. The suit of a card can be represented using an enumeration type. Rank can be represented using the numbers 1 through 13, with the ace as a 1 and the king as a 13. Each card is then represented as a struct with two members, suit and rank:

 
< previous page page_1083 next page >