[I7] Most elegant way to see if two numbers are within a range of each other

Let’s say I have a number people can have which is on a sliding scale from 1 to 10. Let’s call it Alignment. Say I want to check whether someone’s Alignment number is within 4 of another person’s Alignment (so if Alice is 3 and Bob is 7 there’s one outcome, but if Alice is 9 and Bob is 2 there’s another).

I can think of a couple of pretty convoluted ways of doing this, but I wondered if there was a more elegant expression among the various arithmetic operators Inform7 allows. Any ideas?

2 Likes

I don’t know whether this is the most elegant way, but Inform 7 provides the “absolute” function, which you can use to measure the distance by taking the absolute of the difference between the values, like this:

The Lab is a room.

A person has a number called alignment.

Alice is a woman in the lab. The alignment of Alice is 2.
Bertha is a woman in the lab. The alignment of Bertha is 5.
Charlotte is a woman in the lab. The alignment of Charlotte is 9.

Every turn:
	say "Alignment distance between Alice and Bertha: [the absolute value of the alignment of Alice minus the alignment of Bertha].[line break]Alignment distance between Bertha and Charlotte: [the absolute value of the alignment of Bertha minus the alignment of Charlotte].[line break]Alignment distance between Alice and Charlotte: [the absolute value of the alignment of Alice minus the alignment of Charlotte].";
	if the absolute value of the alignment of Alice minus the alignment of Charlotte is greater than 4:
		say "You sense a severe alignment mismatch between Alice and Charlotte.".

This produces:

Alignment distance between Alice and Bertha: 3.0.
Alignment distance between Bertha and Charlotte: 4.0.
Alignment distance between Alice and Charlotte: 7.0.
You sense a severe alignment mismatch between Alice and Charlotte.

3 Likes

This is precisely the kind of function I was after as it strips out the minus symbol. Perfect, thanks!

LOL, I hope that this debate don’t led to textfire 13: Tennis !! :smiley:

(if someone don’t noticed, measuring difference between numbers is also measuring differences between scores (in the sport sense)…

Best regards from Italy,
dott. Piergiorgio.

1 Like

I gave an attempt at coding something for this but @StJohnLimbo beat me to the post! Here is what I came up with anyway, your mileage may vary :slightly_smiling_face:

Bob (the player) and Alice (the npc) meet at a coffee shop. Alice’s alignment is 10 and Bob’s is 1 at the outset of gameplay. As Bob drinks his coffee his alignment increases. If Bob attempts to say hello to Alice she will decline to talk until the alignment difference between them is 4 or less.

[Room and objects]

Coffee Shop is a room.  
The description of Coffee Shop is "This is a small coffee shop with limited seating.".  
A cup of coffee is a kind of thing.  
The Coffee Shop contains a cup of coffee.
Understand "coffee", "cup of coffee" and "cup" as cup of coffee.

[Establish alignment as a property of a person]

A person has a number called the alignment.  
Alice is a person.   
Alice has alignment 10.  
Alice is in the Coffee Shop.
The alignment of the player is 1.

[Change the players alignment as they drink more coffee]

Instead of drinking cup of coffee (called the drink):
	if the alignment of the player is less than 10:
		Increment the alignment of the player;
		say "You take a sip of [the drink].  You feel invigorated.";
	otherwise:
		say "The [the drink] seems to have gotten cold.";
	stop the action.

[Make decisions based on alignment.]

Instead of telling someone about something, try asking the noun about it. Instead of answering the noun that something, try asking the noun about it.

Instead of asking Alice about anything:
	let A be the alignment of Alice;
	let B be the alignment of the player;
	if the absolute value of A minus B is greater than 4:
		say "You are not my type, sorry.";
	otherwise:
		say "Pleased to meet you!";
	stop the action.

Here is how it plays out (basically Bob needs to drink a lot of coffee to talk to Alice):

Coffee Shop
This is a small coffee shop with limited seating.

You can see a cup of coffee and Alice here.

>say hello
(to Alice)
You are not my type, sorry.

>drink coffee.  drink coffee.  drink coffee.
You take a sip of the cup of coffee.  You feel invigorated.

You take a sip of the cup of coffee.  You feel invigorated.

You take a sip of the cup of coffee.  You feel invigorated.

>say hello
(to Alice)
You are not my type, sorry.

>drink coffee.  drink coffee.  drink coffee.
You take a sip of the cup of coffee.  You feel invigorated.

You take a sip of the cup of coffee.  You feel invigorated.

You take a sip of the cup of coffee.  You feel invigorated.

>say hello
(to Alice)
Pleased to meet you!
1 Like