What's the best way to ask if any of a kind has a certain value for a property?

+1 vote
506 views
asked Apr 19 in Authoring by anonymous

I find myself writing a lot of code like:

A person has a number called house number.

To knock on a door:
  ... 
  if N is the house number of any person:
        ... 

but this doesn't work. So what's the easiest way in Inform 7 to do this?

2 Answers

0 votes
answered Apr 21 by bg (692 points)
edited Apr 21 by bg

I was hoping someone else would answer this question, because I don't know the best way to do it. But since no one has, here's one way that works.

Every person has a number called house number.

The house number of the player is 5.

Place is a room.

When play begins:
    if 5 is a valid house number:
        say "Yes."

To decide if (N - a number) is a valid house number:
    Repeat with P running through people:
        Let HN be the house number of P;
        if N is HN:
            decide yes;
    decide no.

"Repeat running through" is in §11.11 in Writing with Inform, and there are some examples of "to decide if" phrases in §11.16.

Here's another way you could do it:

Every person has a number called house number.

The house number of the player is 5.

Place is a room.

HouseNumberList is a list of numbers that varies.

When play begins:
    Repeat with P running through people:
        Let HN be the house number of P;
        add HN to HouseNumberList.

Every turn:
    if 5 is listed in HouseNumberList:
        say "Yes."

You can read more about testing and iterating over lists in §21.4.

0 votes
answered Apr 28 by ChrisC (1 point)

The most "inform-like", extensible way might be to use relations. This unfortunately seems to require a "running through" to identify the address of a person due to the way unenumerated kinds of value (like texts and numbers) work, but this then allows some very compact code to be written:

There is room.

Occupancy relates various people to one number. The verb to live at house means the occupancy relation. The verb to live at number means the occupancy relation. The verb to live at means the occupancy relation. The verb to house implies the reversed occupancy relation.

Dan is a man, here, living at number 555. Jane is a woman living at number 111. Elle and Rose are women living at house 54.

Knocking on is an action applying to one value. Understand "Knock on [a number]" as knocking on.

Definition: a number is occupied if anyone lives at it.

To decide what number is the address of (P - a person):
    repeat with N running from 1 to 999:
        if N houses P, decide on N;
    decide on 0.

Definition: a person (called observed) is homeless rather than settled if the address of the observed is 0.

Check knocking on:
    unless the number understood is occupied:
        say "No one lives at number [number understood]." instead.

Carry out knocking on:
    say "You knock on [a list of everyone living at house number understood]'s door."

After examining a settled person:
    say "[The noun] lives at number [address of the noun]."
This site is now closed.
As of 1st November 2015, this site is a read-only archive. For more information see the intfiction forum post

Welcome to IF Answers, a site for questions and answers about Interactive Fiction.

Technical questions about interactive fiction development tools such as Inform, Twine, Quest, QuestKit, Squiffy, Adrift, TADS etc. are all on-topic here.

Non-technical questions about interactive fiction are also on-topic. These questions could be about general IF design, specific games, entering the IF Comp etc.
...