|Game Help (pop)||(link) Game Help|
Continue reading below...
So far we can only make the computer do exactly the
same thing over and over, but we also need to tell the computer to adapt
to changing circumstances. We need it to make decisions, like the game
on this page, did you click the left arrow button or the right? And then
make the nibbler go left or right respectively, and not the other. We need
two kinds of things to make that happen. One is a pair of commands for
sequence control (similar to repeat and next), and the
other is a place to put and manipulate data to base those decisions on.
We start with the data.
This is an important programming concept, like running your script in sequential order, and like iteration, we need to be able to set and use the value in a variable. In Chomp we use the command "let A=3" to tell the computer that the variable A is now to have the value (number) 3. If you type that command into the command line above, you will see two things happen. First, it will be added to your script, but you sort of expected that. Then, at the top edge of the game board near the left corner you see a tiny "/ 3" representing the value of the variable A. Try "let B=5" and it will add a 5 next to it. You can do things like that for all 26 variables, in any order, and their values will be lined up along the top edge in alphabetic order, A to Z, except it stops with the highest letter you actually gave a value to, and shows any variables you did not give a value to as zeros.
What happens if you now type in "let A=7"? The 3 changed to the new value 7 you put there. That's why we call them variables, because we can tell the computer to change the value, any time we want, any value we want (there are practical limits, which you are unlikely to bump into today). It is important to remember, this isn't mathematical equality, which is true for all time and in every circumstance, but assignment at this moment only, we are giving it this value. Later, like when we changed it from 3 to 7, we are assigning a different value at that time.
It is perfectly reasonable to tell the computer "let A=A+2" which
cannot mean "A is the same value as A+2" (which is never true) but only that
we are giving A a new value which is +2 greater than its previous value. Some
programming languages use a different symbol (not '=') to remind us that this
is assignment, not equality, but programmers are lazy, and the '=' symbol is
easier to type.
You can also multiply two numbers, like "let D=A*B" (we use
the asterisk, shift-8 on most keyboards, instead of an "x" for multiplying)
and get 35. You can combine operations and numbers like "let E=A+B*3" and
(because it multiplies before adding) you get 7+15 which is 22. If we want it to add
first, then multiply, we put parentheses around the part we want calculated
first, like this: "let F=(A+B)*3" and we get 36. An expression is
any value made up of one or more variables and/or plain numbers, combined
by the math operators and possibly using parentheses, to make a value which
the computer can calculate and put into a variable.
There is another gotcha with division that you need to be aware of: You cannot divide by zero. Most computers will crash your program if you try, but Chomp just quietly refuses. Try it (today, here in Chomp), but mostly you need to be careful not to do that. Even in Chomp you get wrong results. Don't go there.
I put a "Help" link at the top and bottom of each page to explain everything in more detail. See the link below? If it's blue (not purple) it means you have not seen that version yet. As you advance in your skill level, the Game Help page keeps up with more clutter in its hot-linked game image. Just so you know.
|Game Help (link)||(ToC)||Next >>|
Begin Programming Page 4, 2020 February 25