Tilt 1
We've simulated a deck of cards before, but only as entries in a table. This time we're going to do it more completely, with card objects that can be drawn and discarded, and referred to by name. The tedious way to do this would be to make 52 objects by hand and laboriously write out their names and understand rules.
A more sensible way is to make 52 identical card objects, assign them ranks and suits, and allow Inform to generate and parse their names automatically.
So:
Now, we're going to describe the higher numbers as face cards, so it helps to write a new "to say" phrase, just as we did in Jokers Wild. (A subsequent version of this example shows how to print card values with red and black symbols representing the different suits; see "Tilt 3".)
term | value | topic |
"ace" | "1" | "ace/A/one" |
"deuce" | "2" | "deuce/two" |
"three" | "3" | "three" |
"four" | "4" | "four" |
"five" | "5" | "five" |
"six" | "6" | "six" |
"seven" | "7" | "seven" |
"eight" | "8" | "eight" |
"nine" | "9" | "nine" |
"ten" | "10" | "ten" |
"jack" | "11" | "jack/knave/J" |
"queen" | "12" | "queen/Q" |
"king" | "13" | "king/K" |
This is enough already to let inform understand things like "ten clubs", but we want to add a couple of refinements. For one thing, we'd like to accept "of" when it appears in phrases such as "ten of clubs" (but not generically otherwise); for another, we'd like the player to be able to use various names for ranks. To this end, we need to borrow from the Activities chapter and modify the player's command before attempting to understand it:
It may be a bit confusing that the Table of Value Names has both a topic column and a term column, to all appearances essentially identical. But items in the topic column can be matched against the player's input, whereas items in other kinds of text column can be printed out; the two kinds of text are not treated identically by Inform, so we need to have both. Notice that the topic column contains entries like "jack/knave," which will match either "jack" or "knave" in the player's input.
Now to set up the deck at the outset. With some intelligent looping, we avoid having to declare every combination of suit and number individually:
And now we need a simple setting and some actions to manipulate the deck with:
The above phrase rounds a number to the nearest five, because it seems unrealistic for the player to be able to count a large number of cards in the discard pile at a single glance.
This next bit is an optional borrowing from the Activities chapter: we want to prevent Inform printing things like "You can see a discard pile (closed) here.", since we don't want the player to think of the piles as containers, even though Inform thinks of them in those terms.
Finally, we want the player to use "draw" and "discard" to manipulate his hand of cards:
Test me with "seed / draw / g / g / g / g / i / discard seven of spades / draw / discard six / draw / i / discard hearts / discard six of diamonds card / draw / draw / i / discard spades card / draw / discard king card".
Nothing to see here.
You can see a deck of cards here.
>(Testing.)
>[1] seed
>[2] draw
You draw a three of hearts.
>[3] g
You draw a ten of spades.
>[4] g
You draw a four of spades.
>[5] g
You draw a ten of diamonds.
>[6] g
You draw an eight of clubs.
>[7] i
You are carrying:
an eight of clubs
a ten of diamonds
a four of spades
a ten of spades
a three of hearts
>[8] discard seven of spades
You can't use multiple objects with that verb.
>[9] draw
This is a five-card game; you must discard something before drawing anything further.
>[10] discard six
You can't see any such thing.
>[11] draw
This is a five-card game; you must discard something before drawing anything further.
>[12] i
You are carrying:
an eight of clubs
a ten of diamonds
a four of spades
a ten of spades
a three of hearts
>[13] discard hearts
You toss the three of hearts nonchalantly onto the discard pile.
>[14] discard six of diamonds card
You can't use multiple objects with that verb.
>[15] draw
You draw a seven of diamonds.
>[16] draw
This is a five-card game; you must discard something before drawing anything further.
>[17] i
You are carrying:
a seven of diamonds
an eight of clubs
a ten of diamonds
a four of spades
a ten of spades
>[18] discard spades card
Which do you mean, the four of spades or the ten of spades?
>[19] draw
This is a five-card game; you must discard something before drawing anything further.
>[20] discard king card
You can't use multiple objects with that verb.