| 11.10. Repeat running through |
Inform is not used very much for numerical work, so the kind of repeat loop described in the previous section is not much used. Inform's natural domain is really the world of things and rooms, so the following kind of repeat is much more useful.
repeat with item running through open containers:
...
Once again we create a new variable, "item", of only temporary validity. This time the phrases are repeated once for each open container presently found in the world. (If there are no containers, or they are all closed, the phrases will not be followed at all.)
As with counting the "number of ..." objects satisfying some property, we can run through a wide variety of possibilities - any description whose range is possible for Inform to search. For example:
repeat with dinner guest running through the people in the Dining Room:
...
repeat with possession running through things carried:
...
repeat with event running through non-recurring scenes which are going on:
...
The following lists the whereabouts of all men in lighted rooms:
repeat with suspect running through the men who are in a lighted room (called the scene):
say "[The suspect] is in [the scene].";
Note the way we are allowed to give a name to the vaguely described place "lighted room", so that we can refer to it inside the loop. This wasn't really necessary ("holder of the suspect" would have had the same effect) but improved the clarity of the source.
Once again, we are not allowed this:
repeat with N running through even numbers:
...
because the search would be infinite. See the Kinds index for which kinds of value can be repeated through.
Many simple repetitions can effectively be done with a "now..." instruction: it is quicker to say
now every person is angry
than
repeat with offended party running through people:
change the offended party to angry.
Repeat comes in handy when we have something a bit more complicated to do with each item:
"Strictly Ballroom"
A person can be alert or occupied. A person is usually alert.
Dance is a kind of value. The dances are waltzes, polkas, cha-chas, charlestons, fox-trots, tangos, lambadas, and two-steps.
The current round is a dance that varies.
Manner is a kind of value. The manners are swiftly, primly, energetically, suavely, seductively, elegantly, and badly.
Every turn: change the current round to a random dance.
Every turn:
repeat with dancer running through people who are not the player:
if dancer is alert:
change dancer to occupied;
let partner be a random alert person who is not the dancer;
if partner is a person:
change partner to occupied;
say "[The dancer] [the current round][if a random chance of 1 in 5 succeeds] [a random manner][end if] with [partner]. ";
otherwise:
say "[paragraph break][The dancer] is forced to be a wallflower. Poor [dancer]. ";
say paragraph break.
Notice we did not say "repeat with dancer running through alert people who are not the player...". This is because Inform would draw up a list of alert people at the beginning of the repeat, and not take into account which people became occupied partway through the repetition. If we want to make sure that each person dances only with one other person, we have to continue checking alertness each time we run through the repetition.
After all the partners are assigned, we can set up for the next turn by making everyone alert again, and for this we do not need "repeat":
Every turn: now every person is alert; now the player is occupied.
Before doing something to someone: now the noun is occupied.
Before doing something when the second noun is a person: now the second noun is occupied.
Instead of doing something to someone: say "You successfully distract [the noun]."
The Pacific Ballroom is a room. "A rather utilitarian space at the moment, since this is a class and not a party." Timmy, Tommy, Joey, George, Mary, Martha, Yvette, McQueen, Linus, and Patricia are people in the Pacific Ballroom.
Test me with "z / ask linus about blanket / z / z".
|