I7: picking up the happiest cat [solved]

Here’s some code that works, I guess, but I think I am missing something to make it work more sensibly and generally. I’d like to be able to name several objects with the same name. But I would like to be able to disambiguate them, too.

[code]“happycats” by andrew

a cat is a kind of thing. a cat has a number called happiness.

Ace is a cat. He is in room 1. happiness of Ace is 5. Bob is a cat. He is in room 1. happiness of Bob is 6. Doc is a cat. He is in room 1. happiness of Doc is 7. Ike is a cat. He is in room 1. happiness of Ike is 8. Max is a cat. He is in room 1. happiness of Max is 9.

the happiest-cat is a cat that varies.

to decide which cat is the hc:
let X be Ace;
repeat with Q running through cats:
if happiness of Q > happiness of X:
now X is Q;
decide on X.

[It seems that we can’t understand “X” as a variable. And that makes sense. But is there another general way to do things?] [understand “happiest cat” as hc.] [understand “happiest cat” as happiest-cat.]

understand “happiest cat” as ace when ace is happiest-cat. understand “happiest cat” as bob when bob is happiest-cat. understand “happiest cat” as doc when doc is happiest-cat. understand “happiest cat” as ike when ike is happiest-cat. understand “happiest cat” as max when max is happiest-cat.

room 1 is a room.

when play begins: now happiest-cat is the hc;

every turn:
now happiest-cat is the hc;
say “([happiest-cat] is the happiest cat.)”;

report taking happiest-cat:
ignore the standard report taking rule;
say “[happiest-cat] purrs as you pick him up.” instead;

check taking a cat:
if noun is not happiest-cat:
say “He struggles and jumps back to the ground.” instead;

test cats with “take happiest cat”.[/code]

Basically I want a way to define the happiest cat and have the user be able to define a cat as such and not just by their name. Of course, in this example, it takes less typing to say Ace or Max or whatever. But I am sensing I’m missing something about how to use definitions. In particular, the “understand X as…” and the happiest-cat/hc definitions appear to be much more awkward in my code than I need to be. I’d like to use this for priorities for disambiguation, e.g. so you can remember your favorite or something.

What do I need to fix so my code flows better/makes more sense?

The syntax you are looking for:

``````Understand "cat" as a cat.
Understand "happiest" as a cat when the item described is the happiest-cat.``````

(I’ve split up the lines because it’s better that way.)

1 Like

Also, it doesn’t make much sense having a variable happiest-cat and an adjective hc and keeping them in sync manually. You can just use “hc” anywhere and drop the variable.

1 Like

I don’t know if this is particularly useful, but you can use built-in comparisons to automate checking for the happiest cat, as in sections 6.7 and 6.8 of the documentation:

[code]Room 1 is a room.
A cat is a kind of thing. A cat has a number called happiness.
Definition: A cat is happy if its happiness is 2 or more. [The value doesn’t matter here; this just tells inform that larger number = happier cat]
Ace is a cat. He is in room 1. happiness of Ace is 5. Bob is a cat. He is in room 1. happiness of Bob is 6. Doc is a cat. He is in room 1. happiness of Doc is 7. Ike is a cat. He is in room 1. happiness of Ike is 8. Max is a cat. He is in room 1. happiness of Max is 9.

Understand “cat” as a cat. Understand “happiest” as a thing when the item described is the happiest cat.

Report taking the happiest cat:
ignore the standard report taking rule;
say “[The happiest cat] purrs as you pick him up.” instead;

check taking a cat:
if noun is not the happiest cat:
say “He struggles and jumps back to the ground.” instead;[/code]

As section 6.8 points out, this will yield unpredictable results when two cats are tied for the highest happiness score, but the same is true of your original code.

2 Likes

Thank you! That is the syntax I was looking for. It makes a few other things clearer for me, too.

Another section of the documentation I blew over at first just to make a game, any game…and now I understand why it’s there

Does this bit work? From what I’m aware, the “ignore (rule)” syntax is limited only to procedural rules.

Good point – you don’t need it, because the “report taking happiest cat” rule fires before the standard report taking rule and prevents it from firing (thanks to the “instead”).

For the report rule, I would go with:

```After taking the happiest cat: say "[The happiest cat] purrs as you pick him up." [Note: hardcoded "him" would not work for female cats, but there don't appear to be any in this example.]```

Using an “after” rule means the standard “taken” message will be automatically suppressed.

Also, you can simplify your cat declarations somewhat with:

``Ace, Bob, Doc, Ike, and Max are cats in Room 1.``

Thanks, yeah, I wasn’t exactly being tidy with the code. Cut and paste has dangers. I also keep forgetting to consider “after” for some reason. Possibly due to some early confusion there and not knowing about “continue the action” in case I do want to say more. So thanks for reminding me it’s useful and concise.