AFTER THE MANUALS ...

You may be wondering what on Earth to do with your ZX-80 or ZX-81
once you have worked through the manuals. Tim Hartnell obligingly
shows you the way into new programming territory with an explanation
and example listings of random-number generation - the key to writing
games programs.


PLUG IN YOUR machine and position the cursor - that is the white K on
a little black square - in the left-hand corner of your screen, and
input the following, which generates a series of random numbers. You
will be using random numbers time and time again in games:

ZX-80
10 PRINT RND(10);" ";
20 GOTO 10
ZX-81
10 PRINT INT(RND*10)+1;" ";
20 GOTO 10

These programs mill cause the screen to fill with random numbers
between one and 10. They are of absolutely no use at all as they are,
but become invaluable when used as part of a game.

The following program uses the random number generator to produce
numbers between one and six in the game of Russian Roulette. The
principle of the game is simple. You have a pistol with six chambers,
only one of which contains a bullet. You point the pistol at your
head, pull the trigger, and hear either a bang or a click. Input the
program, RUN it a few times:

10 PRINT "RUSSIAN ROULETTE"
20 PRINT "WHAT IS YOUR NAME?"
30 INPUT A$
40 PRINT
50 PRINT "DO YOU WANT TO PLAY,";A$;"?"
60 INPUT B$
70 CLS
80 IF B$="NO" THEN GOTO 270
90 LET J=0
100 PRINT "PRESS NEWLINE TO FIRE"
110 INPUT C$
120 CLS
130 LET J=J+1
140 LET G=RND(6)
    This version is for the ZX-80. If you have a ZX-81, change the line to:
    LET G=INT(RND*6)+1
150 PRINT A$;",";10-J;" SHOTS TO GO"
160 PRINT
170 IF G<6 THEN PRINT "CLICK"
180 PRINT
190 IF G=6 THEN GOTO 220
200 IF J=10 THEN GOTO 250
210 GOTO 100
220 CLS
230 PRINT "BANG...";
240 GOTO 230
250 PRINT "YOU HAVE SURVIVED,";A$
260 GOTO 250
270 CLS
280 PRINT "CHICKEN...";
290 GOTO 280

There is an extraordinary number of things you can learn from this
simple program:
	10 PRINT "RUSSIAN ROULETTE"

Firstly, you see that every line begins with a line number. On a
Sinclair computer, the line numbers can be any from 1 to 9999, but
you will find it best if you work in multitudes [sic. JimG] of 10 or
so because it gives you space to add extra lines between any you
already have in the program.

Extra lines sort themselves automatically into the correct order. The
computer always processes each line number in order from smallest to
largest - unless it is told during the course of a program to go to
some other line.

Next we have the word PRINT - probably the most-used command in the
computer language BASIC, which is what your ZX-80 and ZX-81 use. You
follow the command PRINT with what you want printed in quotation
marks. In this case, it is the words Russian Roulette.

Try adding a comma just before the first set of quotation marks so
the line reads:
	10 PRINT, "RUSSIAN ROULETTE"

Run the program again and you will find the computer moves the words
across the screen. The use of a comma in this way is ideal for
setting out print statements in columns ideal for printing boring
tables of figures.

The line 20 asked your name, and then stopped when it reached line 30
- INPUT A$. This A$ - a letter followed by a dollar sign - is called a
string in BASIC and can be made equal to any combination of letters,
characters and numbers. We shall stay with letters for the time being.

The computer stopped and waited for you to input a string. After you
typed in your name, and pressed NEWLINE so the computer would accept
it, the screen cleared. The CLS line means "Clear the screen" but the
computer still remembered what A$ had been made equal to, and used it
to print your name next time A$ appeared in the program.

Next, the computer asked you if you wanted to play - line 50 - and
accepted a second string - B$ - in line 60 as your answer. Now, the
ZX-80 and ZX-81, like all computers, can make decisions and act on
them. In this case, in line 60, it looks at your answer, B$, to the
question and if the answer is No - that is, if B$ equals "NO" in line
80 - it directs control to line 270, which clears the screen.

Control then passes to the next line, 280, where the word "CHICKEN"
is printed. Line 290 repeatedly sends control back to 280 until the
screen is full and the program stops. You will see that the word
"CHICKEN" forms an attractive pattern, and if you want something to
develop this further use a perpetual loop and a few graphical symbols
in a PRINT statement, to create patterns to fill the screen.

In Russian Roulette, if you do not answer "NO" when asked if you want
to play, the computer moves along to line 90 where the variable J is
assigned [to] the value zero. In Sinclair BASIC, any letter or
combination of letters and/or numbers starting with a letter can be
assigned [to] a value. So, line 90 could say, for example:
	LET SUM=0 or LET PO4=8

