Game Help (pop)   (link) Game Help

Tell the Computer in Numbers

Continue reading below...

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

New User  

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.


Computers have millions -- more often billions, sometimes trillions -- of numbered places to put numbers. We call them variables because the number in that place can change from time to time, but only when you tell it to. Later on you will learn how those numbers can be used to represent the alphabet letters in text and the colored pixels in a picture and almost anything else we can imagine, but for now we'll just do numbers. Usually we give those numbered variables names so we can remember what they mean, but in Chomp we have exactly 26 places to put numbers, and they are named by the letters of the alphabet. But no matter what we imagine those numbers to mean, they are just numbers. Computers are all about numbers.

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.


Just setting variables to numbers is not very interesting. The power of variables is that you can do arithmetic on them and calculate new values to put into your variables. If you have studied algebra in school, then you already know about formulas, but if not, it's not really that hard. Each variable that has a number, you can add it to another variable (they all have the numbers we gave them, or else the zero values they received at Reset) to produce a new number that is the sum of those two numbers. Suppose you still have A=7 and B=5 (as above, or else put them back that way), then when you type in "let C=A+B" C will get the new value 12 (which is 5+7).

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.

Negative Numbers

What happens if you subtract? Obviously 7-5 is 2, but did you try it the other way? If you already know algebra, you expected to see -2, and that's what you got, so skip the rest of this paragraph. If I have five dollars and Steve gives me seven dollars, then I will have twelve dollars. If Steve has seven dollars and he gives me only five, then he still has two dollars, five less than he started with. But if I start out with five dollars and give seven dollars to Steve -- whoa, how can I give Steve more dollars than I have? This is math, it's make-believe -- then I have seven dollars less than I started with. We call that a negative two, which means if I found two dollars on the street, I would end up with nothing at all. Or maybe you can imagine I borrowed the two dollars from Bill to give seven to Steve, and after I found the twosie on the street, I must repay Bill, and then I still have an empty wallet. Negative dollars is money you borrowed, and if you earned that much and paid back what you borrowed, you'd be broke. Negative numbers are just value you didn't have but spent anyway, and you must pay them back before you are even. If that's too hard to understand today, don't worry about it, we'll mostly work with positive (not negative) numbers anyway.


In Chomp we work only with integer (whole number) values. If I have $16 and three friends, I can split it four ways and give each of them exactly $4 and keep the last $4 for myself. If I have $17 and divide it four ways, there are no quarters, we just throw the extra dollar away. Integer arithmetic is like that. Try "let G=17/4" (we use the slant character, usually an unshifted question mark next to the shift key on most American keyboards). Notice you get the same result an when you divide 16 or 19 by 4, the remainder is discarded, and we keep only the quotient.

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.

Next: Conditionals...

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