|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.
Think about your kitchen at home. Most kitchens in America have cupboards
to put things in, plates go in this cupboard, glasses go behind that door,
the silverware goes in this drawer under the counter, and the heavy pots go
over there by the stove. You can put anything you want in any of those places,
but after you have a pile of plates there, you can't put glasses or pots in,
they don't fit. Well, maybe they do, but the computer is more like a checker
board, 64 squares (we only use 32 of the squares for checkers), and once a
square has a black piece on it, you can't put a red piece there too. In chess
you can, but only by removing the black piece. Computers are like that.
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, like when the white queen on a chess board takes the black pawn, the pawn is gone and the queen is there in its place. You can take the plates out of the kitchen cabinet and put the pots there, but then the plates won't fit. Later, like when we changed A 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 April 1