1
0
Fork 0
mirror of https://github.com/ganelson/inform.git synced 2024-07-16 22:14:23 +03:00
inform7/resources/Documentation/Examples/Numberless.txt

51 lines
1.9 KiB
Plaintext
Raw Normal View History

Example: * Numberless
Location: Otherwise
RecipeLocation: Varying What Is Written
Index: Otherwise if demonstrated
Description: A simple exercise in printing the names of random numbers, comparing the use of "otherwise if...", a switch statement, or a table-based alternative.
For: Z-Machine
2019-02-05 02:44:07 +02:00
{*}"Numberless"
2019-02-05 02:44:07 +02:00
The Rambling Warren is a room.
When play begins:
let N be a random number between 1 and 5;
if N is 1:
say "N is one.";
otherwise if N is 2:
say "N is two.";
otherwise if N is 3:
say "N is three.";
otherwise:
say "N is more than the number of your toes."
The final "otherwise" here will fire only if none of the earlier conditions applies; we could leave it out and print nothing in the case that N is 4 or 5.
The more compact way to do this is to create a list of values that our number could match; in many programming languages this is called a switch statement. For example:
{**}When play begins:
let Y be a random number between 6 and 10;
if Y is:
-- 6: say "Six is the magic number!";
-- 7: say "The number of the day is seven!";
-- otherwise: say "Today's magic number is boring."
As a final option, we can use a construction we've seen only briefly before now: a table. The use of tables will be explained more fully in their own chapter, but here we see in brief that we can assign a number of values to one column of a table and then use that table to look up output:
2019-02-05 02:44:07 +02:00
{**}When play begins:
let X be a random number between 11 and 14;
if X is a number listed in the Table of Switching, say "[output entry][paragraph break]";
otherwise say "X is greater than the number of your noses!"
2019-02-05 02:44:07 +02:00
Table of Switching
number output
11 "X is eleven!"
12 "X is twelve!"
13 "X is thirteen!"
2019-02-05 02:44:07 +02:00
Test me with "z".
As we shall see, things other than text can be stored in tables, so we could also use a table as a way to look up objects or even rules to carry out.