Archive for April, 2010

Make All Sad at TWIFcomp

April 26th, 2010

TWIFcomp is a competition for works of interactive fiction with source code not exceeding the length of a tweet. (Unlike on Twitter, whitespace characters do not count toward the total.) There were 61 entries, of which the largest proportion were in Inform 7. My entry, Make All Sad was written in perl. If you really insist on playing it unspoiled, and you have perl installed, you should go there before reading any further.

Aaron A. Reed at >TILT AT WINDMILLS has a good post highlighting a selection of the games. The space of 140 characters really only suffices for one gag, but a pretty wide variety of gags were tried. Adam Thornton gets a special, “if they ever do this again there will have to be a new rule” award for encoding a full length game that’s been years in the making as several hundred megabytes of whitespace characters, with a short perl script to decode it. (Assuming that gets a normal release soon, I’m looking forward to playing it.)

Choosing to use perl instead of a dedicated IF language meant I didn’t get a parser, world model, built in verbs, or even a prompt for free. (I spent 9 characters on the “>” prompt, which was extravagant, but I felt it necessary to give my game the feel of IF.) I do however get text mangling for cheap, and a good set of general programming operators.

My first important design decision was that I was not going to attempt to parse input, but would instead map the length of the input to the effect. This way, you could type any normal IF command, (“TAKE ROCK”, “GO NORTH”, etc.) and it would have an effect on the game, giving the illusion that there is a parser doing something there. (For the sake of having a better way to leave the game than CTRL-C, I did catch just one command: QUIT.)

The other piece of the puzzle was creating something not utterly trivial to do with this piece of information. I settled on having a set of three “objects” and a set of three possible adjectives, with a goal of getting all of the adjectives into a particular state. The game would print “The x is y.” for each object and its current adjective. (In the end, I could find no other way to excise the last character I needed to get down to 140 but to change “The” into “My”. I’m actually happy with that; this way feels more flavorful and implies an NPC behind the words.) Saving characters by having the nouns and adjectives share their final letters was an idea I had before I started coding, and gives the output a bit of visual poetry.

Finally, I needed a function to map the input length to the effect. I wanted different lengths to do different things to the three adjectives, sometimes changing all three, sometimes keeping some of them the same. And I needed something extremely terse. The shift operator “>>” saved me here. The function has period 12, so “MAKE ALL SAD” (12 characters) has no game effect. As a fortuitous coincidence, none of the 12 possibilities can win the game in a single turn.

The rest was perl golf, as the pastime of writing perl programs in as few (key)strokes as possible is called. There is a primer here that gave me some helpful ideas. In standard perl golf, whitespace counts; since it didn’t here, initializing the noun/adjective array by splitting on the space character was a win.

If I had a few more characters, I would have liked to end the game with a victory message in the event of all being sad. But I’m reasonably happy with how it turned out.

The source:
@o = split ' ', 'ma cu to sa re od';
@s = 0..2;
do
{
    map
    {
        $a = $s[$_] += $x >> $_;
        printf "My %sp is %sd.
", $o[$_], $o[$a % 3 + 3]
    } 0..2;
    print ">";
    $_ = <>;
    $x = y///c - 1
}
until /quit/i

Edit: Naturally, after the competition deadline and after I wrote up this post, I found a couple good ways to squeeze out a bunch of characters, and a feature I could add that could fit into the extra space. The new feature is that “G”, the standard abbreviation for AGAIN in IF, repeats the last command. This only took 11 characters to achieve.

The code for the new (and hopefully final) version follows:

@o = split ' ', 'ma cu to sa re od';
@s = 0..2;
do
{
    print "My $o[$_]p is $o[($s[$_] += $x >> $_)  % 3 + 3]d.
" for 0..2;
    print ">";
    $_ = <>;
    /^g$/i || ($x = y///c - 1)
}
until /^quit$/i

Gordon Hamilton’s Polyanimal Zoo

April 6th, 2010

Here’s a problem that I heard from Gordon Hamilton at Gathering for Gardner 9, and tracked down to an article of his in issue #10 of Pi in the Sky, a western Canadian math magazine for high school students and teachers.

A polyomino animal can eat another polyomino animal (his perhaps overly cute term is “polyanimal”) if the second one can be placed inside the first. Find animals of sizes 4, 5, 6, 7, 8, 9, and 10 that can live together peacefully (none can eat any of the others) within a 7×7 square pen.

This is really a satisfying puzzle to solve. Usually in polyform tiling puzzles, you spend a fair amount of time feeling out the territory, learning which pieces like to go in certain places, and which you want to deal with first and which you want to save for the end. But then, the larger part of the solving time is spent in trial and error with various configurations attempted at random until at last you run into a solution.

Here, the whole solving process is learning about the territory of the puzzle, and none of it feels like random crunching. I highly recommend giving it a try, but if you just want to see a solution, mine is here.

Of course, the matter of polyominoes fitting inside other polyominoes is an area that I’ve dealt with in my Polyomino Cover material, which I summarized in my presentation for G4G9. And one of the problems in Hamilton’s Polyanimal problem set is the same as what I’ve called the minimal pentomino cover problem. But most of them are completely different, which only reinforces my belief that this is an area with a lot more waiting to be discovered. (His last problem is “Design a Polyanimal Game.” Now that’s open-ended and provocative!)