Chapter 11: Phrases
11.10. Repeat

The other kind of loop in Inform is "repeat". The trouble with "while" is that it's not obvious at a glance when or whether the loop will finish, and nor is there any book-keeping to measure progress. A "repeat" loop is much more predictable, and is more or less certain to finish.

There are several forms of "repeat", of which the simplest is similar to the old FOR/NEXT loop from the home-computer programming language BASIC, for those with long memories:

repeat with (a name not so far used) running from (arithmetic value) to (arithmetic value)
or: repeat with (a name not so far used) running from (enumerated value) to (enumerated value):

This phrase causes the block of phrases following it to be repeated once for each value in the given range, storing that value in the named variable. (The variable exists only temporarily, within the repetition.) Example:

repeat with counter running from 1 to 10:
    ...

This, and runs through the given phrases ten times. Within those phrases, a special value called "counter" has the value 1 the first time through, then the value 2, then 3 and so on up to 10. (It can of course be called whatever we like: this is only an example.) The range can be from any kind where ranges make sense - anything on which arithmetic can be done, so for instance

repeat with moment running from 4 PM to 4:07 PM:
    ...

and also any enumeration:

Colour is a kind of value. The colours are red, orange, yellow, green, blue, indigo and violet.

...
    repeat with hue running from orange to indigo:
        ...

We are allowed to "nest" loops, that is, to put one inside another.

To plot a grid with size (S - a number):
    repeat with x running from 1 to S:
        say "Row [x]:";
        repeat with y running from 1 to S:
            say " [y]";
        say "."

If we then write

plot a grid with size 5;

then the result is

Row 1: 1 2 3 4 5.
Row 2: 1 2 3 4 5.
Row 3: 1 2 3 4 5.
Row 4: 1 2 3 4 5.
Row 5: 1 2 3 4 5.

Thus the innermost phrase, the say which mentions "y", happens 25 times.

Whenever dealing with numbers in Inform we may need to remember that if the Settings for the project are set to use the Z-machine, the range is restricted to -32768 up to 32767. Repeating with a counter up to exactly 32767 is hazardous, because the counter can never break through this barrier: it's infinity, so far as Inform is concerned, and that can cause the repetitions to go on forever. (On Glulx, numbers can be very much larger.)


175
* Example  Wonka's Revenge
A lottery drum which redistributes the tickets inside whenever the player spins it.

RB

"Wonka's Revenge"

The Caribou Lodge is a room. "Hundreds of expectant faces are turned your way from every table." A lottery drum is in the Lodge. "Before you is the lottery drum[if we have spun the drum], ready to disgorge a ticket[otherwise], waiting to be spun[end if]." In the drum are a red ticket, an orange ticket, a yellow ticket, a green ticket, a blue ticket, a purple ticket, and a ticket of pure gold. The drum is closed and openable.

Understand "spin [something]" as spinning.

Spinning is an action with past participle spun, applying to one thing.

Check spinning: if the noun is an open container which contains something, say "[The list of things in the noun] would fly out." instead.

Carry out spinning a container:
    shuffle the contents of the noun.

Report spinning:
    if the noun contains something, say "You rattle [if the noun is transparent][the list of things in the noun][otherwise]the stuff[end if] in [the noun].";
    otherwise say "Nothing results of your shaking [the noun]."

Inform keeps track of the order in which things have been put into a container. If we want to change that order without the player's intervention, we can move the things ourselves.

To shuffle the contents of (basket - a container):
    let moves be the number of things in the basket;
    repeat with counter running from 1 to moves:
        move a random thing in the basket to the basket.

After opening the drum when we have spun the drum for the first time:
    if something (called the pick) is in the drum:
        try searching the drum;
        say "[The pick] it is, then.";
        silently try taking the pick;
        if the pick is the ticket of pure gold, end the story finally;
        otherwise end the story saying "Oh well, better luck next time."

Test me with "open drum / look in drum / close drum / spin drum / open drum".


PreviousContentsNext