|Game Help (pop)||(link) Game Help|
Continue reading below...
As you can see, building your own maze is tedious and not particularly fast. Once you have a maze built, you'd like to experiment with different ways to navigate that maze without messing up the steps to build it. You can see a little bit of that effect in the one-line command to build a spiral maze, which started off as a script (try "login Spiral" in your script). But what we really want to do is put some well-defined (and working) piece of code into a separate package by itself and sort of nail the lid shut on it. In programming languages this is called a subroutine or function (if it returns a computed value) or method (for obscure reasons we'll get into later). In Chomp it's just a script in a named LogIn that ends with the command "return". If you called it from a running script using the command login followed by reload, then when this called script comes to a return command, it will go back to the script that called it, and resume with the next line in that script.
This is probably easiest to see in a simple example. First convince yourself that you can LogIn as guest of user name "Subrtn" (or else create it yourself, but you might need to make up a different name), and that it contains the following script:
slowNotice that it has two return commands, the first controlled by an if command. Chomp scripts generally start running with all variables initially set to zero, so if we run it by clicking the DoAll button, (the first line slows it down so you can watch things happen), then the second line has no effect (variables A, B, and C are initially zero, so C is still zero), and the if in line 3 tests a zero value in D which skips line 4. Line 5 adds 4 to D so when the reload command in line 6 happens and the script restarts at the beginning and it gets to line 3, D is no longer zero, and it executes line 4, which returns to the line after the reload command, where line 7 adds 5 to variable E and eventually stops because it already returned to the line after the (only) scripted reload command.
Now it gets interesting. Go back to your own account and enter the following script:
let A=A+1(If you had to create your own copy of "Subrtn" be sure to use the name you gave it). Before you run this, try to think like a computer and see if you can guess what it will do. If you are not sure, run it and see. When your "main" script got to the reload command in line 4, the whole script was replaced by the script in "Subrtn" and it started running in line 1 there. Notice that variables A and B are no longer zero, so C gets a new value 3. Variable D is still zero, so it skips to line 5 to add 4 to D then (reloads and) reruns itself, now with 3 in C so it goes up to 6. D is no longer zero, so it takes the return command in line 4, which restores the original script you started and resumes at the last line there, setting F to 6 before stopping.
Did you follow all that? You can run it again as many times as you need, because it's really important that you understand what subroutines do. Make your own changes to the scripts and see what happens.
That was just an experiment. Now we'll do something useful. Let's say you designed an interesting maze saved away in a named script account -- I'll call it "Spiral" (and pretend you don't have a command by that name) -- and now you want to experiment with different scripts to walk that maze. All you need to do is put two lines at the front of each test script:
login Spiralthen follow that with whatever script you think will gobble up all the pills. Did you write a really smart script? Change just the first line to name a different maze and see how well your script does with that maze. See who can write the hardest maze for your friends to run their maze-solving script on, and who can write the best maze runner to navigate all the other mazes.
And then when you are really proud of your maze runner, try it on my "HiScore" maze
There's a lot more you can learn, but we'll give you a taste of one more topic before leaving Chomp behind.
I put a "Help" link at the top and bottom of each page to explain everything in more detail.
|Game Help (link)||(ToC)||Next >>|
Begin Programming Page 7, 2020 February 25