Tag Archives: c

First steps: Frotz in Eclipse

I’ve looked at a lot of the Frotz source code. It’s undeniably simple and yet, it’s a big challenge for me. First of all, it is a virtual machine, no matter how you want to put it. Second, it’s not really that well commented. I’m still pretty enthusiastic about the whole thing. One aspect I had actually completely overlooked is how this project is getting me much close to Eclipse.


Eclipse logo

Eclipse is a very popular Java-based IDE, or Integrated Development Environment. Originally made for Java, it has been adapted for most languages one could reasonably expect it to work with, including of course C and C++. The needs of this project and the time constraints encourage me to use Eclipse and to use it in a more efficient way I have so far.

I’m using it to find out where variables and functions have been declared automatically, for example. It has more than one feature that help with that.

Real-life code

Frotz is definitely real-life code. I’m not in a world of textbooks anymore. This is an example of a comment I found:

For a very different example, I found this nifty piece of code that takes a full filepath and only keeps the basename:

This uses pointers and the fact that in C, there are no strings but zero-terminated character arrays. I actually like seeing a real-life example of this sort of thing.

To be continued.

My topic: analyzing and extending Frotz

Ah, Frotz. The most popular Z-machine and now glulx interpreter out there… Let’s back up for a while.


The first commercially available text-based adventure game was Zork I by Infocom in 1980. It was based on its almost-namesake Zork, that was made at MIT and only ran on big mainframes. For the commercial version, Infocom needed a very simple and versatile format. So they created the Z-machine format. Its only output was text, so interpreters for it could be made for pretty much any platform — and it was.

After Infocom

To my knowledge, the last commercial text-based adventure game was Eric the Unready. After that time, a new interpreter for Z-machine files ended up being necessary. A few appeared. The most popular ended up being Frotz, a command-line tool for Unix. Frotz was eventually ported to many systems, including Windows and more recently iOS.

Adventure games are not dead. Well, maybe not

Even though the number of people who play adventure games has shrunk and the number of people who play text-based adventure games or “Interactive Fiction” or simply IF is now tiny, a small community remains. New tools were made for creating new pieces of IF. Some were brand new, some compiled to the old Z-machine format. At the forefront of that latter category was the Inform programming language and its accompanying system, both created by Graham Nelson, a British poet and mathematician.

As the Z-machine had never been meant to be used by the public before, there was never a standard for it. Graham Nelson established one, aptly called the Z-machine standard in 1997. It has been updated since to version 1.1 in 2004. These standards are now used by most Z-machine interpreters.

A short note about Inform

Graham Nelson’s Inform language was very simple up to version 6. What made it a very powerful tool is that it came built-in with IF-specific rules. IF does require many rules to for the world of the story to behave in the way we as human players expect. These rules could be overwritten, but Inform did not require the programmer to write them. Example of rules could be that if an object is marked as being a container, it can contain other objects. However, an object cannot contain itself. This seems obvious to humans, but has to be coded in order to be in the world. Inform 7 goes much further and actually reads like English. It can still compile to the Z-machine format.

What I intend to do with all that

My independent study this semester will be the following:

  • analyze the Z-machine, using existing interpreters as well as Graham Nelson’s standards,
  • expose parts of the story inside the Z-machine and
  • display the information I gathered in the Windows interface in the Frotz for Windows interpreter.

I don’t know for sure how much of these I’ll be able to do, apart for the first one. But the Z-machine has such a reputation for being a very simple virtual machine that I hope I’ll be able to do all of them.

Deciding on a study topic for Silicon Valley

Creating Interactive Fiction with Inform 7 by Aaron Reed coverSo we have to create a blog for our Silicon Valley elective class. But I already have a blog! And I’ve even updated it. Sort of. Some times. I think.

No worries. I’ll try using a category for the elective-related posts. Let’s hope that will be acceptable.

I was hesitant about my topic for this class. I saw three possibilities. I knew I had to have samples of C++ code by the time this semester was over, but this didn’t seem to really match the purpose of this class. I don’t have much experience with C++, but I already know it fairly well. The point of the class it to learn things and teach them at he end of the semester. I can practice C++ and then teach it, but that does not feel right. And it wouldn’t really even be learning, it would really be practicing. If it’s code samples I’m after (and I am, I need them to get a job), I think I can manage that on my own.

Instead I’m going to do something with Inform 7. The previous article on this blog was about why I love Scala. This was written when I thought I’d be on a pitch project right now, coding in Scala, that why it had “part 1” in its name even though there never was a part 2. But it’s true I love Scala. I love Scala more than I can describe. But that’s nothing compared to how much I love Inform 7.

