Coding with Ken-Part 2 A MOO Programming Tutorial for Absolute Beginners Part 2 Hi! I'm Ken. I'm here to help you learn to program in MOO! This tutorial is for Absolute Beginners, no previous programming experience in any language is assumed!" Last time we met we built a Box of Donuts. This time we will elaborate the code for this object and learn to use the MOO Editor. Let's look again at our original program: @create $thing named Box of Donuts, donuts @property donuts.kinds {"tasty apple turnover", "yummy mooberry croissant", "shiny glazed donut"} @verb donuts:eat this @program donuts:eat donut_choice = this.kinds[random(3)]; player.location:announce_all(player.name," gobbles up a ",donut_choice,"."); Before we continue please check your inventory (inv) to be sure you have already created a Box of Donuts. If you haven't please review the first tutorial tape where you will find directions for creating it. Today we will make our donut object more dynamic. We started out with 3 donuts, but in our original program we could eat and eat and never empty the box. Lets fix that rather uncharacteristic behavior. We will change the first line of the program to read: donut_choice = this.kinds[random(length(this.kinds))]; Note that we have replaced the number 3 with 'length(this.kinds)'. This is because the number of donuts in the box will vary. 'length(this.kinds)' gives the number left. In order to effect this change we will use the MOO Editor. We are going to invoke the editor, list our original program, make a text substitution, compile the new code, and exit the Editor. Whew! I can't help you once you are 'in' the Editor so write down these commands: @edit donuts:eat list s/3/length(this.kinds)/1 compile q (stands for 'quit editor') The second command means 'substitute length(this.kinds) for 3 in line #1'. And 'q' quits the editor. Once you are in the editor you can see the available editor commands by typing 'look'. If you type 'help ' you can get help on a specific command. To see if you were successful, type: @list donuts:eat Do you see the changes? If you were unsuccessful just keep mucking with the editor until you get it to work. The editor is NOT easy to learn and mistakes are inevitable.. keep trying. It is worth while spending some time mucking around with the MOO Editor. It's a line editor and a bit quirky but if you program a lot you'll find yourself using it frequently. Our next step is to build some logic into the program: IF (there are donuts left in the box) THEN (let the player take one and eat it) ELSE (tell the player they're all gone). In MOO code the general form for such an IF/THEN/ELSE statement is: if (x is true) (do y) else (do z) endif Study the pattern in the following code: if (this.kinds) donut_choice = this.kinds[random(length(this.kinds))]; player.location:announce_all(player.name," gobbles up a ",donut_choice,".") ; else player.location:announce_all(player.name," reaches for a tasty donut, but the box is empty!"); endif In MOO code the expression (this.kinds) is considered to be TRUE if there are 1 or more items in the list, FALSE if the list is empty. Now we need to use the editor again to enter the new lines. When you renter the editor type 'list' and you will see the current program. We want to insert the line 'if (this.kinds)' at the very beginning. To do this we must set the 'insertion point' of the editor to be above line #1. You must type: ins ^1 to do this. Then enter the correct line prefixed by a quotation mark " , as if you were 'speaking' to the editor." Once again, if you had troubles try again. The Editor can be exasperating at times until you get used to it. Now that you have a feel for the editor figure out where to insert the rest of the new lines. If you want to set the insertion point after a line, type 'ins _6'. Don't forget to 'compile' and q(uit). If you have tried out the new verb you may have noticed that we can STILL eat all the donuts we want, the box never seems to get empty! Let's fix that now. We need to add the following line just after getting the donut_choice: this.kinds = setremove(this.kinds, donut_choice); This new line removes the eaten donut from the list of donut kinds - that donut is no longer in the box and the length of 'donuts.kinds' decreases by 1. Finally, we need a way to refill the box of donuts when it gets empty. Lets make a verb BAKE DONUTS. I will give you the complete code: Use the skills you have learned to practice entering the program: @verb donuts:bake this @program donuts:bake this.kinds = {"tasty apple turnover","yummy mooberry croissant", "shiny glazed donut"}; player.location:announce_all(player.name, " bakes up a new batch of fresh donuts!"); . If everything worked out ok, you should now have a refillable box of donuts. Try it out. Eat donuts until they're all gone. Then try baking up some more! Oooh don't get sick! Remember to save one of those donuts for me! Next time we will introduce some more fun features of the MOO language. Until then, HAPPY MOOING!"