Tilt 1

Deck of cards that can be referred to by name
Example 318
★★★

A deck of cards with fully implemented individual cards, which can be separately drawn and discarded, and referred to by name.

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:

"Tilt"
Section 1 - Cards
Suit is a kind of value. The suits are hearts, clubs, diamonds, and spades. Understand "heart" as hearts. Understand "club" as clubs. Understand "diamond" as diamonds. Understand "spade" as spades. [Providing the singular forms means that Inform will also understand >EXAMINE SPADE, >DISCARD CLUB, and so on.]
A card is a kind of thing. A card has a suit. A card has a number called rank. Understand the suit property as describing a card. Understand the rank property as describing a card.
52 cards are in the card repository.

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".)

To say (count - a number) as a card value:
   choose row count in the Table of Value Names;
   say "[term entry]".
Rule for printing the name of a card (called target):
   say "[rank of the target as a card value] of [suit of the target]"
Table of Value Names
termvaluetopic
"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:

After reading a command:
   if the player's command includes "of [suit]":
      while the player's command includes "of":
         cut the matched text;
   repeat through the Table of Value Names:
      while the player's command includes topic entry:
         replace the matched text with value entry.
   [This allows Inform to understand "ace", "deuce", "king", etc., as numerical ranks.]

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:

When play begins:
   reconstitute deck.
To reconstitute deck:
   let current suit be hearts;
   now every card is in the card repository;
   while a card is in the card repository:
      repeat with current rank running from 1 to 13:
         let item be a random card in card repository;
         now rank of item is current rank;
         now suit of item is current suit;
         now item is in the deck of cards;
      now current suit is the suit after the current suit.

And now we need a simple setting and some actions to manipulate the deck with:

Section 2 - The Deck and the Discard Pile
The Empty Room is a room. "Nothing to see here."
The deck of cards is in the Empty Room. It is a closed unopenable container. The description is "A standard poker deck."
The discard pile is a closed unopenable container. The description is "Cards in this game are discarded face-down, so the discard pile is not very interesting to see. All you can observe is that it currently contains [if the number of cards which are in the discard pile is less than ten][the number of cards which are in the discard pile in words][otherwise]about [the rounded number of cards which are in the discard pile in words][end if] card[s]."
To decide what number is the rounded number of (described set - a description of objects):
   let N be the number of members of the described set;
   let R be N divided by 5;
   let total be R times 5;
   decide on total.

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.

Rule for printing room description details of something: do nothing instead.

Finally, we want the player to use "draw" and "discard" to manipulate his hand of cards:

Section 3 - Drawing and Discarding Actions
Understand the commands "take" and "carry" and "hold" and "get" and "drop" and "throw" and "discard" as something new.
Understand "take [text]" or "get [text]" or "drop [text]" as a mistake ("Here, you only draw and discard. Nothing else matters at the moment.").
Understand "draw" or "draw card" or "draw a card" as drawing. Drawing is an action applying to nothing. The drawing action has an object called the card drawn.
Setting action variables for drawing:
   now the card drawn is a random card which is in the deck of cards.
Check drawing:
   if the card drawn is nothing, say "The deck is completely depleted." instead.
Check drawing:
   if the number of cards carried by the player is greater than four,
      say "This is a five-card game; you must discard something before drawing anything further." instead.
Carry out drawing:
   move the card drawn to the player.
Report drawing:
   say "You draw [a card drawn]."
Understand "discard [card]" as discarding. Discarding is an action applying to one thing.
Check discarding:
   if the player does not carry the noun, say "You can only discard cards from your own hand." instead.
Carry out discarding:
   now the noun is in the discard pile;
   if the discard pile is not visible, move the discard pile to the location.
Report discarding:
   say "You toss [the noun] nonchalantly onto the discard pile."
Seeding is an action out of world. Understand "seed" as seeding. Carry out seeding: seed the random-number generator with 5681.
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".
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".
Empty Room
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.