Spectrum Helpline Issue 6 Contents Hardware World

First Impressions



  10 BORDER 1: PAPER 6: CLS : PA
PER 6
  15 PRINT AT 3,13;"NIM"
  20 PRINT AT 5,5;"This is a gam
e for two"
  22 PRINT "players,or one playe
r and the"
  24 PRINT "computer"
  30 PRINT AT 8,5;"The players t
aketurns to"
  32 PRINT "subtract any number 
 <=a"
  34 PRINT "predetermined value 
from 100.The"
  36 PRINT "winner is the one to
 reach 0."
  40 PRINT AT 14,0;"What is your
 name?"
  50 INPUT a$
  60 CLS
  65 PRINT "Who are you going to
 play?"
  67 PRINT "(computer=c)"
  70 INPUT b$
  72 INPUT "Who is going to play
 first?";c$
  75 CLS
  90 LET g=0
  95 IF c$=b$ THEN LET g=g+1
 100 LET j=100
 140 INPUT "Max.no.to be subtrac
ted?";b
 145 PRINT AT 3,3;"Max.=";b
 150 PRINT AT 11,14;j
 170 IF g=0 THEN PRINT AT 19,2;a
$;"s turn": LET g=g+1: GO TO 200
 172 IF g=1 AND b$<>"c" THEN PRI
NT AT 19,2;b$;"s turn": LET g=g-
1: GO TO 200
 175 IF g=1 AND b$="c" THEN LET
c=j-INT (j/(b+1))*(b+1)
 180 IF c=0 THEN LET c=INT (RND*
b+1)
 190 LET a=c: LET g=g-1: PRINT A
T 19,2:"Computers turn=";c: PAUS
E 150: GO TO 210
 200 INPUT a
 210 PRINT AT 19,2;"(30 spaces)"
 240 LET a=INT (a)
 250 IF a=0 OR a>j OR a>b THEN P
RINT AT 19,2;"Try again(20 space
s)" : GO TO 200
 300 LET j=j-a
 390 PRINT AT 11,14;"(3 spaces)"
 400 PRINT AT 11,15;j
 450 IF j=0 THEN GO TO 1000
 500 GO TO 170
1000 CLS
1010 IF g=1 THEN PRINT AT 11,3;a
$;"is the winner"
1020 IF g=0 AND b$="c" THEN PRIN
T AT 11,3;"The computer is the w
inner"
1030 IF g=0 AND b$<>"c" THEN PRI
NT AT 11,3;b$;"is the winner"

Alyson Bailey takes her first steps with personal computers

Learning to program the Spectrum way

HOW CLEVER are computers, I wondered, as I unpacked my first, a Spectrum, two weeks ago. Since then my assessment of this tiny box, no bigger than an average-sized book, has varied through "very bright" to "doesn't think for itself" and "shows great potential".

The manuals which accompanied the new toy were excellent and we soon had a variety of combinations of BORDER and PAPER flashing gaily on and off the TV screen, accompanied by notes of random duration. A clock with a moving hand, graphs and a design produced with circles of different diameters followed in quick succession. If this was computing, it was tremendous fun.

It was the youngest member of the family, aged 14, who noticed the first deficiency. His friends, he said, had computers which played games and ours did not. So far as we knew, no software was available for the Spectrum and if we wanted games we would have to type in the programs ourselves. Armed with a selection of computer magazines we set to work enthusiastically but our enthusiasm soon began to wane.

The print for some of the programs was too faint to read. Others, though clearly printed, did not work. Our typing errors did not help; the computer was amazingly fussy about punctuation and that had never been one of our strong points.

Although admitting that some of the failures were our fault, others, we were convinced, were not. The computer refused some words like RND(99) and other programs containing words like CHR and PEEK seemed doomed to failure. PLOT resulted in miniature graphs in the bottom left-hand corner of the screen.

What was wrong? Did the computer have a language and perhaps even a dialect all of its own which prevented it communicating with even its closest relatives, the ZX-80 and ZX-81 ? Was this ASCII - whatever ASCII is? At long last we found a program which worked - Luca from Sinclair User, June, 1982. Junior added sound and colour and altered the graphics and what a difference those additions made. Who complained that the sound was too quiet? Imagine all those beeps played at the same volume as pop music.

Success was followed by more failures, despite avoiding programs containing "foreign" words. Had we over-estimated the powers of understanding of our computer yet again? We studied the listings. Surely there were mistakes. Should the letter in one program not be the number 5, the INT in another be replaced by =; was CLS missing in another?

Several seemed to have whole lines missing while others had superfluous ones, no doubt relics of bygone versions. Our computer could not cope with typing errors. Those errors were human and beyond the mental capacity of the Spectrum. With errors corrected, those programs produced several more working games. Why, we asked, do producers of magazines over-estimate the ability of computers? Surely experience should have taught them that computers are not superhuman?

Frustrated by our failures but elated by our ever-improving ability to understand our computer, we decided to try to write our own program. We agreed that the concept of our first attempt must be simple. Sinclair User provided the basic idea, the game Nim. It is a game for two players who take turns to take any number of matches, up to a predetermined value, from a pile of 100. The winner is the person who takes the last match. We typed in the basic game:

100 LET j=100
150 PRINT j
200 INPUT a
300 LET j=100-a
400 PRINT j
500 GO TO 200

and decided to take up to 11 matches and set our program to RUN. It worked. "That is a bit boring", commented junior, and I must admit it fell far short of Space Invaders.

"Wait", I said, "it will get better". Colour impressed him, so we added that - line 10 - and moved the numbers to the centre of the screen - edit lines 150 and 400 - remembering to clear one before the next appeared - line 390.

Running the program produced only a marginally better response. We added captions, lines 15, 20, 22 - without "one player and the" - 30, 32, 34, 36; some questions and demands for answers - lines 40, 50, 60, 65, 70, 75 - let the computer decide whose turn was next - lines 90, 170, 172, without "AND b$<>"c"), 210, edit 500 and gave a choice of number to be subtracted - lines 140, 145.

Then the game was much more interesting but cheating began to rear its head and had to be eliminated - lines 240, 250. After much thought, I realised how to win and decided to let the computer take my turn if so requested - add the remainder of lines 22 and 172 and add lines 24, 67, 175, 190. We also added lines 450, 1000, 1010, 1020, 1030 to announce the winner. Interest soared.

It was at that point that the computer showed human characteristics. It refused to play if it knew it could be beaten. Addition of another line - 180 - forced it to continue playing. Then it could not prevent me winning, provided my mathematics did not let me down - and frequently they did. Junior complained that the computer won only because it had second turn. He was given the choice of first or second turn - lines 72, 95 - but still he lost and I won. No-one else has beaten the computer and if they do I will alter the program so that even I cannot win.

All that leads me to wonder who really is my opponent when I play games against a computer. Is the computer as intelligent as it appears or am I playing some far-off programmer I have never met?

Put this program into your computer and see if you can beat it or me. If you can, try to alter the program to make the computer win every time. If you succeed you are the true winner. The computer can achieve no more than the programmer - or can it? Are you as quick at mathematics? I am not.



Spectrum Helpline Issue 6 Contents Hardware World

Spectrum User
September 1982