Cycling through piece conditions

So I have this code, which works, which can track which piece you’ve placed where on a board and whether you need to.

a piece can be irrelevant, placed or reserved.

chapter pie

pieing is an action out of world.

understand the command "pie" as something new.

understand "pie" as pieing.

carry out pieing:
	if number of irrelevant pieces > 0:
		say "IRRELEVANT PIECES:[line break]";
		repeat with P running through irrelevant pieces:
			say "[P] [if p is irrelevant](irrelevant) [end if][location of P].";
	if number of placed pieces > 0:
		say "PLACED PIECES:[line break]";
		repeat with P running through placed pieces:
			say "[P] [if p is placed](placed) [end if][location of P].";
	if number of reserved pieces > 0:
		say "RESERVED PIECES:[line break]";
		repeat with P running through reserved pieces:
			say "[P] [if p is reserved](reserved) [end if][location of P].";
	the rule succeeds;

The obvious question here is: can I compress the 3 loops into one?

SHOWME gives ‘piece condition 2: reserved’ but I don’t see how to say

    repeat with Q running through piece condition 2s:
        if number of Q pieces > 0, etc.

Is there a way to do this? Thanks!

Sure.

carry out pieing:
  repeat with P running through pieces begin;
     if P is irrelevant, say "[P] (irrelevant) [location of P].";
    else; if P is placed, say "[P] (placed) [location of P].";
    else; if P is reserved, say "[P] (reserved) [location of P].";
  end repeat;
  the rule succeeds;

Can be seen live and in action at Borogove Snippets whose existence I had managed to miss out on until now.

But now I’m pretty sure you were asking if you could avoid spelling out each separate value. And you can if you make irrelevant, reserved, placed a kind of value called, say, piece-status, and then said pieces have a piece-status called status.

Also, I was ignoring grouping them together. If that’s a crucial part of this, then, no, I don’t think you can avoid looping through pieces multiple times. It could become something like (pseudocode this time):

repeat with Q running through piece-statuses begin;
  repeat with P running through pieces begin;
    if the status of P is Q, say "[P] ([Q]) [location of P]";
  end repeat;
end repeat;
1 Like

Regarding your original question/suggestion:

Unfortunately, Inform isn’t able to compare to variables in that manner, at least not without some help (see also this thread):

"In Pieces"

The lab is a room.

A piece is a kind of thing.
a piece can be irrelevant, placed or reserved.

Foo is an irrelevant piece in the lab.
Bar is a placed piece in the lab.

Chapter Hack

To decide whether (O - object) has (P - L valued property) set to (V - value of kind L):
	(- (GProperty(OBJECT_TY, {O}, {P}) == {V}) -).

To decide what list of Ks is (full list - list of values of kind K) filtered by (P - L valued property) being (V - value of kind L):
	let the filtered list be a list of Ks;
	repeat with item running through the full list:
		if the item has P set to V, add the item to the filtered list;
	decide on the filtered list.

To say (P - a sayable value) in upper case:
	let T be "[P]";
	say "[T in upper case]".

chapter pie

pieing is an action out of world.

understand the command "pie" as something new.

understand "pie" as pieing.

carry out pieing:
	repeat with Q running through piece conditions:
		let L be the list of pieces filtered by the property piece condition being Q;
		if the number of entries in L is not zero:
			say "[Q in upper case] PIECES:[line break]";
			repeat with P running through L:
				say "[P] ([Q]) [location of P].";
	the rule succeeds.

Producing:

>pie
IRRELEVANT PIECES:
Foo (irrelevant) lab.
PLACED PIECES:
Bar (placed) lab.

Just as another alternative: if you don’t need the exact formatting from your example, you could also simply list them and keep the grouping by using the built-in list-writing facilities and adding the information per piece with a suitable “After printing the name of” rule.

A1 is a room. A2 is a room. A3 is a room. A4 is a room. A5 is a room. A6 is a room.

A piece is a kind of thing. A piece can be irrelevant, placed or reserved.

WPawn1 is an irrelevant piece in A1. BPawn1 is a placed piece in A2.
WKing is a placed piece in A3. WRook is an irrelevant piece in A4.
BKing is a placed piece in A5. BBishop is a reserved piece in A6.

Pieing is an action out of world.
Understand "pie" as pieing.

Carry out pieing:
	say "IRRELEVANT PIECES:[line break][list of irrelevant pieces].";
	say "[line break]PLACED PIECES:[line break][list of placed pieces].";
	say "[line break]RESERVED PIECES:[line break][list of reserved pieces].";

After printing the name of a piece (called P) while pieing:
	if P is irrelevant, say " (irrelevant) [location of P]";
	if P is placed, say " (placed) [location of P]";
	if P is reserved, say " (reserved) [location of P]";

Output:

>pie
IRRELEVANT PIECES:
WPawn1 (irrelevant) A1 and WRook (irrelevant) A4.

PLACED PIECES:
BPawn1 (placed) A2, WKing (placed) A3 and BKing (placed) A5.

RESERVED PIECES:
BBishop (reserved) A6.

If you want to have them on one line each, you could also add:

Before printing the name of a piece (called P) while pieing:
	say "[line break]";

Giving the output:

>pie
IRRELEVANT PIECES:

WPawn1 (irrelevant) A1 and
WRook (irrelevant) A4.

PLACED PIECES:

BPawn1 (placed) A2,
WKing (placed) A3 and
BKing (placed) A5.

RESERVED PIECES:

BBishop (reserved) A6.

2 Likes

Lots of answers already, but part of your question seemed to be how to get the compiler to understand what you want to iterate through. Try:

Carry out pieing:
    repeat with Q running through piece condition 2: [You were close! see WWI 4.10 Conditions of things]
	    say line break; [optional]
	    if the number of pieces that are Q is at least 1: [in lieu of "Q pieces"]
		    say "[Q]:[line break]" in upper case;
		    repeat with R running through pieces that are Q:
			    say "[R] ([Q]) [location of R]."

Note that this is really just a real code version of Zed’s pseudo-code, with a little extra formatting per your original code.

Also, as explained in WWI 4.10, you can also provide a name for the property instead of letting a generic name be assigned:

A piece can be irrelevant, placed or reserved (this is its relevance property). [no longer piece condition 2]

then

    repeat with Q running through relevance:
        ...

will work.

2 Likes

Me too. Until now. It’s always good to have one more resource. http://rosetta-code.com has been a big help for me as well to see how to do basic stuff in I6 or I7 or other languages I’ve wanted to learn.

Thanks for the link to the other topic (it touched on some questions I’d forgotten I wanted to know) and also for the I6 code. I’m weak on I6, and seeing it helps things click. It may not make sense at first until I understand what has to work, where.

This is one of those hints I keep forgetting and remembering. Formatting is tough!

Wow! Ten years after I discovered Inform 7, I’m still learning neat tricks like this that seem obvious once someone shows them to me. This makes things really easy for me. Also, thanks for the pointer to the documentation. I tend to read it to get what I want and say okay, that’s good enough, then later I find I’d have gotten more if I’d looked ahead.