Game Help (pop)   (link) Game Help

Telling the Computer What to Do

Continue reading below...

 
HTML5=JavaScript required
 
Score 0
 
   
 
Choose a (hard to guess) name:
 

 
New User  
 
   

Computers are mostly programmed (told what to do) by typing text commands into a program. Older computer systems let users do this one line at a time (it's called a command line in Linux) and it seems easy to do. Command lines are really the hard way to program computers, so we will soon learn better ways, but this is a good way to start.

The command line lets you type any command the computer knows how to do, and then the computer will do exactly that one command. Try typing in the name of one of the arrow keys, like "left" or "right" (no quotes) then see what happens when you click the "Do It" button. See if you can guess what command will make the nibbler go up or down.

This gets tiresome pretty quick because it's faster to click the arrow buttons than it is to type commands into the command line. However, each command you type in gets saved in the list on the left (it's called a "script") when you click the "Do It" button, and if you click on the "Do All" button, it will do them all in order. Try it. It's still tiresome, but the beauty of doing it this way is that if you make a mistake, you can correct just your mistake without retyping the whole program again. We make a lot of mistakes, but mostly we only find out later. We'll get into more of that later. For now you can watch it on the screen and see what it did wrong, then fix your script.

Mostly when you type commands in to be added to your script, they go at the end. If you see a mistake, you can type the line number at the front of your line (with a space, like it shows on the listing) and the line will replace that line in the script. If you type only a line number and nothing else, it will delete that line (and renumber the rest to fill in the gap). To insert a line between two existing lines, use a decimal fraction like 2.5 to insert between lines 2 and 3, and it will push lines 3 and following down (and renumber them) to insert your new line 3.

The important idea here -- well it's pretty obvious -- is that the computer does your commands in a particular sequence, generally the order you typed them in (unless you made insertions or deletions). You control the sequence, because it usually matters what order you do things. It's like when you make a bowl of cereal, first you put the bowl on the table, then you pour the cereal out of the box into the bowl, and then you pour the milk over the cereal. It doesn't work if you get things out of order: if you pour the cereal out before you have the bowl in place, the cereal goes all over the table and onto the floor. If you pour the milk before the cereal is in the bowl, then when you pour the cereal in, it just floats on top, and the milk doesn't mix in properly.

Similarly, in our Chomp game, you need the nibbler to go up to get to the power pills, but you can't go up first because there's a wall in the way. Instead you must go left or right at least one place (but probably six places), then go up eleven, then four more in the first direction (left or right), in that order. In Chomp we enforce the sequence by putting the commands in a numbered list. Later (in Java) you will do it more implicitly.

But because I told the computer how to understand the commands you type into it (somebody must tell it how to do everything it knows how to do; that was me today, but next week that will be you.  I can also tell it to let you repeat the same command multiple times. Try "left 3" (with a space before the number) and see what it does. What commands would you type in to make it go all the way to the end of the row then turn and go all the way to the top? When you have multiple commands like this doing some complex sequence of things, it's called a "script" or program. Notice that clicking the Reset button a couple times erases your whole script, but you can Save it and later Restore it.

Click here to learn more about saving...

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 0, 2020 April 1