Classic Computer Magazine Archive COMPUTE! ISSUE 48 / MAY 1984 / PAGE 140

THE BEGINNER'S PAGE

Richard Mansfield, Senior Editor

A Random Leap

One of the enjoyable things you can do with a computer is simulate real events: things which might be too dangerous, too expensive, or too time-consuming to try in real life. The Air Force and some commercial airlines use a flight simulator so true-to-life that it can serve for all but the most advanced pilot training.

We don't have enough RAM memory, or the computation speed, or the ultrahigh resolution screens necessary to create a flight simulation of breathtaking realism. But we can try a simple simulation and get a feel for how they are programmed. The basis of the simulation will be accidental, unpredictable events created by the RND (random) command in BASIC.

Lurching Across A Bridge

Imagine a frog, lurching across a bridge. Every time he leaps, you don't know if it will be to the left or to the right. He doesn't know either. The one thing you can count on is that he will never leap straight ahead.

There are three possibilities in this game. He will either fall off the left or right side of the bridge, or safely reach the other side of the river. For this simulation, we're going to assume that the bridge is as wide as your computer screen and that the frog starts his journey midway between the left and right sides. That gives him a fair chance to make it across.

By setting up this simulation, we'll learn how to make use of the RND command as well as a way to animate characters on the screen. Let's look at the program line by line, to see what each BASIC command contributes to the overall effect. (Atari computers don't have a TAB command, so the animation technique discussed below will not work on them.)

First, we've got to define the size of the bridge, its width. Leave line 100 as it is if you have a Commodore 64 or any other computer which allows 40 characters per screen line. If you have a VIC, you should change line 100 to read: COLS = 22. The VIC has 22 characters per screen line. If you have a TI, change it to: COLS = 32.

The variable Y in line 110 is going to signify the position of the frog each time it leaps. If Y is raised to a higher number, the frog will appear further to the right on the screen (and be nearer the right side of the "bridge"). If Y goes down, if something is subtracted from Y, the frog moves left. At the start of the game, though, we want to put the frog in the middle between the left and right sides of the bridge so we divide COLS by 2. If you've got a 40-column screen, Y starts off equaling 20. That means that the frog is 20 from the right edge and 20 from the left—smack in the middle.

Rounding Numbers

The variable X in line 130 will tell us whether the frog should leap to the right or the left each time he leaps. This is the only complicated-looking line in the program, but it contains an important trick: the INT command. It "rounds off" a decimal number. INT (12.3) becomes 12. INT (12.7) becomes 12. Wait a minute. That's not rounding off as we usually think of it. 12.7 should become 13 since .7 is closer to 13 than to 12.

In fact, INT merely throws away anything to the right of the decimal point. This isn't true rounding. That's why we need to add the + .5 in line 120. By adding .5, we force a number to be rounded correctly by INT. 12.7 + .5 would be 13.2 and INT (13.2) would give us the right answer: 13. Likewise, 12.3 + .5 would be 12.8 and INT (12.8) would give us the correctly rounded answer: 12.

It's not important to remember why you need to add .5 to any number you want rounded by INT; just remember to do it. In line 120 we're not rounding off 12 or 13, all we want is an answer that tells us to go in one of two directions, to go either left or right. This is like tossing a coin, you get heads or tails. So here X will be either a 0 or a 1 after INT gets through rounding off RND(0). But what does RND(0) do for us? It creates a random number. But, by itself, the random number is a decimal fraction between 0 and 1. Try this:

10 PRINT RND(0):GOTO 10

When you RUN this, you'll see a series of decimal fractions, all kinds of different numbers. How would you get higher random numbers? Just multiply RND(0) by something. Try: PRINT RND(0) * 10. If you just want whole numbers (called integers), use INT.

Anyway, in our frog simulation we don't need these higher random numbers. If X becomes a 0 in line 120, we move the frog to the left (in line 160). If X becomes a 1 in line 120, we move the frog to the right (in line 140). Line 130 is the test to see which number is in X.

Notice that we don't need to write a line like: IF X = l THEN 140. You could write that test and put it in line 135 if you wanted to. It wouldn't do any harm. But you don't need to. The computer will go to line 140 all by itself if X is anything other than a 0 when it's tested in line 130. The computer always performs each action in the order listed unless you force it not to with a GOTO, IF, or GOSUB command. If it doesn't come across one of those commands, it will go from line 140 to 150 to 160 and on up the list in simple line-number order.

Also on line 120 is another counter, the variable C. It will keep track of the total number of leaps the frog has made (either left or right). This lets us know how far he got before he fell off. It also sometimes shows that he's won the game. If he manages to leap a certain distance without falling, he's crossed the bridge.

But back to our simulation. After lines 130–160 make an adjustment to variable Y (our "position-of-the-frog" counter) we come to a series of tests in lines 170–190. Each of these tests will end the program in a different way. In 170, if the frog position is greater than (>) the total number of columns, he has fallen off the right side. In 180, if his position is less than 1, he has fallen off the left side. And, finally, in line 190, if he has taken more leaps than the width of the bridge, he made it across. You can change this line if you want to make it harder for him to cross the bridge. Just replace COLS with a higher number.

Line 200 prints the frog symbol on the screen to show us his position. The TAB command is just like a TAB key on a typewriter: It moves over a certain number of spaces from the left side of the screen. In this case, the number of spaces is controlled by the position variable Y.

Finally, to slow the frog down a bit, we put in line 210. This is often called a delay loop or a donothing loop because it simply takes up some time and serves no other purpose. Here we're asking the computer to count from 1 to 10 before going back down to line 120 and figuring out the frog's next leap.

100 COLS = 40 : REM PUT YOUR SCREEN SIZE HERE
                                     : rem 232
110 Y = COLS/2                       : rem 186
120 X = INT(RND(0) + .5) : C = C + 1 : rem 176
130 IFX = 0THEN160                   : rem 174
140 Y = Y + 3                        : rem 226
150 GOTO170                          : rem 103
160 Y = Y - 3                        : rem 230
170 IFY>COLSTHENPRINT" >>>FROG FELL OFF RIGHT SIDE. IN"C"LEAPS." : END                                   : rem 120
180 IFY<1THENPRINT" <<<FROG FELL OFF LEFT SIDE. IN"C"LEAPS." : END                                     : rem 30
190 IFC>COLSTHENPRINT"FROG SAFELY CROSSED THE BRIDGE" : END         : rem 160
200 PRINTTAB(Y)"*"       : rem 14
210 FORT = 1TO10 : NEXTT             : rem 13
220 GOTO120                          : rem 96