Previous Exercise |
Next Exercise |
Lecture |
Top Level
Cards
Context
Using the same technique as in the last exercise, we will add another
monopoly object to our game: the event cards.
Let's analyse them. First of all, there doesn't seem to be a
fundamental difference between the chance cards and the community
chest cards. We will therefore represent both types by the same
class.
We will again put all the cards into one big CardIndex and
use a CardPtr to refer to the data in the index. Similar to
the DeedPtrList, we will use a CardPtrDeque which
will behave like a stack of cards: we can draw the topmost
card and put back a card underneath the bottom of the
stack.
The card event will be implemented in the execute() member
function of every card object. In this change, we will just have a
function stub for this member function, as we do not yet have enough
objects to effectively implement the execute() member
function.
What we can do is further classify the types of cards we
have. I did this classification for you:
- JailCard
- Get out of jail for free.
- one community chest, one chance
- GotoJailCard
- Go to jail, do not pass go.
- one community chest, one chance
- RepairCard
- Pay x per house and y per hotel.
- community chest "You are assessed for street repairs. $40 per house and $115 per
hotel".
- PayCard
- Pay x to bank (negative values allowed).
- community chest "Pay hospital $100".
- community chest "Pay school tax of $150".
- community chest "Doctor's Fee, pay $50".
- chance "Pay poor tax $15".
- community chest "From Sale of stock, you get $45".
- chance "Your building and loan matures, collect $150".
- community chest "Receive for services $25".
- community chest "You inherit $100".
- community chest "Xmas fund matures, collect $100".
- community chest "Life insurance matures, collect $100".
- community chest "You have won second prize in a beauty contest, collect $10".
- community chest "Bank error in your favor, collect $200".
- community chest "Income tax refund, collect$20".
- PayAllCard
- Pay x to all players (negative values allowed)
- chance "You have been elected chairman of the board. Pay each player $50".
- community chest "Grand opera opening. Collect $50 from every player".
- AdvanceCard
- Go to nearest type of space.
- chance, twice "Advance token to the nearest station and pay owner twice the
normal rent. If unowned, you may buy it from the bank".
- chance "Advance token to the nearest utility, roll dice and pay the
owner 10 times the amount thrown. If unowned, you may buy it from the
bank".
- AdvanceToCard
- Go to space.
- chance "Advance token to boardwalk".
- chance "Advance to St. Charles Place".
- one chance, one community chest "Advance to go".
- chance "Take a ride on the Reading".
- chance "Advance to Illinois Ave.".
- AdvanceByCard
- Advance by x steps (negative values allowed).
- chance "Go back 3 spaces".
Observe the entity relationship diagram:
Exercises
- Change 17
- Unlike change 12, we can immediatly
implement the Card class and it's subclasses. Since the
subclasses are going to be so small and similar (use cut&paste!),
we will all put them into one pair of header/implementation files with
the basename CardTypes.
The file Card.h is provided, you have to write
CardTypes.h and CardTypes.C. Note that the
Card class contains no data and has only one pure
virtual function. Therefore, it doesn't need an implementation
file Card.C!
Use the Key class to store destinations. We will replace this
with the appropriate class once we defined it. As usual, looking at
the tests can provide helpful hints
- Change 18
- Implement the CardIndex class and add a data member
called cards to the Game class. This change is almost
identical to change 12. No header file will
be provided, you have to write it. As usual, looking at the
tests can provide helpful hints.
- Change 19
- Implement the CardPtr and the CardPtrDeque
classes. Both classes will share the same header file
CardPtr.h. Note how we overload
the assignment operator=() to pop off a card from the deque
or to slip it back underneath. As usual, looking at the
tests can provide helpful hints.
- Change 20
- We are beginning to have a sizeable amount of game data. To
facilitate testing, we will add a file
game.data to our source files. As
we add more and more monopoly objects, this file will eventually
contain the whole game database.
You will note that the file contains two CardPtrDeque
entries, one for the community chest stack, one for the chance
stack. Add data members called chest and chance to
the Game class so that it can read and write the
game.data file.
Warning! There is a catch: CardPtrDeque uses
CardPtr, which in turn uses Game in it's
definition. Therefore, you cannot simply declare an element of type
CardPtrDeque in Game since this would result in a
circular definition. Instead, you can make a forward
declaration of CardPtrDeque and use a pointer
to it in the declaration of Game in
Game.h. You can initialize this
pointer to the real thing inside the constructor in Game.C. Don't
forget to delete your items in the destructor.
Yet another reason to use the strict separation between definition and
implementation...
Christian Goetze