{"id":20,"date":"2011-05-26T20:09:51","date_gmt":"2011-05-27T01:09:51","guid":{"rendered":"http:\/\/www.playwithlua.com\/?p=20"},"modified":"2011-05-26T20:09:51","modified_gmt":"2011-05-27T01:09:51","slug":"interactive-fiction-part-1","status":"publish","type":"post","link":"http:\/\/www.playwithlua.com\/?p=20","title":{"rendered":"Interactive Fiction (part 1)"},"content":{"rendered":"<p>Like it or hate it, and there are plenty of reasons to do both, object-oriented programming is an idea that you can&#8217;t escape. Practically every language and every library uses it. Every CS curriculum teaches it. Every learn-to-program tutorial explains it. And so, people have some funny ideas about what it exactly <em>is<\/em>. If your first language supports it (like Java or Ruby, say) then it becomes very hard to distinguish between <em>object-oriented programming<\/em> and <em>how your language does object-oriented programming<\/em>.<\/p>\n<p>Unless you&#8217;re using Lua. Because OO in Lua is whatever you write for it. The language doesn&#8217;t offer any of what a Java programmer would consider essential features, like classes, instance variables, even types. What it offers is a way to <em>make<\/em> those things. So that&#8217;s what I&#8217;m going to do over the next couple posts, in the process of writing a miniature interactive fiction engine. At the end it&#8217;ll look a little bit like <a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/eblong.com\/zarf\/essays\/rule-based-if\/index.html');\"  href=\"http:\/\/eblong.com\/zarf\/essays\/rule-based-if\/index.html\">this<\/a> except that I&#8217;m going to make a bunch of compromises that Zarf never would, because he&#8217;s smarter than I am and can foresee how it&#8217;ll mess everything up. Also because he intends to use his for longer than a blog post.<br \/>\n<!--more--><\/p>\n<h2>Game design<\/h2>\n<p>First, let&#8217;s define what we&#8217;re going to do. We want to have a system that simulates a world, which interacts with the user solely through text. We&#8217;ll give it commands like &#8220;get lamp&#8221; or &#8220;walk north&#8221; or &#8220;put chalice on altar&#8221;, and it will change the game state in some way and respond with a message. First, let&#8217;s talk about how we&#8217;re going to represent the game world.<\/p>\n<p>As far as nouns go, there are two kinds of things in our game: there are rooms, which are the locations the player can be in (&#8220;you are in the kitchen&#8221;), and there are props, which are the things that are in the rooms (&#8220;there is a knife and a pan of brownies here&#8221;). Props can also be other places too, like in the player&#8217;s inventory or inside other props (&#8220;the chest contains a sword and a blue cloak&#8221;). So the world is a directed graph of rooms, and each room is the root of a tree of objects. In addition to this, there can be other &#8220;global&#8221; factors at play, like if it&#8217;s dark then all room descriptions are &#8220;it&#8217;s dark, you may be eaten by a Grue&#8221;. We&#8217;ll have a list of global rules that aren&#8217;t part of the world graph, but get first dibs on any command.<\/p>\n<p>Time for a big helpful picture or two. Here&#8217;s how the rooms will be laid out:<br \/>\n<img src=\"http:\/\/www.playwithlua.com\/wp-content\/tfo-graphviz\/ce10947893110b7a4452c202caffe93f.png\" class=\"graphviz\" \/><\/p>\n<p>And here&#8217;s the layout of items within a single room:<br \/>\n<img src=\"http:\/\/www.playwithlua.com\/wp-content\/tfo-graphviz\/7b022dc2b12083e9a449902729fe5e04.png\" class=\"graphviz\" \/><\/p>\n<p>This first post, we&#8217;re going to lay the groundwork, and talk about how to implement rooms. First though, since this is ostensibly about object-oriented programming, it&#8217;s about time we mentioned objects.<\/p>\n<h2>Basic objects<\/h2>\n<p>An object, in the Java \/ C++ sense, is an instance of a class. And a class is a user-defined datatype (where you, the programmer, are the user; classes are typically defined in code), just like &#8220;string&#8221; or &#8220;array&#8221;. This method of OO is almost totally dominant, to the point where languages like Javascript are thought of as not supporting OO because they use a different model. Really, though, an object is anything that fits these criteria:<\/p>\n<ul>\n<li>Contains some related state.<\/li>\n<li>Contains the code to manage that state<\/li>\n<\/ul>\n<p>Se let&#8217;s say that a room has a name (&#8220;Kitchen&#8221;), a description (&#8220;A small apartment kitchen. Nothing fancy but good enough for making brownies and frozen pizza&#8221;), and a set of exits (south to the bedroom, west to the living room). That&#8217;s its state. We can represent this in Lua with a table:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">kitchen <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #66cc66;\">&#123;<\/span>\n    name <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #ff6666;\">&quot;Kitchen&quot;<\/span><span style=\"color: #66cc66;\">,<\/span>\n    description <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #ff6666;\">&quot;A small apartment kitchen. Nothing fancy . . .&quot;<\/span><span style=\"color: #66cc66;\">,<\/span>\n    exits <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #66cc66;\">&#123;<\/span> w <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #ff6666;\">&quot;LivingRoom&quot;<\/span> <span style=\"color: #66cc66;\">&#125;<\/span>\n<span style=\"color: #66cc66;\">&#125;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>Now we need the operations to manage the data. We could just make these be functions stored in the table, but we have to assume that we&#8217;ll have lots of different rooms sharing the same functions (after all, what does <code>print_description<\/code> do differently for the bedroom than the kitchen), so we should have a way to share these across different objects. Lua provides a facility called <em>metatables,<\/em> which control how tables respond to different events, like someone looking for a key that isn&#8217;t there. In that case Lua looks in the metatable under <code>__index<\/code>, and if that&#8217;s a function it calls it (with the table and the missing key). As a shortcut, if <code>__index<\/code> is a table it just looks in that table for the missing key.<\/p>\n<p>So let&#8217;s take all the code to manage a room&#8217;s state and put it in a table, room_methods:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">room_methods <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #66cc66;\">&#123;<\/span>\n    print_description <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #aa9900; font-weight: bold;\">function<\/span><span style=\"color: #66cc66;\">&#40;<\/span>room<span style=\"color: #66cc66;\">&#41;<\/span>\n        <span style=\"color: #0000aa;\">print<\/span><span style=\"color: #66cc66;\">&#40;<\/span>room<span style=\"color: #66cc66;\">.<\/span>name <span style=\"color: #66cc66;\">..<\/span> <span style=\"color: #ff6666;\">&quot;<span style=\"color: #000099; font-weight: bold;\">\\n<\/span><span style=\"color: #000099; font-weight: bold;\">\\n<\/span>&quot;<\/span> <span style=\"color: #66cc66;\">..<\/span> room<span style=\"color: #66cc66;\">.<\/span>description<span style=\"color: #66cc66;\">&#41;<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n    <span style=\"color: #808080; font-style: italic;\">-- and so on...<\/span>\n<span style=\"color: #66cc66;\">&#125;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>Now we can set it as the metatable&#8217;s <code>__index<\/code> table, and call things in it:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\"><span style=\"color: #0000aa;\">setmetatable<\/span><span style=\"color: #66cc66;\">&#40;<\/span>kitchen<span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #66cc66;\">&#123;<\/span> __index <span style=\"color: #66cc66;\">=<\/span> room_methods <span style=\"color: #66cc66;\">&#125;<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\nkitchen<span style=\"color: #66cc66;\">.<\/span>print_description<span style=\"color: #66cc66;\">&#40;<\/span>kitchen<span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>There&#8217;s even syntactic sugar so that we don&#8217;t have to pass in the first argument by hand:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">kitchen<span style=\"color: #66cc66;\">:<\/span>print_description<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>Note that the <em>metatable<\/em> isn&#8217;t what we look for the functions in, the <em><code>__index<\/code> field in the metatable<\/em> is. We can also put things in other slots in the metatable, like <code>__tostring<\/code>. The tree looks like this:<\/p>\n<img src=\"http:\/\/www.playwithlua.com\/wp-content\/tfo-graphviz\/b788195ea0f40c9fc7a8498fcd48bdd3.png\" class=\"graphviz\" \/>\n<p>So that accounts for the state and the functions to handle the state. Notice that nowhere in there is there anything that corresponds to a class, or a type. This is just an object. The only primitives used at all are tables. Since we made this system ourselves in the language, instead of it being provided for us <em>by<\/em> the language, we can mold it some to fit our problem better.<\/p>\n<h2>Refining our objects<\/h2>\n<p>I think later on we&#8217;ll want to have objects that share the characteristics of multiple different kinds of objects, like a glowing sword has all the &#8220;weapon&#8221; properties as well as all the &#8220;light source&#8221; properties. We won&#8217;t be able to do that if we just shove all the functions into an <code>__index<\/code> table, but if we have an <code>__index<\/code> function that can look in multiple tables&#8230;<\/p>\n<p>So let&#8217;s make a couple generic functions for dealing with things like this, and that&#8217;ll be the basis of our object system. Start with a <code>become<\/code> function that takes a table of state (an object) and a table of functions (some operations), and adds those operations to that object:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\"><span style=\"color: #aa9900; font-weight: bold;\">function<\/span> become<span style=\"color: #66cc66;\">&#40;<\/span>obj<span style=\"color: #66cc66;\">,<\/span> operations<span style=\"color: #66cc66;\">&#41;<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">local<\/span> m <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #0000aa;\">getmetatable<\/span><span style=\"color: #66cc66;\">&#40;<\/span>obj<span style=\"color: #66cc66;\">&#41;<\/span>\n&nbsp;\n    <span style=\"color: #aa9900; font-weight: bold;\">if<\/span> <span style=\"color: #aa9900; font-weight: bold;\">not<\/span> m <span style=\"color: #aa9900; font-weight: bold;\">then<\/span> <span style=\"color: #808080; font-style: italic;\">-- No metatable yet; we'll add one<\/span>\n        m <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #66cc66;\">&#123;<\/span>behaviors<span style=\"color: #66cc66;\">=<\/span><span style=\"color: #66cc66;\">&#123;<\/span><span style=\"color: #66cc66;\">&#125;<\/span><span style=\"color: #66cc66;\">,<\/span> __index<span style=\"color: #66cc66;\">=<\/span>dispatch<span style=\"color: #66cc66;\">&#125;<\/span>\n        <span style=\"color: #0000aa;\">setmetatable<\/span><span style=\"color: #66cc66;\">&#40;<\/span>obj<span style=\"color: #66cc66;\">,<\/span> m<span style=\"color: #66cc66;\">&#41;<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n&nbsp;\n    <span style=\"color: #0000aa;\">table.insert<\/span><span style=\"color: #66cc66;\">&#40;<\/span>m<span style=\"color: #66cc66;\">.<\/span>behaviors<span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #66cc66;\">,<\/span> operations<span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #aa9900; font-weight: bold;\">end<\/span><\/pre><\/div><\/div><\/div>\n\n<p>The <code>dispatch<\/code> function in that metatable will look in all the <code>behaviors<\/code> tables in order and return the first matching method it sees:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\"><span style=\"color: #aa9900; font-weight: bold;\">function<\/span> dispatch<span style=\"color: #66cc66;\">&#40;<\/span>obj<span style=\"color: #66cc66;\">,<\/span> message_name<span style=\"color: #66cc66;\">&#41;<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">local<\/span> m <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #0000aa;\">getmetatable<\/span><span style=\"color: #66cc66;\">&#40;<\/span>obj<span style=\"color: #66cc66;\">&#41;<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">if<\/span> <span style=\"color: #aa9900; font-weight: bold;\">not<\/span> m <span style=\"color: #aa9900; font-weight: bold;\">then<\/span> <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> <span style=\"color: #aa9900;\">nil<\/span> <span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n&nbsp;\n    <span style=\"color: #aa9900; font-weight: bold;\">for<\/span> _<span style=\"color: #66cc66;\">,<\/span> operations <span style=\"color: #aa9900; font-weight: bold;\">in<\/span> <span style=\"color: #0000aa;\">ipairs<\/span><span style=\"color: #66cc66;\">&#40;<\/span>m<span style=\"color: #66cc66;\">.<\/span>behaviors<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">do<\/span>\n        <span style=\"color: #aa9900; font-weight: bold;\">if<\/span> operations<span style=\"color: #66cc66;\">&#91;<\/span>message_name<span style=\"color: #66cc66;\">&#93;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">then<\/span> <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> operations<span style=\"color: #66cc66;\">&#91;<\/span>message_name<span style=\"color: #66cc66;\">&#93;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n&nbsp;\n    <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> <span style=\"color: #aa9900;\">nil<\/span>\n<span style=\"color: #aa9900; font-weight: bold;\">end<\/span><\/pre><\/div><\/div><\/div>\n\n<p>So now creating our kitchen will look like this:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">Room <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #66cc66;\">&#123;<\/span><span style=\"color: #66cc66;\">&#125;<\/span>\n<span style=\"color: #aa9900; font-weight: bold;\">function<\/span> Room<span style=\"color: #66cc66;\">.<\/span>print_description<span style=\"color: #66cc66;\">&#40;<\/span>room<span style=\"color: #66cc66;\">&#41;<\/span>\n    <span style=\"color: #0000aa;\">print<\/span><span style=\"color: #66cc66;\">&#40;<\/span>room<span style=\"color: #66cc66;\">.<\/span>name <span style=\"color: #66cc66;\">..<\/span> <span style=\"color: #ff6666;\">&quot;<span style=\"color: #000099; font-weight: bold;\">\\n<\/span><span style=\"color: #000099; font-weight: bold;\">\\n<\/span>&quot;<\/span> <span style=\"color: #66cc66;\">..<\/span> room<span style=\"color: #66cc66;\">.<\/span>description<span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n<span style=\"color: #808080; font-style: italic;\">-- and so on...<\/span>\n&nbsp;\nkitchen <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #66cc66;\">&#123;<\/span>\n    name <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #ff6666;\">&quot;Kitchen&quot;<\/span> <span style=\"color: #808080; font-style: italic;\">-- And all the rest . . .<\/span>\n<span style=\"color: #66cc66;\">&#125;<\/span>\n&nbsp;\nbecome<span style=\"color: #66cc66;\">&#40;<\/span>kitchen<span style=\"color: #66cc66;\">,<\/span> Room<span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>Okay, so, now we&#8217;re talking. We can now represent a single room, so a table of these is a list of the rooms. Let&#8217;s make that table (which I won&#8217;t do inline because it&#8217;ll be long and repetitive), and then start thinking about how to interact with them.<\/p>\n<h2>Game engine<\/h2>\n<p>What we&#8217;ll do is have a function that takes a table representing a command (it will have a verb, and an optional subject and direct object) and then modify some game state based on that. We can pretty easily represent this as an object called &#8220;game&#8221;. The game object will have a current room and a list of global &#8220;rules&#8221;:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">Game <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #66cc66;\">&#123;<\/span><span style=\"color: #66cc66;\">&#125;<\/span>\n&nbsp;\ngame <span style=\"color: #66cc66;\">=<\/span> new<span style=\"color: #66cc66;\">&#40;<\/span>Game<span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #66cc66;\">&#123;<\/span>\n        current_room <span style=\"color: #66cc66;\">=<\/span> ROOMS<span style=\"color: #66cc66;\">.<\/span>Bedroom<span style=\"color: #66cc66;\">,<\/span>\n        <span style=\"color: #0000aa;\">globals<\/span> <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #66cc66;\">&#123;<\/span><span style=\"color: #66cc66;\">&#125;<\/span>\n<span style=\"color: #66cc66;\">&#125;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>(<code>new<\/code> is a convenience function that takes a type and a table, calls <code>become<\/code>, and returns a new object)<\/p>\n<p>A &#8220;global rule&#8221; is a function that takes a game and a command, and returns either a string to say to the player (if it knows how to interpret that command) or nil (if this rule doesn&#8217;t affect that command). This is how we&#8217;ll handle all verbs that aren&#8217;t tied to a particular room or object:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">game<span style=\"color: #66cc66;\">:<\/span>add_global<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #aa9900; font-weight: bold;\">function<\/span><span style=\"color: #66cc66;\">&#40;<\/span>game<span style=\"color: #66cc66;\">,<\/span> command<span style=\"color: #66cc66;\">&#41;<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">if<\/span> command<span style=\"color: #66cc66;\">.<\/span>verb <span style=\"color: #66cc66;\">==<\/span> <span style=\"color: #ff6666;\">&quot;walk&quot;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">then<\/span>\n        <span style=\"color: #aa9900; font-weight: bold;\">local<\/span> new_room <span style=\"color: #66cc66;\">=<\/span> game<span style=\"color: #66cc66;\">.<\/span>current_room<span style=\"color: #66cc66;\">:<\/span>room_in_dir<span style=\"color: #66cc66;\">&#40;<\/span>command<span style=\"color: #66cc66;\">.<\/span>subject<span style=\"color: #66cc66;\">&#41;<\/span>\n        <span style=\"color: #aa9900; font-weight: bold;\">if<\/span> new_room <span style=\"color: #aa9900; font-weight: bold;\">then<\/span>\n            game<span style=\"color: #66cc66;\">.<\/span>current_room <span style=\"color: #66cc66;\">=<\/span> new_room\n            <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> new_room<span style=\"color: #66cc66;\">.<\/span>name <span style=\"color: #66cc66;\">..<\/span> <span style=\"color: #ff6666;\">&quot;<span style=\"color: #000099; font-weight: bold;\">\\n<\/span><span style=\"color: #000099; font-weight: bold;\">\\n<\/span>&quot;<\/span> <span style=\"color: #66cc66;\">..<\/span> new_room<span style=\"color: #66cc66;\">.<\/span>description\n        <span style=\"color: #aa9900; font-weight: bold;\">else<\/span> <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> <span style=\"color: #ff6666;\">&quot;There is no exit that way&quot;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n<span style=\"color: #aa9900; font-weight: bold;\">end<\/span><span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>So, in order to handle any command (so far, anyway) we just have to have <code>game:input<\/code> call each of the global rules in order until one of them returns non-nil, and that&#8217;s the response:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\"><span style=\"color: #aa9900; font-weight: bold;\">function<\/span> Game<span style=\"color: #66cc66;\">.<\/span>input<span style=\"color: #66cc66;\">&#40;<\/span>game<span style=\"color: #66cc66;\">,<\/span> command<span style=\"color: #66cc66;\">&#41;<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">for<\/span> _<span style=\"color: #66cc66;\">,<\/span> rule <span style=\"color: #aa9900; font-weight: bold;\">in<\/span> <span style=\"color: #0000aa;\">ipairs<\/span><span style=\"color: #66cc66;\">&#40;<\/span>game<span style=\"color: #66cc66;\">.<\/span><span style=\"color: #0000aa;\">globals<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">do<\/span>\n\t  <span style=\"color: #aa9900; font-weight: bold;\">local<\/span> message <span style=\"color: #66cc66;\">=<\/span> rule<span style=\"color: #66cc66;\">&#40;<\/span>game<span style=\"color: #66cc66;\">,<\/span> command<span style=\"color: #66cc66;\">&#41;<\/span>\n\t  <span style=\"color: #aa9900; font-weight: bold;\">if<\/span> message <span style=\"color: #aa9900; font-weight: bold;\">then<\/span>\n\t\t <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> message\n\t  <span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n&nbsp;\n   <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> <span style=\"color: #ff6666;\">&quot;Sorry, I didn't understand that&quot;<\/span>\n<span style=\"color: #aa9900; font-weight: bold;\">end<\/span><\/pre><\/div><\/div><\/div>\n\n<p>Now we can start this up, walk around the place, and get responses back:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\"><span style=\"color: #66cc66;\">&gt;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> game<span style=\"color: #66cc66;\">:<\/span>input<span style=\"color: #66cc66;\">&#123;<\/span> verb<span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff6666;\">&quot;walk&quot;<\/span><span style=\"color: #66cc66;\">,<\/span> subject<span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff6666;\">&quot;n&quot;<\/span> <span style=\"color: #66cc66;\">&#125;<\/span>\n<span style=\"color: #ff6666;\">&quot;Living Room\n&nbsp;\nA well-appointed but cluttered living room&quot;<\/span>\n&nbsp;\n<span style=\"color: #66cc66;\">&gt;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> game<span style=\"color: #66cc66;\">:<\/span>input<span style=\"color: #66cc66;\">&#123;<\/span> verb<span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff6666;\">&quot;walk&quot;<\/span><span style=\"color: #66cc66;\">,<\/span> subject<span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff6666;\">&quot;w&quot;<\/span> <span style=\"color: #66cc66;\">&#125;<\/span>\n<span style=\"color: #ff6666;\">&quot;There is no exit that way&quot;<\/span>\n&nbsp;\n<span style=\"color: #66cc66;\">&gt;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> game<span style=\"color: #66cc66;\">:<\/span>input<span style=\"color: #66cc66;\">&#123;<\/span> verb<span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff6666;\">&quot;walk&quot;<\/span><span style=\"color: #66cc66;\">,<\/span> subject<span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff6666;\">&quot;e&quot;<\/span> <span style=\"color: #66cc66;\">&#125;<\/span>\n<span style=\"color: #ff6666;\">&quot;Kitchen\n&nbsp;\nA simple kitchen.&quot;<\/span>\n&nbsp;\n<span style=\"color: #66cc66;\">&gt;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> game<span style=\"color: #66cc66;\">:<\/span>input<span style=\"color: #66cc66;\">&#123;<\/span> verb<span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff6666;\">&quot;walk&quot;<\/span><span style=\"color: #66cc66;\">,<\/span> subject<span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff6666;\">&quot;up&quot;<\/span> <span style=\"color: #66cc66;\">&#125;<\/span>\n<span style=\"color: #ff6666;\">&quot;There is no exit that way&quot;<\/span>\n&nbsp;\n<span style=\"color: #66cc66;\">&gt;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> game<span style=\"color: #66cc66;\">:<\/span>input<span style=\"color: #66cc66;\">&#123;<\/span> verb<span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff6666;\">&quot;yodel&quot;<\/span> <span style=\"color: #66cc66;\">&#125;<\/span>\n<span style=\"color: #ff6666;\">&quot;Sorry, I didn't understand that&quot;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>Now we&#8217;re just one parser and some props away from a system that can make interactive fiction. <a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/gist.github.com\/994437');\"  href=\"https:\/\/gist.github.com\/994437\">About two pages of code<\/a> so far, counting the descriptions of the rooms. Next time, we&#8217;ll finish it!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Like it or hate it, and there are plenty of reasons to do both, object-oriented programming is an idea that you can&#8217;t escape. Practically every language and every library uses it. Every CS curriculum teaches it. Every learn-to-program tutorial explains it. And so, people have some funny ideas about what it exactly is. If your [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/posts\/20"}],"collection":[{"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=20"}],"version-history":[{"count":0,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/posts\/20\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=20"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=20"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}