I’ve taught the very, very basics of Inform 7 when I was a teaching assistant (or T.A.) during the National High School Game Academy  But, to be fair, I’m a little lost about the details of Inform 7 myself. It’s big, powerful and detailed. It deserves my attention. One of our advisers said C++ was the one to go with because it gets jobs, but the other one said that Inform 7 would make me stand out. I’m much less sure I can write a decent piece of Inform 7 on my own time than a decent piece of C++. C++ you can crank out. Inform 7 must be slowly and lovingly carved out of a fine alloy of effort and imagination.

I’ll try to revive my old idea for an Inform 7 game, Go, Jill!, maybe with less focus on dialog and more on some of Inform 7’s specific features, like the persuasion system, which I think is very impressive. Scope will be an issue, but I’ll try to shrink and expand it as necessary. I’m pretty confident I can make it work.

You are in a maze of twisty little passages, all alike.

Not anymore I’m not!

Why I love Scala – Part 1: Boxes

I Love ScalaThe Scala programming language logo

Here’s a small example of why I love Scala. First of all, if you don’t know what Scala is, it’s a relatively recent language that compiles to Java binaries that can be run on the Java Virtual Machine. It keeps the best from Java: the JVM is now very fast, very efficient and runs on pretty much every device you could want. Plus, all Java classes are also Scala classes. So as long as something has been ported to Java, you can use it in Scala without waiting for someone to port it. The language itself hardly has anything to do with Java. Unlike Java, Scala is extremely concise and makes very high and frequent use of implicit typing and implicit declarations. In Scala, when

you don’t need to tell Scala that a in an Int. Scala will figure it out for you. It is strictly statically typed, but you don’t need to state the obvious about types. Just program what you need.

The Problem

In this post, I’m going to talk about a programming issue that Scala handles, I think, better than any programming language I’ve ever use: the special unassigned case.

For example, let’s say that you have a string, for example, "abc". And you want to find the first occurrence of a specific character in that string. In Java, for example, you’d use indexOf:

this would evaluate to 2. The index of "a", would evaluate to 0. And that’s bad. That’s very bad. Because

is false, even though a definitely has an index. In most languages, 0 is false, even tough it doesn’t always mean false. And how about the index of "w"?

Java and C++ use the special value -1 as a way to indicate that the index was not found. C doesn’t have an equivalent for indexOf, as it doesn’t even technically have strings but char arrays. But this clever little bit of code: <http://cboard.cprogramming.com/c-programming/100548-strpos-function-c.html> also uses -1 as the special “not found” value.

There are many problems with using -1 as meaning “not found”. First of all, -1 simply does not actually mean “not found.” Second, unlike 0, -1 evaluates to true. All in all, this is usable, but not ideal.

Let’s look at the haphazard mess that is PHP. PHP gives us strpos(). That function will return 2 for strpos(“abc”, “c”) and 0 for strpos("abc", "a"). And in PHP, 0 will also evaluate to false. But for strpos("abc", "w"), it will return a boolean false, which is equal to 0, but not identical. That means that even though all the programming teachers in the world have been, rightly, encouraging their students not write code like

PHP actually requires us to write

And then re-evaluate the whole thing or save the result in the first place. That’s PHP-style messiness.

Python gives us a much cleaner, safer, more elegant and semantically accurate way of dealing with the whole matter. Each string as a built-in index function:

If an unknown index is found, Python will raise an exception, which is perfectly fine in Python. Pythonic code means that it’s better to ask for forgiveness than permission, unlike the C, C++ and Java principle of “look before you leap.” So in Python, you’d do:

Ta da! If you really want to get a -1 when the value is not found, you can always use the find() method instead of index() that will do just that: return -1 if the substring is not found. But if you do that, it probably means that you just don’t understand Python. Ugh.

A Solution

The question now is: how does Scala do it? Well, here my argument sort of falls apart at first. Scala tries not to add too much to what Java already does well, this means it doesn’t implement strings at all and just uses Java’s Strings. By default, Scala uses Java’s bad way of handling that example. Let’s fix this. Let’s create a new class, a SuperString, that will actually deal with this situation in a more idiomatic way. (We’ll see the details on how to do that later.) Our new strings will have a method called find that will do things the Scala way!

What does that return? Well, since SuperString does things the Scala way, it won’t in fact return an Int. Scala has Ints, of course, but Ints aren’t really what we need here, because, precisely, there is a special case where we don’t get a result at all.

Scala provides us with a very powerful tool for that situation: the Option. David Pollack, creator of Scala’s killer app, the Lift Web Framework, thought that Options were great but that it was possible to do even better, so he did. He re-did his own Option class: the Box.

A Box can contain one of three values.