You follow the instruction printed by line 100 and the screen clears
yet again - line 120. Line 130 adds one to the value of J so J becomes
equal to one. Next time, it will add one again, so J will equal two
and so on.

Line 140 is our old friend the random number generator. Line 150 tells
you how many shots from 10 you have left. Note that in Sinclair
BASIC, you can combine strings, PRINT statements and computation in a
single line. The ZX-80 or ZX-81 works out what each part of the line
means before it prints it.

Line 160 prints a blank line and then line 170 makes another
decision. This kind of statement is known as an IF/THEN statement
because is takes the form IF some condition exists THEN do something
- IF the house is on fire THEN scream. The condition can be almost
anything from:
	IF X=96 to IF A$="FROG"
to complicated conditions like:
	IF A-B>2*Z-Q/2 THEN

Anyway, this IF/THEN statement checks to make sure the random number
generated is less than six, and if it is, it prints "CLICK". If it is
not, the computer continues to scan the program line by line until it
finds something it can do.

If G is equal to six - line 190 - it sends control to line 220 which
uses a similar idea to the one we discussed for "CHICKEN" to print
the fatal word "BANG". Assuming that G is less than six, the computer
will print "CLICK" in line 170, print a blank line in 180, ignore
line 190 and then arrive at line 200.

When it reaches there, it will check to see what value has been
assigned to J. If it equals 10, you have won the game, so the
computer sends control to line 250 to tell you. If J does not equal
10, the computer ignores line 200 and then finds, at line 210, a
command it can obey, and sends all the action back to line 100.

Once you have killed vourself a number of times with the game, add
and/or change a few lines in the program to allow the player to have
a new game if they survive the first version without having to
return [the] command mode. There is an enormous number of ways the
modification can be made. Probably the simplest way is to add the
following:
	255 PRINT "DO YOU WANT ANOTHER GO?"
and change line 260 to read GOTO 60. By doing this, you use the
routine of lines 60, 70 and 80 twice. You may well have done it
differently. It does not matter what changes you made, as long as it
achieves the desired end.

There is another way we could have produced that same effect - by use
of a subroutine. Whenever a computer encounters a GOSUB - GO to
SUBroutine - command, it goes to the line specified and follows on
until it meets the command RETURN. The computer then returns to the
line after the GOSUB command. Make the following changes to your
Russian Roulette program and this should be clearer:

Change line 60 and line 260 to GOSUB 300 and delete lines 70 and 80.
Now add the following which is the subroutine:
	300 INPUT B$
	310 CLS
	320 IF B$="NO" THEN GOTO 270 [bad programming! JimG]
	330 RETURN

This time, when you run the program, the computer will be sent to the
subroutine when it reaches lines 60 or 260. Here is a program of
stunning complexity which relies heavily on repeatedly going to a
subroutine:

10 LET A$="KALKI, THE MIND-READER SAYS"
20 GOSUB 340
30 PRINT "WHO OWNS THIS COMPUTER?"
40 INPUT B$
50 PRINT "PRESS NEWLINE AFTER EACH STEP"
60 GOSUB 320
70 PRINT "THINK OF A NUMBER"
80 GOSUB 320
90 PRINT "DOUBLE YOUR NUMBER, ADD 4"
100 GOSUB 320
110 PRINT "DIVIDE BY 2, THEN ADD 6"
120 GOSUB 320
130 PRINT "SUBTRACT THE NUMBER YOU"
140 PRINT "FIRST THOUGHT OF"
150 GOSUB 320
160 PRINT "SUBTRACT 3, THEN"
170 PRINT "MULTIPLY BY 5"
180 GOSUB 320
190 PRINT "SUBTRACT 3 AGAIN"
200 PRINT "AND DIVIDE BY 2"
210 GOSUB 320
220 PRINT "WRITE DOWN YOUR ANSWER"
230 GOSUB 320
240 PRINT "HOW MUCH MONEY DO YOU"
250 PRINT "HAVE IN YOUR POCKET (IN P)?"
260 INPUT A
270 GOSUB 330 (Note: Not 320)
280 PRINT "THE NUMBER WRITTEN DOWN IS 11"
290 PRINT "WHEN YOU LEAVE, PLEASE"
300 PRINT "GIVE"; B$;"";A/100;" AS A DONATION"
310 STOP
320 INPUT C$
330 CLS
340 FOR J=1 TO 5
350 PRINT
360 NEXT J
370 PRINT A$
380 RETURN

Many of the features we have discussed in connection with Russian
Roulette apply to Kalki, The Mind-reader. After running the program
a few times, you can change the PRINT statements to create a totally
new game. Once you are tired of that, devise your own program to
create designs using subroutines. We shall be looking at more
complicated programming techniques for the ZX-80 and ZX-81 in the
next issue of Your Computer.