If something went seriously wrong, the Box will contain the special Failure object. This is not for special cases like when a substring is not found, or Python’s ValueError or KeyError exceptions. This is for when something really bad happens.

If the Box has no value in it, it contains the special Empty object.

If the Box does have something in it, it contains the special Full object. Boxes and Full are both generic types. Unlike C++, C# and Java, where type arguments are surrounded by angle brackets, in Scala, generic arguments are surrounded by square brackets. So in our example, the find() method will return a Box[Int], that can contain a Failure, an Empty or a Full[Int].

So now that we have our Box[Int], what can we do with it? As it turns out, quite a lot. First of all, we can check if it’s empty. If result is the Box[int]

will be true if the Box contains an Empty or a Failure, false otherwise.

will be true if the Box contains a Full, false otherwise.

But you usually won’t need to use those. You can, in theory, open a Box with open_!

But again, what’s the point of having a Box if you’re just going to open it? The very fact that there is an exclamation point in the name is meant to warn you that you’d better not be using it unless you have a good reason. If the Box contains anything other than Full, it’s going to throw an exception anyway when it’s opened. And if you check to see if it’s defined first, it’s just as clumsy as the C++, C# and Java ways. No. One of the things you’re likely want to do with the Box, is, say, for example, map it. As a functional language, Scala is very big on mapping.

What that does is that, if the Box is Full, it takes what’s inside of it, puts it in a variable, here r, lets you use it in a function, lambda like here, or otherwise, and then re-package it in a Box. If the original Box contained an Empty or a Failure, the new one will too. It’s that simple.

But wait! Scala has other cool tricks going on here. result is a Box[Int], right? So r has to be an Int. It can’t be anything else! No point in telling Scala about it. Scala can figure it out. It’s actually considered good practice to let Scala deal with it.

But wait! We’re still not done! We have one argument and we’re using it once. In that case, we don’t even have to name it. If all your arguments are only used once and in the order they were passed, Scala can invoke them automatically with the default variable _. So really, all we need here is:

At this point, this no longer returns a Box[Int] but a Box[String]. Any type of object can be Boxed, including user-defined objects, of course.

Now, if we need to do something with our String, we will at some point need to open the Box. But what if our String was not found and our Box is Empty? The Box lets us deal with that with the special operator openOr, which takes as an argument a default value in case the Box is Empty:

But wait! It’s not all! Since Scala is essentially a functional language, it doesn’t need dots and parentheses for method invocation that only take one argument. So, really, all we need here is:

A few parentheses are remaining. Let’s get rid of them by using format instead of those messy plus signs:

And finally:

This returns either something like The character was at position 0. or The character was not found. Perfect. Clear. Simple.

But we can still do more! By which I mean less, in the long term.

I Really Shouldn’t Have to Say This

This only worked because we used the find method on our SuperString.

Just to be explicit, the class SuperString is nothing more than:

Oops. This class does not actually use Boxes. Let’s add them.

(Yes, in Scala, classes take arguments. That means that a class declaration is also part of the constructor. Scala likes concision.)

Of course, a String can easily be converted to a SuperString with a function like:

So, what this would give us is:

  • A class called SuperString that has a find method,
  • A String class with which we could probably make good use of our find method, but that we can’t extend because it’s final,
  • A method that can totally convert an object of one class (String) into an object of the other (SuperString).

Couldn’t Scala figure out on its own how to combine these three things? Well, yes and no. Yes, it could but that would be messy. What if we don’t want it to? Just because we can convert one type into another doesn’t mean we always want to. So what if we indicated that we do want to? Scala does let us tell it that with the almost magical implicit keyword. Let’s make that function from before implicit:

So now, code like:

will return a Full(0) and

will return an Empty. When scala fails to find a find method in the String, it tries to look for another type of object that has a find method and sees if there is an implicit way it can do the conversion. If it can, it does. And so, it looks like we’ve added a method to a final class, when in fact we’ve done nothing of the sort. We could also remove the dot and parentheses for the method call. Super cool.

The Final Line

So to get our index and format it and provide a default message if it fails, all we have to do now is:

This will evaluate to:

Whereas this:

evaluates to:

This is fully statically typed, extremely concise, clear and powerful. We don’t need a special case to check if the text is not found, it’s all handled by the Box. We don’t need to specify a default output for the special case, the Box handle that as well.

Once you can wrap your head around the basic concepts, it’s all smooth sailing.

Final Note

This code uses libraries from the Lift web framework. To get Scala working with them, the easiest thing to do is to run it from the Lift console. Download Lift. Make sure you have Java installed. You don’t actually need Scala. Lift will get its own version of Scala for you. From a Lift project’s main directory, run:

Then, from the Scala console, run:

You’re set.