Sunshine Books Programming for education on the Commodore 64 a

Page 1
programming
for education
on the commodore 64
a handbook for primary education
john scriven and patrick hall
Page 2
Page 3
programming
for education
on the commodore 64
john scriven and patrick hall
Page 4
First published 1984 by: Sunshine Books (an imprint of Scot Press Ltd.)
12-13 Little Newport Street,
London WC2R 3LD
Copyright © Patrick Hall and John Scriven, 1984
All rights reserved. No part o f this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, elec
tronic, mechanical, photocopying, recording and/or otherwise, without the prior written permission o f the Publishers.
British Library Cataloguing in Publication Data
Scriven, John
Programming for education on the Commodore 64.
1. Education, Elementary
2. Commodore 64 (Computer) I. Title II. Hall, Patrick
372. V 67 LB 1028.43
-----
Data processing
ISBN 0 946408 27 0
Cover design by Grad Graphic Design Ltd. Illustration by Stuart Hughes. Typeset and printed in England by Commercial Colour Press, London E7.
u
Page 5
CONTENTS
Page
Program Notes vii
Introduction ix
1 Starting with Maths 1
2 Small is Beautiful... 27
3 Words, Words, Words 49
4 Making Friends with your Keyboard 69
5 Story Time 81
6 Logically Speaking 93
7 The Real Thing
107
iii
Page 6
Page 7
Contents in detail
CHAPTER 1 Starting with Maths
Starting off the computer as calculator choices and loops pro grams and randomness Table 1: a table test improvements Table 2: introducing time using graphics and sound looking for patterns Table Patterns: changing the display Bingo: a game setting for a tradi
tional idea repetitive learning moving graphics to add interest
Factor: a program to teach factorisation.
CHAPTER 2 Small is Beautiful...
Short programs and efficiency Missing: find the numbers ordering numbers Series: maths as an introduction to strategy simplicity in program structure Number: a program to find a hidden number using graphics to brighten up simple ideas Firework: a program to test general numerical skills the problems involved in learning fractions Fraction: a program to help with numerators and denominators.
CHAPTER 3 Words, Words, Words
Handling words on a computer sorting things Alphasort: a program to sort words into alphabetical order numbers in strings Simon: a game to improve memory diversity of subjects in primary schools problems in spelling different approaches context clues Hangman: a multi-purpose spelling program research skills Anagram: clues
from word order.
CHAPTER 4
Making Friends with your Keyboard
Tackling entry errors menus making programs foolproof moving the character set BigL: writing large on the screen Keyboard: learning the keys Masking: changing binary numbers.
v
Page 8
Programming for Education on the Commodore 64
CHAPTER 5
Story Time
Creativity artificial intelligence words at random Story: a program to help with creative writing and adjective use interactive use of computers Gosh: more stories.
CHAPTER 6
Logically Speaking
Logical reasoning problem solving Treasure: finding pirates gold coordinates using sound and sprites Sonar: find the submarine.
CHAPTER 7
The Real Thing
Unique role of computer in simulation the appeal of computer graphics simulation versus immediate experience the realm of simulations Lock: an excursion on the canal Power: electricity generation inter active simulations elementary statistics Coin: probability of throwing a head Cascade: Pascals triangle.
vi
Page 9
Program Notes
All Commodore computers use special characters to control various func tions on the screen. For instance, pressing the [CLR/HOME] key and
[SHIFT] will clear the screen. If those keys are pressed after a double quote
mark, an inverse heart will appear in the listing.
Although this is often a quick way to control print colours, inverse char acters and cursor control, there are usually less confusing ways of achiev ing the same effect. To avoid confusion in the listings, we have used these codes as infrequently as possible. The exception is the use of SHIFTed CLR/HOME to clear the screen as mentioned above.
To make the listings clearer, where this character appears, it has been
substituted by [SHIFT/CLR/HOME].
Note: Please be careful when entering programs listed in this book. The quality of printing from a computer printer means that some characters
can look very similar commas, in particular, can look very like full stops.
In the program listings, } appears as
Vll
Page 10
Page 11
Introduction
The aim of this book was not originally to teach you BASIC. We had hoped to let it contain educational programs which we ourselves had found useful, and merely write about their use, as we have with the other books in this series. As we were collecting material, however, we began to realise that many people who used this book might have had little experience of computers. For those that are new to computers, we have spent more time introducing the early programs.
We hope to lead you step by step through the initial stages to simple programming skills. These are combined to produce short programs that show how the Commodore 64 can be used at home or at school to make learning more interesting, while also discovering something about the computer itself.
The 64 is a very advanced microcomputer and has some outstanding features. Unfortunately, these are not accessible from BASIC without addressing the memory directly, and this makes some aspects of the 64 more obscure than they need to be.
We will attempt to show you that these drawbacks can be overcome, and that you will be able to write interesting educational programs once you have understood a little more about how the 64 operates. If something is not clear straight away, come back to it at a later stage and with luck you will understand it.
Above all, this is a book about doing. No one ever learned about computing simply by reading a book, so do try out the examples and alter things as you find necessary.
If you have some experience with computers, you can skip some of the introductory material and dive into the meatier sections. We hope that you wont be content simply to copy out the listings, but will modify and adapt
the programs and routines to suit your own needs.
All the programs have been tried and tested, and are printed from the actual listings, so they should work first time. If they dont, its probably
due to a tiny typing error thats escaped your notice. Check the listing
against the book, as even a missing semicolon can stop the program. If, in spite of this, you still find something doesnt work as expected, wed like to know about it!
Ultimately, we hope that you will continue to enjoy your computing and
that this book will help you achieve your aims.
IX
Page 12
Page 13
CHAPTER 1
Starting with Maths
Starting off the computer as calculator choices and loops
programs and randomness Table 1: a table test improvements
Table 2: introducing time using graphics and sound looking
for patterns Table Patterns: changing the display Bingo: a game
setting for a traditional idea repetitive learning moving graphics to add interest Factor: a program to teach factorisation.
Ten years ago, it was unusual to find calculators in schools, let alone computers. The advent of the home computer, and computers in schools, brings with it the responsibility to make sure a) that the users are familiar with them and b) that they are used wisely.
In this chapter we shall be looking at how computers use information, and how they can display this. If you have some knowledge of computers, then you can skip over the early sections. If you are coming to the 64 as your first machine, then it should clear up some of the mysteries of how computers can make simple decisions and appear to act intelligently.
If you have used a calculator, then you know that you enter data, in the
form of numbers, along with operators, like plus or minus, to tell the calcu
lator what to do with those numbers. For instance,
2+ 2 - 1=
At each stage of the calculation, the display shows the last number entered
or the result at that stage. You can use the 64 as a calculator in the same way. Instead of calculator buttons, you can communicate with the 64 via the keyboard. If you enter
2 + 2 -1 [RETURN]
it will calculate the result. (The [RETURN] key can be seen as sending the information you have entered inside the computer.) The problem is that, whereas you see the result automatically on a calculator, the 64 has to be told to display the answer. To speak to the computer, you have to use the language that it understands. The 64 uses the language BASIC, which is a
1
Page 14
Programming for Education on the Commodore 64
fairly small set of words that are quite close to normal English. These words are entered via the keyboard, just as on a normal typewriter.
To make the 64 print something, you must use the PRINT statement. If
you enter
PRINT 2 + 2 -1 [RETURN]
then the result, 3, will appear on the screen.
PRINT can be used to write words on the screen in just the same way. If
you enter
PRINT YOGI BEAR [RETURN]
then the words YOGI BEAR will appear on the screen.
This is using the computer in immediate mode, which is handy for instant calculations, but is hardly using it in the manner for which it was intended.
The most useful point about computers is that they can store the instruc tions given to them and follow them in the correct order.
So that they can follow the instructions in the correct order, the instruc tions are given line numbers. First make sure theres nothing already in the 64s memory by typing NEW [RETURN]. To perform the simple example earlier, you could use the following:
10 PRINT TWO PLUS TWO TAKE ONE EQUALS ; [RETURN]
20 PRINT2 +2 - 1 [RETURN]
If you enter the lines as they are written, the program lines are stored in the computers memory.
{Important: Make sure that you enter the lines exactly as they are written. Humans can understand things even when there are mistakes, but computers are unforgiving and even putting a letter 1 instead of a figure 1, or a letter O instead of a figure 0, will prevent program instructions from working.)
To execute the instructions, the command RUN must be entered and the
[RETURN] key pressed. The computer will obey the instructions in each line in turn, that is, print the sentence in quotes in line 10 followed by the answer to the calculation in line 20. As there are no more lines, the program will end there, and pass control back to you. (From now on remember to press the [RETURN] key at the end of program lines and after commands. There wont be any more reminders!)
Programs, then, are simply lists of instructions that the computer obeys in a particular order. It doesnt matter in which order you enter the lines, the computer sorts them out for you. So if you now enter
2
Page 15
Chapter 1 Starting with Maths
5 PRINT THIS IS MY PROGRAM
it will be executed first. RUN the program to test this.
If you use the command LIST, the program you have been entering will appear on the screen in the correct order. To get rid of one particular line, enter just the line number. So, entering
5 [RETURN]
followed by LIST, will show you that line 5 has indeed been deleted.
To delete a whole program from memory, use the command NEW. This
clears the complete BASIC program, so dont use it unless youre sure
thats what you really want.
Try it now and check that your program really has disappeared by LIST-
ing it.
Inputs and variables
As well as entering information in program lines, you can enter it via the keyboard during the course of a program. The 64 stores this information in the form of variables. This is just a way of saying that letters can stand for various values, like in algebra. If you say X + 6 = 10, then X must have the value 4. In the language BASIC, you can assign a value to a letter by simply saying X = 4.
Enter the following
X = 4
Now, as a direct statement, enter
PRINT X
The value 4 will be printed on the screen.
X is known as a variable. If the variable is going to stand for something
in particular, then its a good idea to use a longer name, for instance, the variable to hold a year could be YEAR = 1984.
The 64 will only recognise the first two letters of the variable, so YEAR
and YELLOW appear the same to the computer. If you enter
YEAR =1984
then
YELLOW = 6
Page 16
Programming for Education on the Commodore 64
then
PRINT YEAR
the result 6 will appear, so be careful how you use variable names.
If you want to store a string of characters, ie letters, numbers, graphic characters, or anything else on the keyboard, you have to use what is called
a string variable. This is just like a numeric variable except that it ends in a
$.
Enter this:
A$ = CBM 64
PRINT A$
The string variable A$ now contains the string of characters CBM 64, so when you tell it to print out A$, that is what is printed.
Just as it is a good idea to use meaningful names for numeric variables, string variables too are easier to understand when they mean something. For example, NAMES is more meaningful than N$.
Remember that the 64 will confuse NAMES with NAILS, as it only recog nises the first two characters. It is also important that the variable name should not contain a BASIC keyword. This means that FORT is not allowed it contains the BASIC word FOR. The variable BRIGHTONS is not allowed as it contains the word TO.
As was mentioned earlier, variables are useful when you want to enter
information during the course of a program.
10 PRINT ENTER YOUR NAME
20 INPUT NAMES
30 PRINT HELLO, NAME$
40 PRINT ENTER YOUR AGE
50 INPUT AGE
60 PRINT MY! YOU DONT LOOK AGE;NAME$
This program, when RUN, will tell you to enter your name at line 10. In line 20, it will stop program execution until it receives an input, followed by a press on the [RETURN] key. Whatever is entered in this line becomes
known as NAMES, and will be printed out in line 30, after the word HELLO.
Line 40 will print the request for the age on the screen, and this will be stored as variable AGE in line 50. Note that you must enter this in figures or the computer will tell you to REDO the input. Line 60 will print out the text
in that line plus the name and age that were entered. The semicolon
between AGE and NAMES is important to separate them.
4
Page 17
Chapter 1 Starting with Maths
To save entering RUN again and again, it is possible to send program
control back to the beginning. To do this, add the program line
70 GOTO 10
This will cause the program to go into a never-ending loop. To get out of this, it is necessary to press the RESTORE key while holding down the RUN/STOP key at the same time.
If its at all possible, avoid using GOTOs in your programs. Although in
Commodore BASIC you have to use them sometimes, they can make the structure difficult to understand.
Some programs
Now that you can use variables and inputs, you can start to use your 64 to write simple programs, and also make them appear more friendly. Note that, in line 10, the INPUT statement can include words that are printed.
10 INPUT WHAT IS YOUR NAME ;NAME$ 20 INPUT WHAT IS THIS YEAR IN FIGURES’’;YEAR 30 INPUT WHAT IS THE YEAR OF YOUR BIRTH’’;BIRTHYEAR 40 INPUT HOW OLD ARE YOU ;AGE 50 IF AGE = YEAR-BIRTHYEAR THEN PRINT ILL BELIEVE
YOU’’:STOP
60 IF AGE = YEAR - BIRTHYEAR - 1 THEN PRINT ILL
BELIEVE YOU’’:STOP
70 PRINT I DONT BELIEVE YOU’’
The semicolons in INPUT statements are essential to avoid a SYNTAX ERROR message. Notice too that you dont need to put a question mark in; this is automatic when the 64 expects an input. You should understand
this program down as far as lines 50 and 60. In these lines, the computer checks to see if the entered age, AGE, is possible. Line 50 is for those who have already had their birthdays this year, and line 60 is for those whose
birthdays are yet to come. IF the line is true, then a message is printed out, and the program stops.
Note the use of a colon to separate two statements on the same line. If
neither line 50 nor line 60 is true, the program continues to line 70, where
the final message is printed out, and the program stops as there are no more lines left to execute.
The IF .. .THEN statement is the first statement that you have come
across that gives your 64 some intelligence. At last it can make decisions
according to different circumstances. You can imagine almost any human
activity being governed by I F .. .THEN rules.
5
Page 18
Programming for Education on the Commodore 64
IF the alarm goes THEN get up.
IF its raining THEN go back to bed.
These statements are very useful in a program, as they can test for correct
answers to problems posed by the computer.
10 Question
20 Answer
30 IF Answer = right THEN PRINT OK 40 IF Answer = wrong THEN PRINT SORRY
50 Next Question
60 Next Answer 70 IF Answer = .. . etc.
Loops
In the last program, if you had wanted ten questions, you would have had to repeat the same structure as in the first four lines ten times. As well as being wasteful on memory, this would take a long time to enter. Luckily, you can employ a loop to take you through the same section of program a certain number of times. The only loop that the 64 possesses is the FOR... TO ...NEX T loop. Although it looks complicated, its really quite a simple idea.
You need to set up a counter in one program line at the beginning of the loop, and to have a marker line to show the end of the loop. This example should explain it more clearly. First type NEW to get rid of the last program.
10 PRINT LOOP STARTS NOW 20 FOR N = 1 TO 10 30 PRINT LOOP NUMBER N
40 NEXT N
50 PRINT LOOP FINISHED NOW
When the program is RUN, line 10 simply prints a message. Line 20 means Set a counter up called N. Start counting from 1 until you reach 10.’ Line 30 prints out the number of the loop, which is 1. Line 40 means Increase N
by one and go back to the start of the loop. N is therefore now 2. Program control goes back to the start of the loop in line 20; line 30 prints out LOOP
NUMBER 2, N is increased once again, and so on. At the end of the tenth
time through the loop, N becomes 11. When the program returns to line 20, the program says The loop is only supposed to go up to 10, so program
6
Page 19
Chapter 1 Starting with Maths
control must jump straight to the first line after the end of the loop’, ie line
50, and the final message is printed out.
If you enter this short program, and RUN it, you can see this in opera
tion. After it has stopped, enter PRINT N, and you will see that it does
indeed contain the value 11.
When a program is RUN, all the numeric variables are set to zero, and
the string variables emptied, so you can run the program again and again.
Random numbers
If you want the computer to ask specific maths questions, you now have all the knowledge you need. Its far more useful to have it select the questions out of a hat, however, and there is a very useful facility to let it do just that.
If you enter PRINT RND(O), then a number between zero and one will be printed. This number is chosen at random, and the value can be assigned to a variable.
10 FOR N = 1 TO 10 20 X = RND(O) 30 PRINT X 40 NEXT N
This program will print out ten random numbers between zero and ten. Numbers this small may not be much use in simple number problems, so line 20 could be changed to
20 X = RND(0)*100
Note: Computers use an asterisk (*) for multiply so that this wont be
confused with the letter X. The divide sign is an oblique line (/).
Line 20 will now generate numbers between zero and 100. If you RUN this now, you will see that the numbers are so random that they have several decimal places. To lose the decimal part of the number, change line 20 to
20 X = INT(RND(0)* 100)
This will generate integers, or whole numbers between zero and ninety-
nine.
At this stage you know how to generate numbers at random, how to loop
ten times to produce ten questions, how to input answers and how to check
that the answers are correct. You can combine all these points in a program designed to test knowledge of one aspect of maths.
7
Page 20
Programming fo r Education on the Commodore 64
Table 1
10 RE M 20 R E M * * * TA B L E T E S T ** * 30 R EM
100 SC = 0
110 P RINT" [S HI FT / CL R/H OM E1 "
120 P R I N T T AB< 1 2 ) " TA B L E TEST "
130 F O R N = 1 T O 10
140 A = INT(RN D (0)* 13)
150 B = I NT(R N D (0)*13)
160 P R I N T TAB< 4-N*.1 1 ); N; " ) "A" X "B T A B (2 4
170 I NPU T G
180 IF G = A * B T H E N PR I N T TA B(3 0 ) "OK"
190 IF G = A* B T H E N S C =S C 20 0 IF G < > A * B TH E N P R I N T T AB( 3 0 ) "NO - " 3 A* B 21 0 N E XT N 220 P R I N T "Y O U SCO R E D ";SC 23 0 P R I N T" P RE S S SP A C E BA R TO GO" 240 GE T Z$ 250 IF 7.$="" T H E N G O T O 240 26 0 G O T O 100
READY .
+ 1
) " = " 5
Commentary
Line 100 sets the variable SC, which holds the score, to zero.
Line 120 uses the TAB statement. This moves the print position across the screen.
Lines 140 and 150 choose two numbers between 0 and 12 at random.
Line 160 prints the question on the screen. Lines 170 to 200 accept an input and check it against the correct answer.
Line 190 increments the score by one. Lines 220 to 260 print the score and allow the user to go again.
In the past, computers have always been associated with mathematics and, even nowadays, computers tend to be used more in the maths and science departments of schools. In fact, computers are good at operating with any kind of symbols, not just numbers, and it would be a pity if their number-
8
Page 21
Chapter 1 Starting with Maths
crunching capability were the only thing they were used for in the edu cational sphere.
In spite of this, maths is an area where a simple introduction can be made. This is also an area where it is easy to write programs that test recall without being truly educational. It has been shown by many educational psychologists since the war, such as Jean Piaget and Jerome Bruner, that children develop by passing through distinct learning stages. They cannot learn automatically, but need much practical experience before being able to manipulate symbols in their minds and finally achieve a stage of logical, formal thought. The computer in its present form cannot replace practical experience, but its graphic capabilities can help tremendously where the child begins to learn to use symbols or concrete images rather than the objects themselves.
We have tried to avoid as far as possible the type of program that uses a computer to do what can be just as easily achieved using traditional mater ials. Computers are expensive artefacts to have around without careful thought being given to their most profitable use, whether at home or at school.
You may think that we have spent some time on showing you how to write a program that we are now tearing to pieces. The traditional table test is, unfortunately, an example of a weak, drill-type program. The reason it is included here is that it demonstrates simple principles of program design and is easy to understand.
If this was the only sort of program you were ever going to write, we would consider that we had failed in our aim to introduce a more dynamic approach to using computers in education. It is true that it merely tests recall, without reinforcing correct answers, diagnosing problems or attempting to offer help. It can, however, be used with caution, provided these shortcomings are realised.
Instead of leaving you with the simple version of the program, here is a listing that attempts to go one step further. It produces the numbers at random, but starts a time at the first question, so the user has a target to aim at, in trying to better his or her previous time. Also included is a facility for the teacher or parent to check the results of each program run and to compare the times taken.
Table 2
10 REM ** ,7. SC RIVEN ** 20 REM 30 REM ** 10/10/82 ** 40 REM 50 REM ** TIMED TABLE TEST ** 60 REM 70 REM ** IN ITIALIZATION **
Page 22
Programming for Education on the Commodore 64
80 REM 85 REM 90 DIM NA$(30 )
100 DIM S C (30) 110 DI M T M (30) 120 NU = 1 130 FL = 0 140 REM 150 R EM ** TES T LO OP ** 160 RE M 170 FO R NU = 1 TO 30 180 P R I N T" C S H I F T /C L R / H G ME3 " 190 PR IN T T A B (12) "T ABL E TEST"
200 P RI NT T A B ( 10 )" ENTER YOUR NAME "
210 INP UT NA *(NU)
220 P RI NT "OK, "; NA$(NU )
230 PRINT "PRE SS S P AC E B AR TO BE GIN "
240 G ET Z*
250 IFZ$="" THE N 2 40 260 SC (NIJ) =0 270 TS=TI 280 P R I N T " C S H I F T /C L R /HOM E 3" 290 P RI NT T A B (12 )" TAB LE TEST" 300 PRINT T AB( 1) NA *( NU ) 310 FOR N = 1 TO 10 320 A = I N T (R N D (0)*13) 330 B « TNT (RN D (0) * 13) 340 P RI NT T A B (4 N * . 11 );N 5 "-)A " X "B T A B ( 350 INP UT G 360 IF G = A* B TH E N PRI NT T A B (30 )"OK" 370 IF G = A*B TH EN S C( N U ) =SC ( N U ) +1 380 IF G <> A* B T HE N PRINT T A B (30)"NO- "; 390 N EX T N 400 T T = I N T ( (TI-TS)/60) 410 P R I N T N A t (NU ) ;" SCOR ED " ;SC(NU ); 420 PRINT" OUT OF 10 IN ";T T ;" SECS" 430 TM (N U) = TT
440 P R IN T "P R ES S SP AC E BAR FO R A N OT HE R GO" 450 G ET Z$ 460 IF Z'$="" TH EN GO TO 4 50 470 IF Z$=".V T HEN GO TO 1000 480 N EXT NU 490 P RIN T"C LAS S COMPLE TE D" 500 FOR M = 1 TO 2000s NEXT M
1000 REM 1010 R EM ** RE SUL TS R OU TI NE ** 1020 R EM 1030 PR IN T "C S HI F T/ C LR / HO M E3 " 1040 P RI NT T A B (15 )"R ESU LTS"
24) "
A*B
10
Page 23
Chapter 1 Starting with Maths
1050 PRINT 1055 PRINT 1060 PRINT T A B (5)"NAME ";TA B (24)"SCORE ";T A B (34)
1070 PRINT 1080 FOR NU = 1 TO 30 1090 PRI NT NU5 T A B (5)N A $ (N U )5 TA B <24)SC( N U);T A B (34)
1100 IF NU = 15 THEN PRINT"PR ESS SP ACE BAR TO
1110 IF NU = 15 THEN GET Z$
1120 IF Z$ = "" THEN 1110
1130 NEXT NU
READY.
"TI ME
TM(NU)
CONTINUE"
Commentary
One exercise, often given to children learning tables, is to discover the patterns that the answers make when these numbers are shaded in on a grid of figures. It is usual to make the grid 10 x 10 or 12 x 12, and this idea forms the basis of the next program.
There are many ways in which the 64 could be told to draw a grid of numbers on the screen, such as printing each line separately. A neater way, that uses more of the computers in-built functions, would be to put the print routine in a loop. If you simply entered
FOR N = 1 TO 200:PRINT NrNEXT N
the numbers would certainly appear, but they would not be formatted in neat lines with units and tens underneath each other. If you consider the screen layout on the 64, you will see that the numbers could fit in very regularly. The screen is 40 characters in width, and the largest number takes three figures (2,0 and 0). If a space is left between each column, you can see that ten numbers, each one preceded by a space, will exactly fit on each line.
HTU HTU HTU HTU HTU HTU etc.
If we could tell the 64 to PRINT AT a particular row and column posi tion, it would be easy. Unfortunately, such a command does not exist in Commodore BASIC. Luckily for us, however, there is such a routine buried inside the operating system. Near the top of the memory lies the kernal. This is a table of routines that jump to different parts of the memory to perform particular functions. The one we are interested in is called PLOT and lies at location 65520. All you need do is load two regis
11
Page 24
Programming for Education on the Commodore 64
ters with the row and column values by POKEing, and then call the kernal routine PLOT. You can do this in one line:
POKE 782,X:POKE 781,Y:SYS 65520:RETURN
and GOSUB this routine just before you print something. The new print
position will be at the X and Y coordinates you have just entered. This is a very useful routine and you will find it occurring quite frequently in these
programs. In the next program, it is used to move the print position across
to each new column and row.
When the numbers have been printed, the user is asked to enter the table that is required. So that these numbers show up, the next routine changes
the colour of just these numbers to white. Again, this is not a very complex task. The 64 stores the screen text in one set of locations, and the colour in
which the text appears in another. The second set of locations starts at
55296. As the grid of numbers is spaced evenly, starting at every fourth
location, the colour value can be POKEd in another FOR... NEXT loop.
How can you tell it to act only on the numbers in the particular table
chosen? A useful trick is to test if there is a remainder after division by the table number. If this seems difficult to grasp, try this program:
10 INPUT A 20 FOR N = 1 TO 100 30 IF INT(N/A) = N/A THEN PRINT A
40 NEXT N
This runs through the numbers from 1 to 100, and divides each one by the number you input. If the number has a remainder, the INT function will round down to the nearest whole number below, eg INT(11.5) = 11. It will only print those numbers that dont have a remainder, as the INTeger of 12
is 12.
Notice that this program tests the codes of keys that are pressed in GET
statements. If any key other than the numbers 1 to 9 is pressed, the program
loops back to ask for the key to be pressed again.
Table Patterns
10
REM
*#*
TABLE
PATTERNS
***
20 REM
30 REM
***
A.J.S.
, 4/10/83
*#*
40 REM 50 REM
60 REM
12
CLEAR SC RE EN +
Till.
Page 25
Chapter 1 Starting with Maths
70 REM 8@ PR I NT " C SHI FT / CL.R / HOME 1 " 90 X =0:Y=0 5G OSU B540
100 P R I N T"TABLE P A T T E RNS"
110 X=2: Y~2
120 REM 130 REM *** LOOP TO PRINT NUMBERS ***
140 REM
150 FOR N * 1 TO 200
160 IF N=10 OR N=100 THEN X = X-1
170 60SUB 5 40:FRINTN 180 X ~ X + 4'
190 IF I N T(N /10 )= N /10 THEN X-l 200 IF I NT (N / 10) -~N / 10 THEN Y=Y+1
210 IF I N T (N /10)= N /10 AND N>99 THEN X=0 220 NEXT N
230 REM
240 REM *** SELECT TABLE ROUTIN E *** 250 REM
260 POKE 198,0
270 X = 5:Y « 23 :GO SUB540
280 P R INT"*PRESS THE TABLE NUMBER*" 290 GET AT:IF AT = "" THEN 290 300 A-~VAL.. (AT)
310 IFAC 1 THEN 260
320 X= 30: Y=0 :GOSUB540:PR I N T "T A BLE"A " 330 REM
340 REM *** CHANGE NUMBER COLOUR S ***
350 REM
360 COL = 55296 370 FOR N = 1 TO 200
380 IF INT(N/A) <> N/A THEN 420 390 POKE COL.+81 + (N- 1) *4,1
394 REM
400 POKE COL+ 81+(N--1)*4+1,1
410 POKE CO L +81+ < N -1)*4+2,1
420 NEXT N
430 X=5:Y = 23:GOSU B540
440 POKE 198,0
450 PRIN T"PRESS SPACE BAR TO CLEAR"
460 GET AT:IF AT = "" THEN 460
470 FOR N = 55296 TO 56319
480 POKE N ,14
490 NEXT N
13
Page 26
Programming for Education on the Commodore 64
500 GOTO 260
510 REM
520 REM *** "PRINT AT" SUBROUTI NE ***
530 REM
540 P0 K E 7 82 ,X s P 0 KE78 1,Y s SYS6552 0s RETURN
RE A D Y.
Commentary
Line 80 clears the screen. Line 90 sets the X and Y coordinates to zero.
Lines 150 to 220 contain the loop that prints the table grid.
Lines 190 to 210 move the print position at the end of the line when the
value of N is exactly divisible by 10.
Lines 260 to 310 allow the number of the table to be input. Line 320 prints
the table number at the top of the screen.
Variable COL is the location of colour memory. Lines 370 to 420 change
the text colour by POKEing the relevant memory locations. Line 380 skips
over the POKEs if the value of N is not exactly divisible by the table chosen.
Line 540 contains the PRINT AT subroutine.
Lines 280 and 450 contain the same number of characters in the print
statements so that they can print on top of one another.
Bingo
After the element programs in this chapter, we move on to a couple in the
area of maths that extend the use of the 64s features. If you are fairly new
to BASIC, you may find the ideas more difficult to grasp. Although they are explained in some detail in the program notes, you may prefer simply to type in the listings. As your skills improve in the following chapters, you can come back to these programs at a later stage.
Bingo is an attempt to move beyond the simple approach of answering questions on screen. The child is presented with a game format, and, although the questions still only test recall of previously-learned know
14
Page 27
Chapter 1 Starting with Maths
ledge, there is a reward motive in completing the whole card. When this stage is reached, there is a visual display.
There is always some problem over how many of a computers facilities need to be incorporated in every program. Just because Commodore computers support voice synthesis, high-resolution graphics and music in three-part harmony, it does not mean that a good program has to possess all of these features, or indeed any of them. A happy medium has to be reached at the planning stage. Some programs benefit from using specialist features, but usually take more time to program. In some cases, their implementation can be irritating and work contrary to the expectation of the teacher or parent.
One example is the addition of loud sound effects, which can be disturb ing in a classroom if there are other children engaged in quiet activities. If this is the case, removal of the offending sound commands will prove an answer. An alternative, of course, is to reduce the volume by use of the TV volume control! Flashing displays should usually be avoided. Where sections of the screen are required to flash, this is done to draw attention to part of the display. Having had personal experience of a child suffering a
petit mat attack due to the program loading display on a cheaper computer,
we are particularly aware of this possibility. Also, overuse of this feature is likely to prove distracting in many cases.
10 REM BIMBO - PAT HALL, JAN '84
20 REM 30 GO SUB 110 * REM INITIA LI SA TION
40 GOBUB 330 : REM SET UP 50 GOSUB 770 : REM TEST- 60 GOSUB 1150 s REM REWA RD 70 GOTO 40 80 END 90 REM INITIALISATION
100 REM SET UP ARRAYS 110 DIM A( 20 ) : DIM B < 20 ) 120 DIM C( 20 ) s DIM D( 20 ) 130 DIM L B ( 20 ) 140 REM SELECT LOCATIONS FOR BOXES 150 N = 0
160 FOR I = 1144 TO 1180 STEP 9 170 FOR J = 0 TO 600 STEP 200 180 N N + 1
190 L B ( N ) = I + J 200 NEXT J 210 NEXT I
15
Page 28
Programming for Education on the Commodore 64
220 REM INITIALISE GAME AMD SCORE 230 6 » 0 s S * 0 240 REM WHITE SCREEN 250 POKE 53280, 1 s POKE 53281, 1 260 PRINT "" 270 REM SWITCH ON EXTE NDED COLOUR MODE 280 POKE 53265, 91 290 RETURN
300 REM
310 REM SET UP GAME
320 REM YELLOW BAC KGROUND 330 POKE 53282, 7 340 REM PUT TITLE ETC.. ON SCREEN 350 PRINT CHR'-T ( 144 ) 360 X » 14 s Y - 0 : GOSUB 710 370 PRINT "<< BINGO >>" 380 PRINT CHRT < 28 ) 390 X - 0 : Y = 1 : GOSUB 710 400 G = G + 1
410 PRINT "GAME "$ G
42.0 X = 31 : GOSUB 710 430 PRINT "SCORE r, S
440 FOR I = 1 TO 20 450 REM INITIALISE FLAG 460 D< I ) = 0 470 REM CALC UL ATE NUMBER
480 A ( I ) = I N T ( R N D ( 1 > * 8 > + 2
490 B ( I ) = I N T < R N D ( 1 ) * 5 ) + 5 500 C< I ) = A ( I ) * B < I )
510 REM PLACE BOX ON SCREEN
520 FOR J = 0 TO 3
530 FOR i< = 0 TO 80 STEP 40
540 L = LB ( I ) + J + K
550 POKE L + 54272, 7 ; POKE L, 96
560 NEXT K
570 NEXT J 580 REM PLACE NUMBER IN BOX
590 REM RED FOREGROUND 600 POKE L B ( I } + 54313, 2 610 POKE L B ( I ) + 54314, 2 620 E = I N T ( C< I > / 10 ) + 112 630 POKE L B ( I ) + 41, E 640 E = E - 112 650 E = C( I ) - E * 10 + 112
16
Page 29
Chapter 1 Starting with Maths
660 POKE LB < I ) +42, E 670 NEXT I 680 RET URN 690 REM 700 REM PRINT TAB ROUTI NE 710 POKE 782, X. s POKE 781, Y 720 SYS 65520 730 RETURN 740 REM 750 REM TEST ROUTINE 760 REM CHOOSE ONE BOX AT RANDOM
770 T = INT( R N D ( 1 ) * 20 ) + 1
780 REM CHECK BOX IS STILL ON SCREEN 790 IF D( T ) = 1 THEN 770 800 DI T > = 1 810 X = 9 s Y = 22 s GOSUB 710 820 PRINT "WHAT IS" AIT) "X" BIT)
830 X. = 30 : GOSUB 710 840 PRINT " 850 X = 30 : GO SU B 710 860 INPUT AN* s AN = VAL I AN* ) 870 IF AN <> Cl T ) THEN 830 880 REM INCREASE SCORE 890 S = S + 1 900 X - 37 : Y = 1 : GOSUB 710 910 PRINT S 920 REM SOUND EFFECT, VOICE 1 930 POKE 54296, 15 : REM VOLUME 940 POKE 54277, 136 : REM ATT / DEC 950 POKE 54278, 136 ; REM SUST / REL
960 POKE. 54276, 17 : REM W AVEFORM 970 POKE 54273, 76 : REM HIGH NOTE 980 POKE 54272, 252 : REM LOW NOTE 990 FOR I = 1 TO 100 s NEXT I
1000 POKE 54277, 0 1010 POKE 54278, 0 1020 POKE 54276, 0 1030 REM ERASE BOX 1040 FOR I = 0 TO 3 1050 FOR J = 0 TO 80 STEP 40 1060 L = LB I T ) + I + J 1070 POKE L. + 54272, 1 : POKE L, 32 1080 NEXT J 1090 NEXT I
17
Page 30
Programming/or Education on the Commodore 64
1100
1110 FOR I
1120
IF I N T<8/20) < > S / 20
= 1
RETURN 1130 REM 1140 1150
REM REWARD ROUTINE
PRINT
it l l
1160 RESTORE 1170
FOR I 1180 READ
1190
FOR J == 0 TO 1 1200 FOR K 1210
1220 1230 1240 1250
1260 1270
1280
POKE POKE NEXT K NEXT NEXT I FOR I = 1 TO 3000 PRINT RETURN
= 1
N
= 0
N + J N + J
J
H M
1290 REM 1300
1310 DATA
REM D
ATA FOR 'BINGO'
1024, 1104, 1184, 1320 DATA 1424, 1330 DATA 1824, 1340
1350
1360
1370
1380 1390 1400 1410 1420 1430 DATA 1440 1450 1460 DATA 1470 1480
1490
1500
1 51 0
1520 1530
DATA 1190, DATA 1548, DATA 1868, DATA DATA
1274, 1354, 1434,
1674, 1754, 1834, 1914, 1038
DATA 1118 , DATA
1518, 1598,
DATA 1918, 1080, DATA
1401 ,
1801 ,
DATA
1284 , 1364,
DATA 1684,
1209, 1289,
DATA 1609, DATA 1 1 3 3, DATA 1773, 1853, DATA 1298, DATA 1698,
1
DATA DATA
.1. a
1622, 1702,
THEN 770
TO 1000
: NEXT I
TO 115
TO 40 STEP 40
+ K + + K, 1
54272, 28
xi
s NEXT I
1264, 1504 , 1584, 1664, 1744 1904,
1026,
1068, 1110 1270, 1350, 1388, 1466 1590, 1670, 1750, 1830 1906, 1034, 1114,
1514,
1198,
1278, 1678, 1161 ,
1481 , 1561 ,
1358, 1438
1758,
1241 , 1321
1641 , 1882, 1044, 1124, 1204
1524, 1764,
1444, 1844, 192.4,
1369, 1449, 1529 1689, 1769, 1849, 1213,
1612,
1614,
1931 , 1138, 1218 1378,
1458, 1538, 1618 1778, 1858, 1060, 1142 1302, 1382, 1462,
1782, 1862, 1940
1344
1194 1594
1838
1721
1604 1129
1051 1693
1542
18
Page 31
Chapter 1 Starting with Maths
Commentary
The program is controlled by lines 30-80. Line 70 causes the program to continue indefinitely until RUN/STOP is depressed.
Lines 100-290 form the first subroutine of the program, called by line 30 in the control module. The routine initialises various variables needed by Bingo. Five arrays are set up. The A and B arrays hold the twenty pairs of numbers presented to the child and the C array stores the answer for each multiplication. The D array consists of flags, one for each number pair, which ensure that the same question is not repeated. Finally the LB array stores the screen locations of each coloured box the numbers are printed in. The actual value of each member of the LB array is the location of the top lefthand corner of the coloured box. These values are calculated by the two nested FO R .. .NEXT loops between lines 160-210. Here the values that the I and J loop variables STEP through are arranged so that their sum, I + J, gives the exact location. Note the way in which the variable N is incremented through the loops to identify each box.
This first subroutine also sets the values of S (the score achieved by the child) and G (the number of sheets completed) to zero. Line 250 sets the entire screen area to white and then line 280 switches on the Commodore 64s extended colour mode. This allows text to be printed with a different background colour to the screen background (and is the equivalent of the command COLOUR (colour +128) in BBC BASIC). Extended colour mode is switched on with POKE 53265,91 and off with POKE 53265,27. It allows three more background colours to be set up simultaneously on the screen, but requires a different character set for each.
Lines 310-680 present the bingo sheet. Use is made of the PRINT TAB routine at lines 700-730. This allows an easy way to format the display. X and Y screen coordinates have to be defined before calling the routine and this is done here, for example, at line 360. Lines 480-500 calculate the questions and answers and the boxes are drawn by lines 520-570. The J and K values in the two nested loops increment the value of LB calculated before, and this value is then POKEd to screen and colour map by line 550. Each number is placed in its box by 600-660. Extended colour mode is used and the two-digit numbers have to be separated by the simple arithme tical routine into the individual ten and unit figures. These are POKEd separately to the screen.
Lines 750-1120 present the test. The Tth box is chosen at line 770 and the flag is checked by line 790 to make sure that box and question have not already been chosen. The TAB routine is used again in presenting the ques tion and the childs input is error-trapped. A sound effect is produced and
19
Page 32
Programming for Education on the Commodore 64
finally the box itself is erased by lines 1040-1090. Line 1100 tests when the entire screen is cleared.
The reward routine simply places the word Bingo large upon the screen. This is done in a very similar way to the drawing of the boxes but here the location of each box is not calculated but held as lines of DATA which are READ by line 1180. The nested loops between 1190-1240 draw each section of the letters.
Factor
The last program in this chapter, Factor, extends the approach to that of a video game. It does not require high-speed manual dexterity, however, simply the ability to input any factor of a number that appears on the screen. The number is successively factorised by the inputs until it reaches
1, when another number is chosen. If the factor chosen is permissible, a plane flies across the screen and drops a bomb on to the number resulting in an explosion. If the number is incorrect, the bomb misses. There is also an on-screen scoring facility.
A last point needs to be made about the type of reward offered in a program. Although an exciting display may offer positive reinforcement to the child, it is hoped that the reward will become intrinsic, ie that the child enjoys the success of getting a problem right without any external
reward. When adults succeed in solving a whodunnit, or complete the Times crossword, the achievement alone is enough. This internalisation has taken a long time to achieve, however, and praise, even from a
machine, is important!
It is also necessary to point out incorrect responses in a gentle manner, or
the child feels despondent and even aggressive. If a computer has ever
beaten you at chess, you will know the feeling. Although it may seem
obvious to state, the difficulty of the problem has to be within the solving capacity of the child. When designing programs, the incorrect response
should not result in a screen display that is more impressive than that for a
correct response, or the reinforcement effect will be lost, as the child
attempts to fail because the display is prettier!
1000 REM FACTOR - PAT HALL, JAN '84 1010 REM 1020 GOB LIB 1120 : REM DEFINE SPRIT ES 1030 GOSUB 1610 a REM STORE PRIMES 1040 GOSUB 1700 s REM SET UP DISPLAY 1030 GOSUB 1880 a REM TEST RO UT INE
20
Page 33
Chapter 1 Starting with Maths
1.060 80SUB 21.60 s REM FLY PLANE I. 070 GOTO 1.050 .1.080 END 1090 REM II. 00 REM DEFINE SPRITES
11.1.0 REM BEGI NNI NG OF 6567 VID EO CHIP
11.20 VC - 53248
11.30 REM PLANE, SPRITE 0 1140 REM SET POINTER FOR SPRITE 0
11.50 POKE 2040, 13
1.1.60 REM READ DATA FOR SPRITE 0 1170 FOR I. = 0 TO 1.9 1180 POKE 832 + I, 0
11.90 NEXT I
1.200 FOR I = 20 TO 62
1.210 READ N ; POKE 832 + I, N
1.220 NEXT I 1230 REM BOMB, SPRITE 1. 1240 REM SET PO IN TER FOR SPRITE 1
1.250 POKE 2041, 1.4
1.260 REM READ DATA FOR SPRITE 1 1270 FOR I 0 TO 39 1280 POKE 896 + I,
0
1.290 NEXT I
1.300 FOR I = 40 TO 62
1.310 READ N s POKE 896 + I, N
1.320 NEXT I
1330 REM FLASH, SPRITE 2
1340 REM SET POINTE R FOR SPRITE 2
1350 POKE 2042, 15
1.360 FOR I = 0 TO 39
1370 PO KE 960 + I, 0
1.380 NEXT I
1390 FOR I = 40 TO 62
1.400 READ N s POKE 960 + I, N
1410 NEXT I.
1420 REM ENLARGE SP RI TES 0 AND I.
1430 REM X--DIEECTION
1.440 POKE VC + 29, 5
1450 REM Y- DIRECTION
1460 POKE VC + 23, 5
1470 REM SELECT COLOUR
1480 REM BL UE PLANE 1490 POKE VC + 39, 6
21
Page 34
Programming for Education on the Commodore 64
.1.500 REM BLACK BOMB
1.510 POKE VC + 40, 0 1520 REM BROWN FLASH 1530 POKE VC + 41, 9 1540 REM LOCATE FLASH ON SCREEN 1550 POKE VC + 4, 165 ; REM X-COORD 1560 POKE. VC + 5, 143 5 REM Y-COORD
1.570 RETURN 1580 REM 1590 REM STORE PRIMES 1600 REM DIMENS ION PRIMES' ARRAY, PN 1610 DIM P N ( 29 ) 1620 REM READ PRIME NUMB ERS FROM DATA 1630 FOR I = 1 TO 29 1640 READ N : P N ( I > - N 1650 NEXT 1 1660 RETURN 1670 REM
1.680 REM SET UP DISPLAY
1690 REM CYAN BORDER
1700 POKE 53280, 3
1710 REM CYAN BACKGROUND
1720 POKE 53281, 3
1730 REM CL EAR THE SCREEN
1740 PR I NT " I SHI FT / CLR / HOME li " 1750 REM DRAW DESERT LANDSCAPE 1760 FOR I = 0 TO 280 STEP 40 1770 FOR J = 0 TO 39 1780 REM POKE COLOUR TO SCREEN 1790 POKE 5597 6 + I + J,
8
1800 REM POKE. CHAR ACT ER TO SCREEN 1810 POKE 1704 + I + J , 160 1820 NEXT J 1830 NEXT I 1840 RETURN 1850 REM 1860 REM TEST ROUTINE 1870 REM SELECT NUMBER 1880 TN = INTI R N D < 1 ) * 70 ) + 30 1890 REM CHECK. TARGET IS NOT PR IM E 1900 N = TN : GQSUB 2080
1.910 IF PF - 1. THEN 1.880
1.920 X = 19 : Y = 16 s GOSUB 2520
1.930 PRINT TN
22
Page 35
Chapter 1 Starting with Maths
1940 X = 8 : Y = 0 : 6QSUB 2520
1950 PRINT "TYPE A FACTOR OF" TN
1960 X = 30 : GO SUB 2520
1970 PRINT "
1980 GOSUB 2520
1990 INPUT FT 2000 IF A S C ( FT) < 49 THEN 19600 2010 IF A S C ( FT) > 57 THEN 1960 2020 F = V A L ( FT) 2030 IF INT(TN/ F) <> TN/F THEN 1960 2040 IF F = 1 OR F = TN THEN 1960 2050 RETURN 2060 REM 2070 REM PRIME CHECK 2080 PF = 0 2090 FOR I = 1 TO 29 2100 IF N » PN( I ) THEN PF = 1 2110 NEXT I 2120 RETURN 2130 REM 2140 REM FLY PLANE 2150 REM SWITCH ON PLANE, SPRIT E 0 2160 POKE VC + 21, 1 2170 REM PLANE ON RIGHT HAND SIDE 2180 REM SO SET MOST SIGNI FICANT BIT 2190 POKE VC + 16, 1 2200 FOR I = 50 TO 0 STEP -1 2210 REM ADJUST X-COORD OF PLANE 2220 POKE VC, I 2230 REM LEAVE Y-COOR D C ONSTANT 2240 POKE. VC + 1 , 80 2250 NEXT I 2260 REM CON TINUE FL IGHT ON LEFT 2270 REM TURN OFF MOST S IGN IFICANT BIT 2280 POKE VC + 16, 0 2290 REM DROP BOMB, SWITCH ON SPRITE 1 2300 POKE VC + 21, 3 2310 FOR I = 255 TO 189 STEP -1 2320 REM ADJUST X-COORD OF PLANE 2330 POKE VC, I 2340 REM LEAV E Y-CO ORD CONSTANT 2350 POKE VC + 1, 80 2360 REM BOMB HAS SAME X-COORD AS PLANE 2370 POKE VC + 2, I
23
Page 36
Programming for Education on the Commodore 64
2380 REM Y-COORD OF BOMB 2390 POKE VC + 3, 355 - I 2400 NEXT I 2410 REM SWI TCH BOMB OFF, FLASH ON 2428 PO KE VC +21, 5 2430 REM FLY PLANE ON 2440 FOR I = 188 TO 0 STEP -1 2450 POKE V C , I : POKE VC + 1, 80 2460 NEXT I 2470 REM SWI TCH OFF PLANE AND FLASH 2488 POKE VC + 21, 0 2490 RETURN 2500 REM 2510 REM PRINT TAB ROUT INE 2520 POKE 782, X : POKE 781, Y
2538 SYS 65520 2540 RETURN
2550 REM 2560 REM DATA FOR PLANE, SPRITE 0 2570 REM 2580 DATA 7, 0, 0, 15, 3, 128, 30, 4 2590 DATA 64, 60, 127, 255, 252, 255 2608 DATA 255, 255, 255, 255, 255, 1 2610 DATA 255, 254, 3, 255, 224, 0,
CN IN
in
2620 DATA 224, 0, 63, 224, 0, 15, 240 2630 DATA 0, 3, 252, 0, 0, 254, 0,0, 63 2640 REM 2650 REM DATA FOR BOMB, SPRITE 1 2660 REM 2670 DATA 4, 8 , 0, 6, 0, 0, 7, 0, 0 2680 DATA 24, 0, 0, 112, 0, 0, 240, 0 2698 DATA 0, 240, 0, 0, 224, 0 2700 REM 2710 REM DATA FOR FLASH, SPRITE 2 2720 REM 2730 DATA 65, 0, 0, 42, 0, 1, 0, 64, 0 2748 DATA 136, 128, 4, 65, 16, 2, 42 2750 DATA 32, 1, 0, 64, 0, 227, 128 2760 REM 2770 REM DATA FOR PRIME NUMBERS 2780 REM 2790 DATA 2, 3, 5, 7, 11, 13, 17, 19 2800 DATA 23, 29, 31, 37, 41, 43, 47
24
Page 37
Chapter 1 Starting with Maths
2810 DATA 53, 57, 59, 61, 67, 71, 73 2820 DATA 79, 83, 87, 89, 91, 93, 97
REA D Y .
Commentary
Factor is controlled by lines 1020-1070.
The first subroutine, called by line 1020 of the control module, defines three sprites used in the program. Sprite 0 is a plane, sprite 1 is the bomb that it drops and sprite 2 is the flash of the resultant explosion when the plane successfully hits its target number. Each of the three sprites is defined in a similar way.
The diagram shows how the plane has been drawn on a rectangular grid of 21 rows and 24 columns. Each block of eight horizontal squares has been coded as a binary number and then stored in a special area of memory reserved by line 1150. Usually after a sprite has been designed and calcu lated as a set of 63 numbers, these are held inside a program as DATA. However, in the case of the plane sprite it can be seen from the diagram that the first twenty blocks of eight squares have not been filled in at all. Thus the numbers corresponding to these blocks are each zero.
It is much easier to place these values into memory with the simple FOR
.. .NEXT loop between lines 1170-1190 than to store them as DATA.
The remaining numbers for the sprite are, however, READ from the
DATA lines. The values for sprite 0 are held at locations starting at 832. The two other sprites are stored at locations starting at 896 and 960. Each time, the sprite pointer is set to indicate where in memory the sprite data is to be found.
The rest of the routine then attends to other sprite parameters. These are
all done in terms of the number VC defined at line 1120. This is the starting location of the video chip, which handles all the sprite manipulation.
POKE VC+ 29, for example, is the location which controls the size of a
sprite in the horizontal, x, direction. POKEing the sprite number to
VC + 29 will cause it to be stretched sideways. In this program, both sprite
0 and sprite 2 need to be expanded and so the value 5 is POKEd. This is the
sum of 1 (sprite 0) and 4 (sprite 2). In a similar way, the colour and location
of sprites on the screen are all controlled with reference to this important
location VC.
In the operation of the program, it is necessary to be able to identify prime
numbers. Primes are held, again as lines of DATA, and READ into the PN
array by the routine between lines 1590-1660.
25
Page 38
Programming for Education on the Commodore 64
Diagram A
The screen display is built up by the routine between lines 1680-1840. As in the program Bingo, the screen is controlled by directly addressing the screen and colour maps. For example, the bottom section of the screen is turned into a desert by the nested loops between lines 1760-1830.
The test routine from lines 1860-2120 present a target number which the child has to attempt to bomb. This can only be done by typing in a correct factor. The input routine is error-trapped to prevent the number itself, or
1, or non-numeric values, from being accepted. The target number itself is carefully tested by the separate subroutine to make sure that a prime number is not set. This is achieved by using the array of prime numbers set up earlier in the program.
The routine from 2140-2500 flies the plane over the desert and bombs the number if a correct factor has been typed. The REMs present in this routine indicate each stage of the flight. Note how the location VC + 16 has been set at line 2190 to allow the flight across the righthand side of the screen.
26
Page 39
CHAPTER 2
Small is Beautiful
Short programs and efficiency Missing: find the numbers
ordering numbers Series: maths as an introduction to strategy
simplicity in program structure Number: a program to find a
hidden number using graphics to brighten up simple ideas
Firework: a program to test general numerical skills the problems
involved in learning fractions Fraction: a program to help with numerators and denominators.
Although it has been pointed out that exciting displays do not turn a mediocre program into a good one, there is no doubt that some programs benefit from interesting graphics.
At the beginning of this chapter, we shall show you some short programs in which the fast calculating power of your 64 is used, rather than its graphic ability. Towards the end, some of these ideas are put into a more advanced program that includes graphics.
Firstly though, an indication of simple ways in which the computer can
do tasks faster and more efficiently than humans.
A question of format
In the last chapter, there was a table test program that used the computer to generate the questions. The same idea can be used with any sort of maths
problem. One difficulty that many children in the early primary years
experience is that of coping with problems posed in an unfamiliar way. If a sum says
15 - ? = 7
then it is likely to pose more problems than one that says
15-7 = ?
The following program chooses 10 problems at random using this format.
27
Page 40
Programming for Education on the Commodore 64
Missing
1 REM *** MISSI NG NUMBERS *** 20 REM 30 REM *** A.J.S. 1/7/83 *** 40 REM 50 S=0s PR INT"[S H I FT/CL R /HOME 3"s PRINT 60 FOR N - 1 TO 10 70 A = INT < R N D (0)*100) 80 B = IN T (RN D (0)*100) 90 IF B>A THEN 70
100 C = A - B
110 PRINT N A ;" - ? =";B
120 PRINT
130 INPUT "MISSING NUMB ER IS";GU ESS
140 IF GUESS = C THEN PRINT "OK":S=S+1
150 IF GUESS <>C THEN PRINT "NO";C
160 FOR COUNT=l TO 500 : NEXT COUNT
170 PRINT "CS HIFT /CLR/ H OME3 " 180 NEXT N
190 PRINT"YOUR SCORE WAS"S"O UT OF 10"
REA D Y m
Line 50 sets the score to zero and clears the screen. Line 60 starts the loop to ask 10 problems.
Lines 70 and 80 choose random whole numbers between 0 and 99, and line 90 checks that B is not greater than A.
Line 100 calculates the answer. Line 110 sets out the question line. The semicolons in this line are not essential, as they are in an INPUT line, eg line 130.
Line 120, a simple PRINT statement, is an easy way of missing a line.
Line 130 requires an input, GUESS. Lines 140 and 150 check GUESS and compare it with the correct answer, C. If it is the same, the score is increased by one.
28
Page 41
Chapter 2 Small is Beautiful
Line 160 shows a simple way of introducing a pause. The empty FOR.. . NEXT loop counts up from 1 to 500, and then allows the program to continue.
Line 170 clears the screen. Line 190 prints the score, S.
Everything in order
Sequences and series of numbers sometimes take a long time before the idea is grasped. The next program shows how the computer can generate a series of numbers in a particular range. Although not an elaborate pro gram, it will generate sequences and mark the responses all day without getting tired.
The particular sequence chosen is one that increases by the same amount
each time, eg
4 8 12 16 20 24 etc.
It is quite easy to change the program so that it chooses numbers where the increment alters by a set amount, eg
1 2 4 7 11 15 etc.
or one that generates squares such as
1 4 9 16 25 36 etc.
Series
10 REM *** SERIES GUESSER ***
20 REM 30 REM *** A.J.S. 5/7/83 *** 40 REM 50 PRINT" CSHIFT/CLR/HOMEIt " : PRINT 60 PRINT "SERIES" 65 PRINT;S = 0 70 FOR N = 1 TO 10 75 PRINT "QU ESTION"N 80 A -I N T (R N D (0)* 2 6 )
90 I NCR = I NT (F;NB ( 0) * 11)
100 PRINT A;
110 FOR X = 1 TO 4
120 PRINT A + (INCR * X);
29
Page 42
Programming for Education on the Commodore 64
130 NEXT X 140 PRINT 150 B = A + (INCR * X) 160 INPUT "WHAT IB THE NEXT NU MBER";G 170 IF G = B THEN PRINT"OK":S-B+l 180 IF G O B THEN PR I N T " NO "B 190 NEXT N
200 PRINT "YOU SCO RED"S "OUT OF 10"
READ Y .
Commentary
Line 50 clears the screen. Line 65 sets the score to 0 at the start.
Line 70 begins the loop to generate 10 questions. Line 80 chooses A, the start of the series, at random. Line 90 chooses the size of the step or incre ment between each number, INCR. Line 100 prints the first number in the sequence.
Lines 110 to 130 contain a loop to generate the next four numbers in the series. Each number is bigger by the amount INCR.
Line 150 relies on the fact that the FOR... NEXT loop counters contain a value one higher than the last count of the loop, ie 5, to set B equal to the next number in the sequence. Line 160 asks for an input, G.
Lines 170 and 180 compare G with the correct answer, B, and print OK or NO, as well as increasing the score if necessary.
Line 200 prints the final score.
Logical guessing
There is a game that you can play using the blackboard, a piece of paper, or just your memory. It is the familiar one of I am thinking of a number ask me any question about it you like, but I can only reply using yes or no. Faced with this problem for the first time, the child may be inclined to guess. If he or she realises that the number can be anything under (say)
1000, it is not long before it is appreciated that the task is difficult, and that some sort of logical strategy must be employed.
30
Page 43
Chapter 2 Small is Beautiful
In a classroom situation, the class usually divides up into guessers,
hypothesises and pseudo-hypothesisers. Guessers do just that.
Hypothesisers try to reduce the number of possible choices by asking such questions as is it an even number or is it less then 500’. Pseudo-hypothe sisers are more subtle than the guessers. If the response no is received to the question is the number less than 500’, then the sort of question they will ask is is it more than 500. It sometimes seems a big hurdle to jump before this type of child realises that a no response to a question provides as much information as a yes.
This sort of game is a useful activity as it can improve mental skills including memory and visualisation of a sort of internal number line. It also helps develop logical questioning and inquiry strategies. You can probably see that the most logical strategy to follow is to divide the answer approximately by two.
A typical series of questions may go as follows:
Number is 242.
Is it
larger than 500?
Is it
larger than 250?
Is it
larger than 125?
Is it
larger than 190? Yes.
Is it
larger than 230?
Is it
larger than 240? Yes.
Is it
larger than 245?
Is it
larger than 243?
Is it
larger than 241?
Is it
242? Yes.
No. No. Yes.
Yes.
No. No. Yes.
Using this binary chopping method, it is always possible to reach the number without guessing in less than ten attempts two raised to the power of ten is, of course, 1024. Most older children will adopt this method, or one similar, until they get fairly close and then guess. It is not envisaged that they will actually be calculating the odds on a correct guess, although at this level it does resemble gambling!
A computer can choose the number, select correct responses and give the total number of attempts at the end. In this way, a teacher or parent can be relieved of the task of administering this game to children, and the pro gram allows an individual or group approach. For the child who finds it difficult to acquire a good strategy, it can provide as much practice as is necessary.
There are no interesting displays, no exciting sounds, and the program has been made as simple as possible to show how even short programs can have their point.
31
Page 44
Programming for Education on the Commodore 64
Number
10 REM ** NUM BER Gl.JESSER ** 20 REM ** A.J.S. 1/4/82 * * 30 REM 40 P 0 K E 5 3 2 8 1,1:REM BACKGROU ND WHITE 50 PR IN T "ISHIFT/CLR/HO M E 1" 60 5C-0 70 BO=0 80 PRINT" I. WILL THINK OF A NUMBER. "; 90 P RINT"YOU MUST TRY"
100 PRIN T"TO GUESS IT IN AS FEW GOES ";
110 PR INT"AS YOU CAN."
120 PR INT"CHOO SE A TOP LIMIT ";
130 INPUT"FOR YOUR NUMBER";T P
140 N = I N T(R N D <1)* T P )
150 P RINT" THE NUMB ER LIES BETWEEN ";
160 PRINTED;" AND ";TP
170 SO=SC+l
180 PR INT"E NTER GUESS NO.";SC;" ";
190 INPUT GS 200 IF GS-N THEN GOTO300 210 IF GS>N THEN TP=GS 220 IF 6S<N THEN B0-G3 230 G O TO 150 240 REM 300 PRINT"YO U GOT IT IN ";SC;" GOES" 310 PRINT"PR ESS SP AC E BAR TO GO AGAIN" 320 GET Zf 330 IF Z*<>" " THEN 320
340 GOTO50
READY.
Commentary
Line 40 sets the background colour to white. Up to this point, you have
probably been putting up with the light blue background that appears when you switch on. POKEing this location in memory with a number
from zero to 16 sets the background colour.
Line 60 initialises the score, SC. Line 70 sets the bottom limit for the
number to zero.
32
Page 45
Chapter 2 Small is Beautiful
Lines 80 to 120 contain the instructions.
Line 130 allows the user to set the upper limit for the number. Line 140
chooses the number to be guessed.
Lines 180 and 190 allow the input of a guess.
Lines 200 to 220 check the guess, GS, against the chosen number, N. If the
guess is larger than the number, then the guess becomes the new upper
limit; and if the guess is smaller than the number, the guess becomes the
new bottom limit.
Although it is not so easy to see, there is a loop between lines 150 and 230. This loop is negotiated each time a number is guessed, until the correct answer is given. This causes an exit in line 200. 64 BASIC only supports a FO R.. .NEXT loop, and this entails knowing how many times you will
pass through it, which in this program, of course, you dont.
Lines 300 and 310 print the number of guesses, SC.
Line 320 contains the words GET Z$. This waits until a key is pressed, and then checks the input in line 330. If the key is anything except the space bar, the program repeats line 320.
Line 340 sends the program back to the start.
Firework
The next program, Firework, is also based on a simple idea offering
examples of the four rules (add, subtract, multiply and divide). The incen
tive to do well here is the impressive display after five correct answers.
Each of the graphic routines is held in a module, and may be adapted to your own use or copied directly into a different program. Full notes on how the effects are achieved are to be found at the end of the program.
1000 HEM FIREWORK
- PAT HALL, JAN '84
1010 REM
1020 GOSUB 1110 :
1030 GOSUB 1520 : 1040 GOSUB 1730 : 1050 GOSUB 2040 ;
REM DE FINE SPRITES REM SET UP DISPLAY REM TEST ROUTINE
REM LA UNCH RO CKET 1060 GOTO 1030 1070 END
33
Page 46
Programming for Education on the Commodore 64
.1.080 REM
1.090 REM DEFINE SPRITES
1.100 REM BEGI NNI NG OF 6587 VIDEO CHIP 1110 VC « 53248 1120 REM ROCKET, SPRITE 0
1.130 REM SET P OINTER FOR SPRITE 0 1140 POKE 2040, 13 1150 REM READ DATA FOR SPRITE 0 1160 FOR I = 0 TO 8 1170 READ N : POKE 832 + I, N 1180 NEXT I 1190 FOR 1 - 9 TO 51 STEP 3 1200 POKE 832 + I, 0
12.1.0 POKE 833 + I, 1.27 1220 POKE 834 + I, 0 1230 NEXT I 1240 FOR I 54 TO 62
1.250 READ N : POKE 832 + 1, N 1260 NEXT I
1.270 REM EXHAUST, SPRITE 1 1280 REM SET POIN TE R FOR SPRITE 1
1.290 POKE 2041 , 3.4 1300 REM READ DATA FOR SPRITE 1 1310 FOR I = 0 TO 62 1320 READ N : POKE 896 + I, N
1.330 NEXT I 1340 REM MOON, SPRITE 2
1.350 REM SET POINTER FOR SPRITE 2 1360 POKE 2042, 15 1370 REM READ DATA FOR SPRITE 2 1380 FOR I = 0 TO 62 1390 1400
1.41.0 REM 1420 1430 1440 POKE 1450 1460 POKE
READ N : POKE 960 +
I ,
NEXT I
ENLARGE SPRITES IN
POKE
VC + 29, 5
REM ENLARGE SPRITES IN Y
VC + 23, 5
SELECT COLOUR
REM
VC + 39, 1
N
X DIREC TION
DIRECT ION
.1.470 POKE VC + 40, 2 1480
POKE
VC + 41 , 7
1490 RETURN
1500 REM
1510 REM BLUE SKY
34
Page 47
Chapter 2 Small is Beautiful
1520 POKE 53280, 6 : POKE 53281, 6 1530 PR I NT " t: SHI FT / CL..R / HOME 1 " 1540 REM DRAW GR ASS 1550 C - 5 s REM SELECT GREEN 1560 FOR 1 = 0 TO 160 STEP 40 1570 FOR J = 0 TO 39 .1580 L = 1824 + I + J 1590 GOSUB 2900 1600 NEXT J 1610 NEXT I 1620 REM PLACE ROCKET ON GRASS 1630 REM HORIZO NTAL POSITION .1.640 POKE VC, 165 1650 REM VERTIC AL POSITI ON 1660 POKE VC + 1 , 190 1670 REM SWITCH ON ROCKET, SPRITE 0 1680 POKE VC + 21, 1 1690 PRINT C H R T ( 5 ) 1700 RETURN 1710 REM 1720 REM TEST RO UT INE 1730 S = 0 1740 P = 10 : REM SET PAUSE 1750 X = 14 s Y = 0 : GOSUB 2700 1760 PRINT "<< FIRE WO RK >>" 1770 A = IN T ( R N D ( 1 ) * 96 ) + 5 1780 B = I NT-: RND< 1 ) * A ) .1790 REM PREVENT TOO EASY QU ES TI ONS 1800 IF B < 11 THEN 1770 1810 GOSUB 2940 : REM PAUSE 1828 X = 8 : Y = 2 : GOSUB 2700 1830 PRINT "WHAT IS " A B 1840 X = 30 s Y = 2 ; GOSUB 2700 1850 PRINT " 1860 GOSUB 2700 1870 INPUT C* 1880 IF A S C ( C$)< 49 THEN 1840 1890 IF A S C ( C*> > 57 THEN 1840 1900 C = VAI.. < C$> 1910 IF C = A - B THEN 1950 1920 GOSUB 2700 ; PRINT " NO " A B 1930 GOTO 1770 1940 REM INCREASE SCORE 1950 S = S + 1 IF S < 5 THEN 1770
35
Page 48
Programming for Education on the Commodore 64
.1.960 REM CLEAR TOP OF SCREEN .1970 X « 14 : Y » 0 s GOSUB 2700 1980 PRINT " 1990 X » 8 : Y » 2 s GOSUB 2700
2000 PRIN T " 2018 RETU RN 2020 REM
2030 REM LA UNCH ROCKET
2040 P - 1 s REM ADJUST PAUSE
2050 REM BNITCH ON EXHAUST, SPRIT E 1
2060 POKE VC + 21, 3
2070 SD - 1 : GOSUB 2770 : REM SOUND 2080 FOR I == 190 TO 120 STEP -1 2090 REM KEEP X COORDINA TE CONSTANT 2100 POKE VC, 165 2110 REM A DJUST Y COORDI NATE 2120 POKE VC + 1 , I 2130 REM PLACE EXHAUS T 2140 IF I > 160 THEN 2230 2150 REM E NLARGE EXHAUST 2160 REM ADJUST IX X DIRECTIO N 2170 POKE VC + 23, 7 2180 REM A DJUST IN Y DI RE CTION 2190 POKE VC + 29, 7 2200 REM X COOR DIN ATE FOR LARGE EXHAUS T 2210 POKE VC + 2, 165 2220 GOTO 2240 2230 POKE VC + 2, 177 2240 POKE VC + 3, I + 43 2250 GOSUB 2940 : REM PAUS E 2260 NEXT I 2270 REM GROUND SLIPS AWAY 2280 C = 6 : REM SELECT BLUE 2290 FOR 1 = 0 TO 160 STEP 40 2300 FOR J = 0 TO 39 2310 L = 1824 + I * J 2320 GO SUB 2900 2330 NEXT J 2340 NEXT I 2350 P = 5 s GOSUB 2940 : REM PAUSE 2360 REM BLACK SKY 2370 POKE 53280, 0 : POKE 53281, 0 2380 PR I NT " I! SHI FT / CLR / HOME 1 " 2390 P = 10 : GOSUB .2940 s REM PAUSE
36
Page 49
Chapter2 Small is Beautiful
2400
241.0
2420 2430 2440 2450 2460 2470 2480 2490
2500 2510 2520 2530 2540 2550 2560 2570 2580 2590
2600 2610 2620 2630 2640 2650 2660 2670 2680 2690 2700
2710
2720 2730
2740 2750 2760 2770 2780 2790 2800 2810 2820
REM MOON ARREARS POKE VC + 21, 7 REM MOVE MOOM DOWN SCRE EN P = .5 s REM ADJUST PAUSE FOR I «
8 TO 250
POKE VC + 4, 30 POKE VC + 5, I (30SUB 2940 : REM PAUSE NEXT I. P = 5 : (30SUB SD = 0 s GQSUB
940 : REM PAUSE
2770 : REM SOUND REM STARBURST REM SWITCH OFF
ROCKET, SPRITE 0
POKE VC -I- 21, 0
FOR I = 1 TO 8
L = 1524 + I : L. * 1524 - I s L 1524 -i- I * L --- 1524 - I *
I.... = 1524 + I +
L ~ 1524 + I - L. = 1524 I + L = 1524 - I
GOSUB 2980
GOSUB 2980
40 ; GOSUB 2980
40 s GOSUB 2980
I * 40 s GOSUB 2980 I * 40 : GOSUB 2980
I * 40 : GOSUB 2980
I * 40 : GQSUB 2980 NEXT I REM LIGHT UP SK
Y
POKE 53280, 3 : POKE 53281, 3 P -- 50 : GOSU B 2940 : REM PAUSE RETURN REM REM PRINT TAB RO UT INE POKE 782, X s POKE 781, Y SYS 65520 RETURN REM REM SOUND EFFECT REM SD = 1, SOUND ON
REM SD = 0, SO UND OFF
IF SD = 0 THEN 2840 POKE 54296, 15 POKE 54277, 129 POKE 54278, 129 POKE 54276, 129 POKE 54273, 10
POKE 54272,
60
37
Page 50
Programming for Education on the Commodore 64
2830 GOTO 2870 2840 POKE 54277, 0 2850 POKE 54278, 0 2880 POKE 54276, 0 2870 RETURN
2880 REM 2890 REM PLOT CHAR ACTER SPACE 2900 POKE L + 54272, C ; POKE L, 160
2910 RE TU RN 2920 REM 2930 REM PAUSE
2940 FOR T = 1 TO P * 30 : NEXT T 2950 RETURN
2960 REM
2970 REM PLOT STAR
2980 C = IN T ( RND < 1 ) * 7 ) + 1
2990 POKE L + 54272, C : POKE L, 42
3000 RE TU RN
3010 REM
3020 REM DATA FOR ROCKET, SPRITE 0
3030 REM
3040 DATA 0, 8 , 0, 0, 28, 0, 0, 62, 0
3050 DATA 0, 8 , 0, 0, 28, 0, 0, 62, 0
3060 REM
3070
3080
3090
3100
3110
3 1 2 0
3130
3140
3150
3160
3170
3180
3190
3200
3210
3220
REM DATA FOR EXHAUST, SPRITE 1 REM DATA 0, 8, 0, 0, 8 , 0, 0, 24, 0 DATA 0, 60, 0, 0, 118, 0, 0, 231 DATA 0, 1, 227, 128, 1, 225, 192 DATA 1, 225, 224, 0, 241, 224, 0
DATA 249, 240, 0, 125, 240, 0, 29 DATA 240, 0, 15, 224, 0, 7, 192
DATA 0, 7, 128, 0, 7, 128, 0, 7, 0 DATA 0, 14, 0, 0, 12, 0, 0, 16, 0 REM REM DATA FOR MOON, SPRITE 2 REM DATA 0, 30, 0, 0, 7, 128, 0, 3 DATA 224, 0, 1, 240, 0, 0, 248, 0 DATA 0, 252, 0, 0, 126, 0, 0, 126 DATA 0, 0, 127, 0, 0, 127, 0, 0
3240
3250
DATA 127, 0, 0, 127, 0, 0, 127, DATA 0, 126, 0, 0, 126, 0, 0, 25
f-0 Q
38
Page 51
Chapter 2 Small is Beautiful
3260 DATA 0, 0, 248, 0, 1, 240, 0, 3 3270 DATA 224, 0, 7, 128, 0, 30, 0
READ Y .
Commentary
The control module for Firework is formed by the lines 1020-1060. It can be seen that the program is in an overall loop and so RUN/STOP has to be
used to escape from it. The first routine of the program defines three sprites. First the value of VC
is initialised by line 1110. Then the rocket, its exhaust and the Moon are defined in turn as sprites 0-2 . The three diagrams show each of the sprites in detail. It can be seen that the major part of the rocket is a very repetitive
shape and this pattern is reflected in the way that the corresponding sprite data is built up by the use of the F O R .. .NEXT loop from lines
1190-1230. The other two sprites are built up instead simply from the
DATA stored at the end of the program. Lines 1410-1480 then select the size and colour of the sprites.
Lines 1510-1690 colour the screen blue and place grass at the bottom. The rocket is located on the grass by POKEing the two locations VC and V C +1, which control its horizontal and vertical location on the screen. Finally the rocket is made to appear by POKEing its sprite number, 1, to VC+ 21. This is the location which controls whether a particular sprite appears on the screen or not. Line 1690 selects white text for the subsequent test routine.
The test given to the child is simple subtraction of numbers. Line 1770 chooses A to be a number less than 100. Line 1780 then multiplies A by a random decimal number and takes the integral part. In this way, it is ensured that the number being subtracted is not larger than the one it is being subtracted from. Line 1800 then prevents the calculations from being too easy. Again extensive use is made of a PRINT TAB subroutine to allow the screen to be neatly arranged. Lines 1840-1890 stop letters being accepted as an input. The variable S keeps a record of the childs success and governs exit from the test to the reward routine at line 1950.
Lines 2030-2670 control the flight of the rocket up into the sky. Here the sprites allow easy animation. An increased sense of upward motion is attempted by removing the ground between lines 2280-2340 and by moving the Moon downwards in the loop 24402480. The sound of the
rocket is created by the separate routine between lines 2740-2870 and con
trolled by the value of the flag SD. The loop from 2540-2630 uses another
subroutine to place a starburst on the screen.
39
Page 52
Programming for Education on the Commodore 64
40
Page 53
Chapter 2 Small is Beautiful
41
Page 54
Programming for Education on the Commodore 64
Diagram D
42
Page 55
Chapter 2 Small is Beautiful
Fraction
There is no doubt that in the whole of maths teaching, there is no topic so
off-putting to children, parents and teachers alike as that of fractions.
Children of seven and eight often have an intuitive grasp of halves and
quarters, but the notation of vulgar fractions is unfamiliar and the terms
numerator and denominator are awkward in themselves. Recent surveys, such as the Cockroft Report (1982), have implied that work should concen trate on decimal fractions at least until the upper end of primary schools.
This does not mean that the idea of fractions can go out of the window. Children need a lot of practical experience in dividing objects up into equal-sized parts before they understand fractional concepts. It is usually in the recognition of what a fraction stands for, and in the manipulation of its parts, that problems occur. In this chapter, we have attempted to tackle this problem in different ways.
Fraction displays a rectangle, representing the whole object; it then colours in a particular fraction and asks the child to input the numerator and denominator. These terms are used on the screen and there is no doubt about which is which.
As an aid to knowing how to express the fraction, ie whether j or { is required, the whole rectangle is divided up into the same number of parts as the denominator. For instance, if \ is required, the rectangle divides into six clearly-defined sections, and two of these are coloured in. Should the child make an incorrect input, there is a routine to show how the fraction is built up. If the child is successful, the examples become progressively more dif ficult, whereas those for a child with difficulties remain simple.
.1.000 REM FRACT ION
1010 REM
1020 GOSUB 1200 :
1030 GOSIJB 1170 : 1040 GOSUB 1360 : 1050 GOSUB 2010 s 1060 GOTO 1030 1070 END 1080 REM 1090
1100
1110
1120
.1.130 1140 1150 1160
REM INITIALISATION
REM SCORE AND NUMBER AT TEMPTED S = 0 : G - 0
REM LEVEL OF DIFFICULTY DF = 1 RETURN REM REM SELECT FR AC TION
- PAT HALL, JAN '84
REM INITIALISATION REM SELECT F RACTI ON REM DRAW D IAGRAM REM REQUEST ANSWER
43
Page 56
Programming for Education on the Commodore 64
I N K RNIK 1 )
1170 1180 N = 1190 IF
1200 1210 W « 1220
1230 IF 1240 IF D < 8 THEN W = 1250 1260 IF 1270 1280
1290 IF D < 3 THEN W - 1300 REM' 1310 LD 1320 RETURN 1330 REM 1340 REM DRAW DIAGRAM 1350 REM WHIT E SCRE EN 1360 POKE 53280, 1 : POKE 53281, 1 1370 PRINT "CSHIFT/CLR/HOME'J" 1380 REM PRINT TITLE 1390 PRINT C H R T ( 144 ) 1400 X = 13 : Y = 0 s'GOSUB 1950 1410 PRINT " « FRACTI ON >>" 1420 PRINT CHR$< 30 ) 1430 X = 12 : Y = 2. : GOSUB 1950
1440 PRINT "SCORE: " S " / " G 1450 PRINT C H R T ( 144 ) 1460 REM DRAW D DIVISI ONS 1470 FOR I = 1 TO D 1480 REM LOCATE TOP LEFT OF DIVISION 1490 LD = LD + W + 1 1500 REM PLOT TOP AND BOTTOM OUTLINE 1510 C = 0 : REM SELE CT BLACK 1520 FOR J = 0 TO W 1530 L = LD + J 1540 GOSUB 1920 1550 L - LD + 560 + J 1560 GOSUB 1920 1570 NEXT J 1580 REM SOUND EFFECT 1590 POKE 54296, 15 : POKE 54277, 129 1600 POKE 54278, 129 : POKE 54276, 17
D =
INT < R N D ( 1 )
N < 1 THEN 1180
REM1 SELECT WIDTH
1
D < 13 THEN W
IF
D < 10 THEN W
IF
D < 7 THEN W =-- D < 6 THEN W =
IF
D < 5 THEN W = D < 4 THEN W =
IF
1 LOC ATE TOP LEFT OF
« 1203 - D * (
* DF * D )
OF DI VISIONS
_
= 3
4 5
'tf
8
11
17
W + 1
) + 2
DIAGRAM
) / 2
44
Page 57
Chapter 2 Small is Beautiful
1610 POKE 5 4 2 7 3 , 68 : POKE 54272, 149 1620 FOR T = 1 TO 90 : NEXT T 1630 POKE 54277, 0 r. POKE 54278, 0 1640 POKE 54276, 0 1650 REM LEFT HAND EDGE 1660 FOR J = 40 TO 520 STEP 40 1670 L = LD + J
1680 GOSUB 1920 1690 NEXT J 1700 REM COLOUR IN DIVISION 1710 REM COL OUR NU MERATOR GR EE N
1720 C = 5
1730 REM COLOUR OTHER DIVISION S YELLOW
1740 IF I > N THEN C 7
1750 FOR J - 40 TO 520 STEP 40
1760 FOR K. = 1 TO W
1770 L = LD + J + K
1780 GOSUB 1920
1790 NEXT K.
1800 NEXT J
1810 NEXT I
1820 REM FINAL RIGHT HAND EDGE
.1830 C = 0 ; REM SELECT BLACK
1840 LD = LD + w + 1
1850 FOR I = 0 TO 560 STEP 40
1860 1... = LD + I
1870 GOSUB 1920
1880 NEXT I
1890 RETURN
1900 REM
1910 REM PLOT CHARACTE R SPACE
1920 POKE L + 54272, C : POKE L, 160
1930 RETURN
1940 REM PRINT TAB ROUTIN E
1950 POKE 782, X : POKE 781, Y
1960 SYS 65520
1970 RETURN
1980 REM
1990 REM REQUEST ANSWER 2000 REM INCREMENT NUMBER ATTEMPT ED
2 0 1 0 G = G + 1
2020 REM REQUEST ANSWER 2030 X = 8 s Y = 20 : GOSUB 1950
2040 PRINT "WHAT FRACT ION IS GREEN ?"
45
Page 58
Programming for Education on the Commodore 64
2050 Y = 22 : G OSUB 1950 2060 PRINT "TYPE NUMERATOR;-"
2070 XA = 22 : GOSUB 2200
2080 Ml = AN
2090 X = 8 s Y ~ 23 ; GOSUB 1950
2100 PR INI...f'YPE DENOMINATOR; ~"
2110 XA » 24 : GOSUB 2200 2120 D 1 ~ AN
2130 REM CHECK ANSWER 2140 IF N1=N AND D1«D THEN GOSUB 2330 2150 IF N I O N OR D I O D THEN GOSUB 2500 2160 FOR T = 1 TO 2000 ; NEXT T 2170 RETU RN 2180 REM
2.190 REM ACCEPT ANSWER 2200 X - XA 2210 GOSUB 1950 2220 PRINT " 2230 GOSUB 1950 2240 INPUT ANT 2250 REM REJECT NON NUMERICAL.. VALUES 2260 IF A S C ( ANT) < 48 THEN 2200 2270 IF A S C ( ANT) > 57 THEN 2200
22.80 AN = VAl... ( ANT ) 2290 RETURN 2300 REM 2310 REM REWARD ROUTINE 2320 REM GREE N SCREEN 2330 POKE 53280, 5 ; POKE 53281, 5 2340 PR I NT " C SHI FT /CLP / HOME II " 2350 REM INCREASE SCORE 2360 S = S + 1 2370 REM INCREASE DI FFICULTY 2380 DF - DF + 1 2390 IF DF > 17 THEN DF = 17
2400 PRINT C H R T ( 5 )
2410 FOR I = 1 TO 11
2420 PRINT "WELL DONE. THAT IS THE " ;
2430 PRINT "CORRECT ANSWER." 2440 PRINT 2450 NEXT I 2460 RETURN 2470 REM .2480 REM CORRECTION ROUTINE
46
Page 59
Chapter 2 Small is Beautiful
2490 REM YELLOW SCREEN 2500 POKE 53280, 7 : POKE 53281, 7 2510 PRINT H I! SHI FT/CL P/HOME 3" 2520 REM DECR EASE DIFFICULTY 2530 DF * DF - 1 2540 IF DF < 1 THEN DF = 1 2550 FOR I = 1 TO 11 2560 PRINT " NO. THE CORRECT "5 2570 PRINT "ANSWER IS " N "/" D 2580 PRINT 2590 NEXT I 2600 RETURN
REA D Y .
Commentary
The control module for Fraction lies between 1020-1060, which form a loop calling up initialisation, selection, drawing and testing routines. The program does not come to an end unless RUN/STOP is pressed.
Lines 1090-1140 are the initialisation routine. The variables S and G are set equal to zero at line 1110. They record the score the child has achieved and the number of fractions attempted. The variable DF is initialised by line 1130. DF controls how hard each fraction is. The smaller the value of
DF, the smaller the numerator and denominator of the fraction drawn on the screen. The value of DF is continuously monitored during the execu tion of the program, so that when the child types in a correct answer the level of difficulty increases. If an answer typed in is incorrect, the value of
DF is decreased so that the fractions become easier. It is not quite this simple, as a random factor is introduced as well, but DF does determine the overall trend towards harder or easier examples.
The fraction is generated between lines 1160-1320. The denominator, D, is selected at line 1170. DF enters into the expression as the number that the random fraction RND(l) is multiplied by. Line 1180 relates the size of the numerator, N, to that of D. Obviously, the way in which the two numbers are generated ensures that the numerator is smaller than the denominator, as is necessary.
Lines 1210-1290 relate the value of the variable W to that of D. W is used in the following routine when the diagram of the fraction is drawn on the screen. Here, however, it is adjusted to give the largest possible diagram
for the fraction chosen. Finally, this routine calculates a value for the
47
Page 60
Programming for Education on the Commodore 64
variable LD in terms of W and D. LD allows the subsequent calculation of the top lefthand corner of each division of the fraction.
Lines 1340-1890 form the routine which places the fractional diagram on the screen. It is built up quite naturally in a series of blocks or divisions corresponding in number to the size of the denominator.
After the screen has been cleared by line 1370 and the title and other headings placed by lines 1380-1440, a lengthy FOR. .. NEXT loop, from lines 1470-1810, constructs each division of the diagram in turn. At the beginning of this loop, a new value is first calculated for LD for the current division. The J loop between lines 1520-1570 draws the top and bottom of the division in black. A subroutine at lines 1910-1930 is used by this loop, and also for all successive plotting on the screen. This is followed by a brief sound effect to help draw attention to the process of building up the dia gram and hence to aid the childs counting. The sound is generated by the various POKEs between lines 1590-1640. After this, the lefthand edge of the division is plotted by the loop between 1650-1690.
So far, only three sides of the division have been drawn. The enclosed area is coloured in by the two nested FOR. .. NEXT loops from
1750-1800. The colour the area is plotted in is chosen at lines 1710-1740. The numerator of the fraction appears in green and the remaining divisions in yellow. Line 1810 ends the I FO R.. .NEXT loop.
After the D divisions are drawn, there is still the extreme righthand edge of the diagram to be filled in. The loop between lines 1850-1880 does this. Note how the same subroutine has been repeatedly called during the whole of the construction of the diagram.
Lines 1990-2160 make extensive use of the PRINT TAB routine between
1940-1970 to format the presentation of the question on the screen. Another subroutine at lines 2190-2290 checks that acceptable values are typed in. Lines 2140 and 2150 then select either reward or correction
routines at the end of the program. It is here that the value of DF is adjusted to correspond to the degree of success or failure the child is experiencing. Success will lead fairly quickly to harder examples, whereas constant
failure would lead to the child using the program being shown a diagram of
7 over and over again, together with the correction routine. Although it is
quite simple to arrange for programs to be self-adjusting to the childs level of ability, nevertheless such an arrangement allows far more effective learning programs to be constructed.
48
Page 61
CHAPTER 3
Words, Words, Words
Handling words on a computer sorting things Alphasort: a
program to sort words into alphabetical order numbers in strings
Simon: a game to improve memory diversity o f subjects in
primary schools problems in spelling different approaches
context clues Hangman: a multi-purpose spelling program research skills Anagram: clues from word order.
So far in the book, the majority of programs have involved some sort of
maths skills. The 64 contains many facilities that make the handling of
words much easier. As has been mentioned before, computers are just as
happy manipulating letters as figures. Numbers are stored as numeric
variables, such as X or COUNT, and strings of characters as string varia
bles, such as A$ or BOOKS. Each character in the 64 has a code number that
refers to it, so 65 stands for A, and 90 stands for Z. You can check this out
yourself by entering this short program.
10 FORN = 65 TO 255 20 PRINT CHR$(N) 30 NEXTN
The function CHR$ converts the Commodore code to its character equivalent, so you will see the alphabet printed out if you RUN the pro gram. (This will be in upper case if you are in the normal mode after switch ing on, or in lower case if you have changed modes by pressing SHIFT and the Commodore key [C = ] at the same time.)
The function that does the reverse of CHR$ is ASC. ASC is short for ASCII, the initials of the American Standard Code for Information Inter change. Although the 64 has its own codes for graphics symbols, it follows ASCII codes for letters and numbers. This program shows you how ASC works:
10 PRINT PRESS A KEY 20 POKE 198,0 30 GET Z$:IF Z$ = THEN 30
Page 62
Programming for Education on the Commodore 64
40 C = ASC(Z$)
50 PRINT C 60 GOTO 10
Commentary
Line 20 clears the keyboard buffer (a temporary memory store for key board input).
Line 30 reads the keyboard and repeats until a key is pressed. Note that there is nothing between the two quote marks. This is called an empty string.
Line 40 assigns the ASCII code of the key pressed to the variable C.
Lines 50 and 60 print the code and send the program back to the start.
If you run this program, you will discover many useful things. Apart from pressing letter keys, try pressing number keys, the arrowed cursor keys, and the function keys. You will find that all the keys generate codes except
[RESTORE], [CTRL], [SHIFT] and [SHIFT LOCK], [C = ] and
[RUN/STOP] on their own. Many keys, such as A, have three codes, depending on whether you are pressing [SHIFT] or [C = ] at the same time.
The key A normally has a code of 65, but it produces 193 with [SHIFT] and 176 if [C = ] is also held down. This is useful in programs, as you can check which keys are being pressed at any time.
If you have a program that requires a word or phrase to be input into a
string, the length of the string can be checked by using the function LEN. If
you enter directly
X$ = HELLO
then
PRINT LEN(X$)
the result 5 will appear, as HELLO has five characters.
Other string functions are RIGHTS, LEFTS, and MIDS. RIGHTS
returns the righthand portion of a string, LEFTS the lefthand portion, and
MIDS can extract a portion from anywhere in the middle. To see how they operate, try this:
10 AS = ABCDEFGHIJ 20 PRINT RIGHT$(A$,4)
50
Page 63
Chapter 3 Words, Words, Words
30 PRINT LEFT$(A$,4) 40 PRINT MID$(A$,2,6)
If you RUN this, it will print GHIJ in line 20, as these letters are the last four letters on the right of the string. ABCD will be printed out in line 30, as they are the first four letters on the left of the string. Line 40 will print out BCDEFG, as these are the six letters starting from the second character in the string.
This program will show you how the 64 can search through strings using these functions:
10 PRINT ENTER A WORD 20 INPUT A$ 30 IF LEN(A$)< 3 THEN PRINT TOO SHORT :GOTO 10 40 PRINT A$ IS LEN(A$) CHARACTERS LONG 50 PRINT IT STARTS WITH LEFT$(A$,1) 60 PRINT AND ENDS WITH RIGHT$(A$,1) 70 PRINT AND CONTAINS MID$(A$,2,LEN(A$)-2); 80 PRINT IN THE MIDDLE
Because the 64 gives all the keyboard characters numbers, it is possible to sort words into alphabetical order according to their ASCII codes. This program prints out a list of the words in the order in which they were input, then sorts them and prints them in their new order. Although 20 words can be sorted in about four seconds, 100 take about a minute and a half, as the number of individual comparisons grows. Enter line 245 S = S + 1, and
PRINT S at the end, if you want to see how many comparisons were made between words.
Alphasort
100 REM *** ALPHASORT *** 110 REM 120 REM *** AJS 5/8/83 *** 130 REM 140 REM 150 INPUT "NUMBER OF WORDS";N 160 DIM A*<N+1> 170 FOR X = 0 TO N ~ 1 180 INPUT A*(X> 190 NEXT X
51
Page 64
Programming for Education on the Commodore 64
EOR X
20 0 21 0 PRINT At (X ) 22 0 NEXT X
FOR K = 0 TO N -1
230 240
FOR L
250
IF A#
= 0 TO
= K+l
(L) >=
N 1
TO N
At <K) 260 Ut = At (L) 270 At <L) = At (K 280
At (K) = Lit
)
290 NEXT L
300 NEXT
FOR X = 0 TO N
310
K
320 PR INI- At (X )
330 NEXT
R E A D Y .
Commentary
There are one or two new ideas in this program. A sorting routine needs to hold the words, not in one variable, but in an array. This has to be DIMen- sioned so that some space in memory is reserved for the words that will be stored in it.
Line 160 reserves space according to the number of words to be sorted.
Lines 160 to 220 print out the words to be sorted.
Lines 230 to 300 contain the sorting routine. There are two loops, K and L, and the ASCII codes of the words are compared in line 250. If a word has a
higher code than the one after it, they are swopped in the list. The loop
compares all the words against each other until they are in alphabetical order.
Lines 310 to 330 print out the new list.
Strings and things
As has been mentioned previously, string variables can contain any key board character, not just letters. Although you can redefine characters to be any shape you like and use them in your programs, all Commodore computers contain graphics characters that can be produced by the key board. Your 64 contains two character sets, but only one can be used at any one time. When it is switched on, the 64 is in upper case mode. As you press keys, upper case characters are produced. Pressing the [SHIFT] key and a letter key gives you the graphic character printed to the right on the front of
52
Page 65
Chapter 3 Words, Words, Words
the key. Pressing the [C = ] key and a letter key gives you the graphic char acter printed to the left on the front of the key.
If you hold down the [SHIFT] and [C = ] keys at the same time, you can switch between upper and lower case modes. Lower case mode is a little like using a typewriter: pressing a letter key gives you the lower case version, and pressing [SHIFT] with the letter gives the upper case version of that letter. Pressing the [C = ] key and a letter key still gives you the graphic character printed to the left on the front of the key.
You can switch to upper case mode in a program by including PRINT CHR$(142) in a line. To switch to lower case mode, use PRINT CHR$(14).
The graphics characters can be used to draw pictures on the screen, by using quotes to surround the graphics, just as if you were printing words.
Numbers in strings
You have probably seen that there are two ways to print 123 on the screen. You can enter
PRINT 123
or
PRINT 123
Although you see the same figures appearing on the screen, it is important to realise the difference that the 64 sees. 123 is seen as a number, with the value 123. 123 is seen merely as a string of characters. To see the differ ence, try this as a direct command:
PRINT 123 + 123
You should get 246 as the answer, because the computer understands these to be numbers. If you enter:
PRINT 123 + 123
you will get 123123, because the 64 treats them as strings.
Although they are completely different, numbers can be turned into strings and vice versa. To turn 123 into a string, you can use the STR$ function, eg
A$ = STR$(123)
53
Page 66
Programming for Education on the Commodore 64
The reverse of this is achieved by using the VAL function. Even though strings cant normally be considered to be numbers, they do have a value.
PRINT VAL( 123) + 1
will print 124. If the first character in the string is not a -I- or a - or a figure, the value is zero.
Simon
In this program, the fact that strings can be concatenated (added together), is used as the basis for a memory game. A string contains the STR$ version of numbers chosen at random from one to eight, and the object is to remember their order. The length of the string increases by one each time a correct answer is entered.
The game of Simon is usually played with colours and sounds as extra memory aids. The 64 stores the code of the colour in which text is printed at location 646 in the memory. If this is POKEd with numbers from 0 to 15, the sixteen available colours can be selected.
The number keys on the 64 have colours printed on them, BLK, WHT, etc. The numbers on the keys are one higher than the code to be POKEd to 646, hence the line that subtracts one from the value of the string.
The sound commands are a little more complicated. They are explained in more detail in the program notes.
10 REM ***
SIMON ***
20 REM 30 REM ***
AJS 5/6/83 *** 40 REM 50 REM
60 REM ***
INSTRU CT IONS *** 70 REM 80 PRINT C H R * (147):P 0 K E 5 3 2 8 1,0 90 PRINT
100 PRINT" THIS IS A GAME OF MEMORY" 110 PRINT
120 PRINT" YOU WILL BE SHOWN A SERIES" 130 PRINT 140 PRINT" OF FIGURES. THESE FIGURES" 150 PRINT 160 PRINT" WILL SOON DISAPPEAR AND YOU"
54
Page 67
Chapter 3 Words, Words, Words
.170
PRINT
180
PRINT" MUST TYPE THEM IN, IN THE"
190
PRINT
200
PRINT" RIGHT ORDER. THE NUMBE R OF" PRINT
210
220
PRINT" FIGURES WILL INCREASE BY ONE"
230
PRINT
240
PRINT" EACH TIME."
250
PRINT
260
PRINT" PRESS SPACE BAR TO GO"
270
GET Z*:IF Z$="" THEN 270
280
PRIN T C H R*(147):P 0KE53281,12
290
REM
300
REM *** MAIN GAME LOOP ***
310
REM
320
FOR Z= 1 TO 10
X-INT(RND(0)*8)+1
330 340
X*=S T R * (X ):X $ = MID* < X * ,2,1)
Y4---YT+XT
350 360
60SUB 490: P O KE 198,0 370 380 390 400 410 420 430 440 450 460 470 480 490 500
INPUT"ENTER YOUR G U E S S " ;G* IF G$ = Y* THEN S=S+1
IF G* < >Y* THEN Z = 11:GOSUB630 NEXT Z REM REM *** SCORE ROUTINE *** REM PRIN T"YOU SCORED" S PRINT" PRESS ANY KEY TO GO AGAIN" GET Z*:IF Z#=""THEN 460 RUN REM REM *** COLOU R PRINT SUBROUTI NE *** REM
510 PRINT 520 FOR N = 1 TO LEN(Y$>
530 A$= M I D$(Y$ , N ,1):V = VAL<A*) 540 POKE 646,V-Is REM CO LOUR VA LU E 550 PRINT A*;" "; 560 GOSUB 700 570 NEXT N 580 PRINT 590 FOR F'AUSE= 1 TO 1000: NEXT PAUSE 600 P R I NTC H R * (147):P0KE 6 4 6 ,1
55
Page 68
Programming for Education on the Commodore 64
610 RETURN
62.0 REN 630 REN *** FAULT SUBROUTI NE *** 640 REN 650 PR I N T"NISTAKE!! IT WAS "Y$ 660 RETURN 700 REN 710 REN *** SOUND SUBROUT INE *** 720 REN 730 SC 54272 740 POKE SC-i-24,15: REN VOLUME 750 POKE S C +4,33 760 POKE SG+5,10s POKE SC+6,4 770 V=V*2 000 780 HIGH = INT(V/256)SLOW = V-HIGH*2 56 790 POKE S C + 1,HIGH sPOKE S C ,LOW 800 FOR PAUSE = 1 TO 400s NEXT PAUSE 810 PO KESC +4,0 820 R ETURN
REA D Y -
Commentary
Line 80 shows another way of clearing the screen PRINT CHR$(147). The second statement sets the background colour to black.
Lines 100 to 260 contain the instructions.
Line 270 waits until any key is pressed. Line 280 clears the screen and sets the background colour to grey.
Lines 320 to 400 form the loop which provides 10 figures.
Line 330 chooses a number at random between 1 and 8. Line 340 turns the number into a string, and the second statement strips away the first char acter, a space. Line 350 adds the new figure to the existing string.
Line 360 sends program control to the print routine and clears the key board buffer to prevent cheating!
Line 380 checks the input against the string and increments the score by one. Line 390 ends the Z loop if the answer is incorrect, and sends the program to the fault subroutine.
56
Page 69
Chapter 3 Words, Words, Words
Lines 440 to 470 print the score and start the program running again.
Lines 520 to 570 print the separate figures in the string after their colour codes have been POKEd into memory location 646. Line 550 prints a space between each figure.
Lines 590 and 600 provide a delay of about two seconds before the string disappears.
Line 650 contains the fault subroutine.
Line 730 sets SC equal to the starting address of the 6581 SID sound chip.
This makes all the subsequent addressing of locations in this chip easier to
follow.
Line 740 sets the volume to maximum. Line 750 sets the total quality of
voice 1 to a sawtooth waveform. Line 760 sets the envelope of voice 1
(attack/decay and sustain/release).
Line 770 sets the frequency of the note by multiplying the value of the figure in the string (V), by 2000. The SID chip is capable of producing notes of a frequency that can be stored in two bytes. The highest value that could be stored is therefore 256*256 or 65536. Line 780 calculates what should be POKEd into this two-byte frequency store to produce the required note.
Line 800 controls the length of the notes. Line 810 sets the waveform con trol register to zero.
Diversity of subject areas
There are many areas of English teaching in which the computer can prove a useful aid. Because of its fast sorting ability and accurate input check, it can be used to help in spelling, alphabetical ordering, punctuation and grammar in general.
It seems a little unfair to divide up the chapters in this book into the traditional subject categories of maths, English, geography, etc, as most teachers in primary schools would agree that such divisions are hard to make. A day spent in looking at a local shopping centre, for instance, might cover such areas as maths surveys of people entering each shop; geography the countries that provide the goods; history how the area has changed over the years; art drawing the shops; English writing about the area, etc.
Such a topic-based approach is closer to the childs experience of the
world, and subject divisions would be artificial. Certain basic skills still
57
Page 70
Programming for Education on the Commodore 64
need to be taught, however, and this book would lack any structure if the programs were not divided up in some manner.
It is with this in mind that the programs in this chapter have been
included. They attempt to help the teacher or parent instil basic spelling and comprehension skills.
Hangman
Some children are lucky and have no difficulty in spelling. Others attempt a phonetic approximation, and yet others never seem to grasp the idea. Although poor spelling often goes hand-in-hand with poor reading skills, there are many good readers who reach adulthood and still suffer from difficulties with their spelling. Traditional methods of learning words by heart for a weekly test are no bad thing if the words are relevant to the childs immediate situation, eg if they come from a project or topic that is being studied. If the child uses the words in their proper context, the spell ing is reinforced.
There are two approaches here one is to show the context of the word
and to offer a multiple choice of possible spellings. The other approach is to build up the word slowly, with spaces for missing letters. The game of hangman is an ideal vehicle for this approach. The program that follows
builds up a picture of lependu, as in the traditional game, and 10 guesses are allowed before the victim is finally throttled.
Words to be selected are held in DATA lines, so there is no problem in
changing these to words relevant to a topic, or to those displaying a parti cular spelling point, such as ie and ei words. You could even use the program to help learn foreign words. The only requirement is that the total
number of words should not change, as the READ statement needs 20 words. If you want a larger selection, then the number may be altered as detailed in the program notes.
1000 RE M ** * HA N GM AN ***
1010 REM
1020 RE M ** * A JS 2/ 8/8 3 ***
1030 REM
1040 P0K E5 3281, 1s P0KE6 46 ,0 1050 GOSUB6000:REM*** INITIALISATION *** 1060 GOBU B7 000 :REM*** INSTRUCTIONS *** 1070 GOSU B2000:REM*** GAME *** 1080 GOTO 1070
200 0 R EM *** GA ME S U BR O UT INE *** 200 5 REM
58
Page 71
Chapter 3 Words, Words, Words
201 0 R E ST OR E 202 0 F OR N= 1TO I N T ( R N D<0 )* 20 ) + 1 203 0 R EA D W$ 204 0 N EX T 206 0 P RIN T"" 207 0 X ~ 5: Y = 15 : 6OS U B40 0 0 208 0 F O RN =l TO LE N< W$ ) 2090 P RIN T"- "; 210 0 N EX T N 212 0 S C =0 :Z= 0 213 0 X=5: Y=20s GO S UB 4 00 0 214 0 G $ = " ":P R INT " YOU R GUESS";
214 5 P O K E 1 98,0 215 0 IN PU TG*: FL AG =1 215 5 IF G$= W$ TH EN G OS U B 30 0 0 :RE T U RN 216 0 L N = L E N < W$) 217 0 IF L E N (G $ )< >LN A ND LE N< 6* >> 1 TH EN X=1 5sY=20s
GO SUB 4 00 0
218 0 IF L E N ( G $ ) O L N A ND LEN <G $ ) > 1 T HE N PRIN T"
219 0 IF LE N < G # ) < >LN AN D LJEN<G$>>1 TH EN GO TO 2130 220 0 IF L EN< G * ) = 1 THEN X=Z : Y= 2 3: G OSU B 40 0 0
2 2 1 0 IF L.EN <G* )=1 THENPRI NTG *
222 0 Z = Z-M. 223 0 F OR N = 1 TO LN 224 0 IF G $= M ID * ( W$ , N ,1 ) TH E N G O SU B 2 50 0 224 5 IF6 f=MIDt- (W# , N , 1) T HE N F LAG =0 225 0 N EX T N
226 0 IF FL AG < >0 T HE N SC=SC,+1: GOS U B2 6 00
227 0 X =24:Y=0s GO SU B40 00
228 0 I F S O 0 T H E N P R INT " LIV ES LEF T - >" 1 0- S C
229 0 IF SC C10 THE N G OT O 213 0
230 0 Y~24s GOS U B4 000
230 5 R EM *** WOR D N OT G UE SS ED ***
231 0 FOR N = 1 TO 1 0sPR I N T " H E L P !! !! "s PR I NT :N EXT 232 0 PR INT "T H E WO RD WA S "W* 233 0 P R IN T 233 5 P O K E 198,0 234 0 P R IN T " PR E S S THE SPACE BAR TO GO" 2350 GET Z$:IF Zt="" TH EN 23 50 2360 RE TU RN 237 0 R EM
250 0 R EM *** PRI N TIN G COR RE CT L E TTE RS *** 250 5 REM 251 0 X =4+ N :Y= 1 5s G O SU B 400 0 252 0 P RIN TG $ 259 0 RE TU RN 259 5 R EM 260 0 REM ** * H AN G ING S U BR O UT I NE ** *
it
59
Page 72
Programming for Education on the Commodore 64
605. REM
£>10 P0KE646 , (S C ) ANDSC >2: REM COLOURPRINT £>20 X=38: REM *** UPRIGHT ***
630 IFSC=1 T HE N F O R Y - 19T03 S T E P-1sG OSUB 4000:
PR I NT C HR * (166) -.NEXT
640 Y=2: REJM *** CRO S SP I EC E *** 650 I FSC=2 T HE N F 0R X= 29T03 8: GOSUB4000-. PR IN T CH RS
(166) -.NEXT
660 X «29:REM ** * ROP E *** 670 IF SC=3 T HE N F 0 R Y= 3 T 05 : GOS U B 40 0 0 :PR I N TCH R *
(180) -.NEXT 720 X=27: Y=5-.GOSUB4000-.REM F AC E * 730 IF S C =4 T HE N P R I N T C H R * (100 )C H R * (101); 740 IF SC=4 T HE N P R I N T C H R * (102)C H R * (103) 745 X=27s Y=6 s GO S UB4 00 0 750 IF SC=4 T HE N P R I N T C H R $ (104)C H R $ (104); 760 IF SC= 4 T HE N P R I N T C H R S (106)C H R S (107) 765 X =27 s Y = 7 : GOSLIB4000 770 IF SC=4 TH EN PRINTCHR-fc(1 08)C H R $ (109); 780 IF S C=4 T HEN P R I N T C H R * (110 )CHR$(111 ) 785 X = 2 7 :Y=8s GOS U B4 0 0 0 790 IF S C=4 T HE N PR I NTC HR* (112) CHR * (113);
ro ro ro ro ro ro ro ro ro ro ro ro ro ro ro w ro ro w
10
800 IF S C=4 T HE N P R I N T C H R $ (114 )C H R $ (115)
10
815 X= 28 :R EM ** * B ODY *** 820 I F S C = 5 TH E N F 0R Y = 9T0 13 s GO SU B 40 0 0 s PRIN TCH R*
(166) C H R $ ( 16 6) sNEXT 825 X=s28s REM ** * L .A RM *** 830 IFSC = 6 THEN F 0 R Y = 9 T 013: X = X - 1:GO SUB 400 0:
PR I NT C H R* ( 1 69 > sN E X T 835 X=29s RE M *** R.AR M *** 840 IF SC= 7 T HE N F 0RY =9 T013: X = X + 1: GOSUB4000-.
PR I N TC HR $ (127) -.NEXT 845 X= 29 :R EM ** * L .L EG ***
850 IFSC=8 THEN F0R Y= 14 T0 18:X=X-1:GOSUB4000:
PR INTC H R $(169):NEXT
855 X=28s REM *** R .LEG ***
860 I FSC=9 T HE N F0R Y= 14T01 8: X = X+1 -. GOSU B40 00:
ro ro to ro ro ro ro ro ro
PR I N TC H R$( 1 27 ) i NE X T
298 0 P 0 K E 6 4 6 ,11 2990 R ETU RN 300 0 RE M 301 0 R EM *** SUC CE S S R O UT IN E *** 302 0 REM 3030 X =5:Y= 15s GOS U B4 000 304 0 P RI NTW * 3050 FOR PA US E = 1 TO 2000; NEX T PA US E 306 0 X = 0 : Y = 11 s GO S UB 4 00 0 307 0 P R IN T"T H AN K S FO R S AVING ME!"
60
Page 73
Chapter3 .Words, Words, Words
308 0 P RI NT 309 0 PR I NT" YOU G UE S SE D "W$ 31 00 PR IN T
3105 P O K E 198, 0 311 0 PR I NT " P RE S S SPA CE BAR TO GO" 312 0 GET Z$:I F Z $ = " " THEN 31 20 313 0 RE TU R N 400 0 R EM 401 0 REM *** 'PRINT AT' R OU T IN E *** 402 0 REM 4030 P 0 K E 7 8 2 ,X s P 0 K E 7 8 1,Y s SY S6 55 2 0 s RETU RN 600 0 REM 6010 REM ** * M OV E CH A RA C TER S ET * ** 60 20 REM
603 0 P R I N T C H R $ (142)
6040 PO K E 5 2 ,48 : P 0 K E 5 6 ,48
605 0 P 0 K E 5 6 3 3 4 ,P E E K (56 33 4) AN D2 54
60 60 P O K E 1,P E E K (1)A N D 2 5 1
6070 F O R N = 0 T O 1023
60 80 P O K E 12 2 8 8 + N ,P E E K (53 2 4 8 + N )
60 90 NEXT N
6100 P O K E 1,P E E K (1)0R4
611 0 P 0 K E 5 6 3 3 4 ,P E E K (5 63 34) O R 1
612 0 P 0 K E 5 3 2 7 2 ,(P E E K (5 327 2)A N D 2 4 0 )+12
613 0 REM
6140 FO R N=1 T O2 0 : R EA D Z $: N E X T
6150 FOR N =0 T O1 2 7
61 60 READ Z
6170 P O K E 12 2 8 8 + 6 8 * 8 + N ,Z
61 80 NE XT N
6190 RE TU R N
7000 REM 7010 R EM *** I NS TR UCT IO NS ** *
70 20 REM
7030 PRINT "" 7050 PRI NT" 706 0 P RI NT 707 0 PRINT" 708 0 P RI NT 709 0 P RIN T" 7100 PR IN T 7110 PRI NT" 7120 PR IN T 713 0 P RINT" 714 0 P RI NT 71 50 PRI NT" 716 0 P R IN T 717 0 P RIN T" WRONG, PA RT OF THE M AN APP EA RS " 718 0 P RI NT
IN TH IS GAME, YO U W IL L BE"
GI VE N A ROW OF D AS HE S"
EA CH DAS H S T AN DS F OR A LET TE R"
YOU H AV E TO GU E SS THE L ET TE RS "
IF YO U A RE RI GH T TH E LETT ER "
WILL APP EAR. IF YO U ARE "
61
Page 74
Programming for Education on the Commodore 64
719 0
720 0
721 0
722 0
723 0
724 0
725 0
726 0
727 0
728 0
729 0
900 0
901 0
902 0
903 0
9040
905 0
906 0
907 0 908 0 909 0 910 0 911 0 912 0 913 0 914 0 915 0 916 0 917 0 918 0 919 0 920 0 921 0 922 0 923 0 924 0 925 0 9260
PRI NT" AND YO U LOSE A LIFE. IF YOU" PRINT PRI NT" T HI NK Y OU KN OW TH E W OR D YOU" PRINT PRIN T" MU ST SP EL L IT CO R REC T LY TO" PRINT PRINT" SAV E THE PO OR MAN!! " PRI NT PRINT" PRE SS T HE SP AC E BA R TO GO" GET Z$:IF Z $ = " " THE N 728 0 RE TU R N REM *** DAT A F OR WO RD S *** REM DA TA M O N DA Y , TUE S D AY, W EDN E S DAY DAT A T H U RS D A Y, F R I DA Y , SAT U R DAY DA TA SUN D AY , J AN U A RY, F E BR U A R Y DAT A M A RC H , A PR I L , MA Y , JUN E , JUL Y DA TA AU G UST , S EPT E M BER , O CT O B E R DAT A N O VE M B E R, D E C EM B E R,Y E A R REM REM *** DAT A F OR FA CE *** REM DA TA 0 , 0 ,0 , 0,3 , 3 ,15 , 15 DAT A 15 , 1 5, 6 3 ,6 3 , 25 5 ,2 5 5 ,2 5 5 ,2 5 5 DA TA 2 40 , 2 4 0, 2 5 2 ,2 5 2 ,25 5 , 255 , 2 55, 2 5 5 DAT A 0 , 0, 0 , 0 ,1 9 2 , 19 2 , 240 , 2 40 DAT A 63 , 63, 6 3,6 3 , 60, 6 0 ,6 3 , 6 3 DAT A 25 5 ,25 5 , 63 , 6 3 ,1 5 , 1 5, 6 0 , 60
DATA 2 5 5 , 255 , 2 52, 2 5 2, 2 4 0 ,2 4 0 , 60 , 6 0
DA TA 25 2 , 252 , 2 52, 2 5 2, 6 0 , 60 , 2 5 2, 2 5 2
DATA 6 3 , 63 , 6 3, 6 3 , 60 , 6 0 ,1 5 , 1 5 DA TA 25 2 ,25 2 , 240 , 2 40, 4 8 ,4 8 , 6 3, 6 3 DAT A 63 , 63 , 1 5 ,1 5 , 1 2, 1 2 , 25 2 , 252 DA TA 2 5 2 ,2 5 2 , 25 2 , 252 , 6 0,6 0 , 24 0 , 2 40 DA TA 3 , 3 ,0 , 0 ,0, 0 ,0 , 0 DAT A 20 7 , 20 7 , 2 40 , 2 4 0, 6 3 ,6 3 , 1 5, 1 5 DA TA 2 43 , 2 4 3, 1 5 ,15 , 2 52, 2 5 2, 2 4 0 ,2 4 0 DA TA 1 9 2,1 92, 0,0 ,0, 0, 0 ,0
READY.
Commentary
As this is a fairly complicated program, we have inserted a lot of REMs to inform you of what is happening.
Lines 1040-1070 constitute the control module. Line 2000 begins the game
subroutine.
62
Page 75
Chapter 3 Words, Words, Words
Lines 2020-2040 choose a word, W$, at random from data. Line 2070 sets the print at coordinates. Lines 2080-2100 print dashes according to the
number of letters in the word. Line 2145 empties the keyboard buffer.
Line 2150 accepts the guess, G$, and sets the variable FLAG to a one. This is only changed to a zero if a correct letter is input, and is used in line 2260 to check if part of the gallows should be drawn.
Line 2155 checks to see if the whole word has been entered. If it has, the program jumps to the winning routine.
Line 2160 sets variable LN to the length of the chosen word, W$.
Lines 2170-2210 check the length of the guessed word against the chosen word. If more than one letter is input, it is ignored, unless it has the same
number of letters as W$.
Line 2220 contains the counter for the number of incorrect guesses, Z.
Lines 2230-2250 check each letter of W$ to see if it contains the guessed letter, G$. If any letter is the same, subroutine 2500 prints it in the correct place. The flag is also changed to prevent part of the gallows being built.
Line 2270 and 2280 print the number of lives remaining at the top of the screen.
Line 2290 sends the program back to the start of the guess loop if less than
10 incorrect guesses have been made. On loop 10, the program continues.
Line 2500 is a short routine to print a letter in the correct place. It uses the loop counter N, to tell it where to put the letter.
Lines 2600-2990 contain the rather large drawing routine to construct the gallows and the man.
Line 2610 uses the number of incorrect guesses, SC, to control the printing colour.
Lines 2620 and 2630 use a loop to draw the upright post. CHR$(166) is a graphics character like a tiny chessboard.
Lines 2640 and 2650 use another loop to draw the crosspiece. GOSUB 4000, to the print at routine, ensures that the next print position is con trolled by the X value in the loop counter.
63
Page 76
Programming for Education on the Commodore 64
Lines 2730-2800 draw the head. This is made up from defined characters
held in DATA statements. The print at routine is again used to control the
position of the head.
Lines 2815-2860 use the same principle to draw the rest of the body
according to the value of the variable SC.
Line 2980 POKEs the printing colour back to grey. The success routine also uses the print at routine to print the correct word and a thank-you message.
Line 4030 contains the short print at routine where the new X and Y coordinates are POKEd into memory. Without this routine, the effects in a program like this would have been much more difficult to achieve.
Line 6000 starts the character-moving routine. Characters cannot be rede
fined on the 64 unless the character ROM is copied into RAM.
This is exactly the same structure as that in the program BigL, so refer to
those notes for more detail.
Lines 6140-6180 redefine the 16 characters from number 68 onwards. Note that this number is not the same as the ASCII code and, when the characters are printed, 32 must be added to the number of the character before it can be used after a CHR$.
Lines 7000-7290 contain the instructions.
Lines 9000 onwards contain all the data for the program. It is divided into two sections, the first containing the words to be used, and the second the data for the redefined graphics used to draw the face.
Anagram
It is hoped that the programs that are included in this book reflect to some degree the belief that children do not come to school simply to acquire knowledge that may or may not be useful in the future. Children need to learn many skills at primary age, and, apart from the most important (social skills), thinking skills and the ability to research and store informa
tion must rank high in the list.
Although spelling is important in its own right, the child who has diffi culty with spelling will be at a disadvantage when it comes to finding information from reference books. There is little point in giving a child an
64
Page 77
Chapter 3 Words, Words, Words
encyclopedia and saying Find out all you can about beer production in Belgium if the child cannot spell Belgium and does not know how to find the information in the first place. Information from a book may not be as good as firsthand experience (especially in the example in question!), but it is better than no experience at all.
The following program can be used as a spelling aid, or in any other
subject area to familiarise children with new words. Anagram allows the parent or teacher to load data lines with a fresh vocabulary. The child is presented with a scrambled version of the word on the screen and has to input the word, spelt correctly. Points are gained for getting the word right. The final score is shown after 10 words have been displayed.
100 REM 1010 REM *** AN A GR A M G AME *** 1020 REM 1030 RE M *** AJS 4 /1 0/ 8 3 *** 1040 R EM 1050 GO SU B 1500 s REM IN STR 1060 FOR TRY = 1 TO 10 1070 GO SUB 2 00 0 s REM GET WO RD 1080 GO SU B 3000s REM M IX W OR D 1090 GO SUB 400 0 s REM GAME 1100 NEX T TRY 1110 GO SU B 5 0 0 0 sREM S CO RE 1120 STO P 1499 REM 1500 REM *** I NS TRU CT IO NS *** 1510 REM 1520 1530 1540 1550 1560 1570 1580 1590 1600 1610 1620 1630 1640 1650 1660 1670 1680 1690 1700
P0K E53 28 1, 1s PO KE 6 46 ,0 PRI NT CH R$ <1 47 > PRI NT PRINT" YOU WILL BE SH OW N A NU MB ER" PRINT PRINT" OF J UM B LE D UP WORDS" PRINT PRIN T" YO U CAN ASK F OR TH EM TO BE" PRINT PRIN T" MI XE D UP A GA IN OR Y OU CAN" PRINT PRIN T" GUE SS WHA T YOU TH IN K THE" PRI NT PRIN T" WORD REALLY IS" PRI NT PRIN T" AF TE R TEN WO RD S Y OU W ILL" PRI NT PRIN T" BE S HO WN YO UR SCO RE" PRINT
65
Page 78
Programming for Education on the Commodore 64
1710 1720 1730 1990
2000
2010
2020
203 0 204 0 205 0 206 0 299 0
300 0 300 5 301 0
302 0
303 0
304 0
3050 306 0 307 0 308 0 309 0 399 0 400 0 401 0 40 20 403 0
404 0
405 0
406 0 407 0 4080 409 0 410 0 411 0 412 0 413 0 414 0 415 0 415 5 416 0 417 0 419 0 420 0 450 0 451 0 452 0
P R I N T PR ES S THE S PAC E BA R TD SO" GET Z*: IF Z*="" TH EN 1720 RE TU R N REM REM *** GET WOR D *** REM REST OR E
FOR N = 1 TO I N T (RND < 0)*21)+1 READ WD*
NEX T RE TU RN REM
REM *** MIX WO RD *** REM
DU MM Y *= WD* N W * = " FOR N = 1 TO L EN (D UMM Y*) IFw R N D (0 )>= .5 TH E N N W * = N W * + M I D * (D U M M Y * , N , 1)
GO T O3 0 60
NW *=M ID * < D U M M Y * , N , 1)+N W* NEXT N DU MM Y* = NW *
IF NW * =W D * THE N 302 0 RE TU R N REM RE M ** * DISP LAY ** * REM P R I N T C H R * (147) PRINT PR INT " YO U R JU M BL E D L E TT E RS AR E:- " PRINT PRINT N W*
X 1= P E E K ( 782 ):Y 1= P E E K (781)
X= 5:Y = 22 : GOS U B60 00 P O K E 198,0 P R I N T " M .TO MIX, G TO GUESS ?" GET Z*
IF Z*< >"M" AND Z*< >"G " T HE N 411 0
X = X 1:Y = Y 1:GO SU B 60 00
IF Z*="M" TH EN GO S UB 30 00 IF Z*="M" THEN GOT O 4020
X= 5:Y =2 2 s GO S U B6 0 00
INPU T" EN TE R YO UR GU ES S IFG* = WD* T HEN G OS U B4 5 00 IFG* O W D * T HE N G OSU B46 00
RE TU RN REM
REM *** SUC CES S R O UT INE *** REM
---
>" 5G*
66
Page 79
Chapters Words, Words, Words
453 0 P RI NT Y E S : SC = SC+1 454 0 P0K.E53281 ,5 45 50 F OR PA US E = 1 TO 2000s NEX T PA USE 45 60 P 0 K E 532 8 1,1 45 70 R E TU RN 460 0 RE M 46 10 R EM *** MI S TA K E R OU T IN E *** 46 20 R EM 46 30 P RI NT 11 NO, WD* 4640 P 0 K E 5 3 2 8 1 ,2 465 0 F OR P AUS E * 1 TO 2000a NEXT P AU SE 4660 P 0 K E532 8 1,1
4670 R ETU RN
50 00 R EM
50 10 RE M ** * S CO RE R OUT IN E *** 50 20 REM 50 30 P R I N T C H R $ (147) 504 0 PRIN T
50 50 P R I N T YO UR S CO RE WA S S C WO RD S R I G H T 50 60 IF SC = 10 T HE N P R IN T WEL L DONE" 50 70 IF S C >6 A ND SC <10 TH EN PR IN T F A I R 50 80 IF S C< 7 TH EN PR IN T LE AR N TH EM !!" 50 90 59 90 600 0 601 0 602 0 8990 900 0 901 0 902 0 903 0 904 0 905 0 906 0 907 0 908 0 909 0 9100 911 0
912 0
RE TU R N REM REM *** 'PRINT AT' ROU TIN E *** REM PO K E 7 8 2 , X s PO K E 7 8 1, Y s SY S 65 5 2 0 s RE TU R N REM REM *** DAT A ST A TE M E NT S *** REM REM *** E NT ER Y OU R OWN WO RDS *** REM REM *** BU T M AK E SU RE TH ER E *** REM REM *** ARE TW EN TY *** REM DATA A A R D V A R K ,B A B O O N ,CAME L *DON KE Y DA TA E L E P H A N T ,F L A M I N G O ,GO O S E ,HO RSE DATA I B E X ,J AC K A L, K A N GA R O O,L E M UR DATA M O N K E Y ,N E W T OS T R I C H ,QUA IL DATA R A B B I T ,S T O R K ,TI G E R ,Z EBR A
READY.
Commentary
Lines 1050-1120 form the control module. Lines 1060-1100 contain the main loop which selects ten words to guess.
67
Page 80
Programming for Education on the Commodore 64
Lines 1500-1730 contain the instructions. Line 1520 sets the background colour to white, and the printing colour to black. Line 1530 clears the screen. Line 1720 waits for a key to be pressed before the program continues.
Lines 2000-2060 get words at random from the DATA lines. If you have more than twenty words in DATA, you will have to increase the size of the loop in line 2030. RESTORE in line 2020 sets the data pointer back to the beginning of the list of words each time.
Lines 3000-3090 mix the letters of the word. The loop in lines 3030-3060 goes through the letters of the chosen word one at a time. It builds a new word by attaching a letter at random to either the front or back of the new word.
Line 3070 uses DUMMY$ to hold a copy of the new word. Line 3080
checks to ensure that the new word is not the same as the original!
Lines 4000-4200 control the display of the game. Line 4020 clears the screen. Lines 4040-4060 print the jumbled word.
Line 4070 saves the print coordinates in variables XI and Y1. Line 4080 resets the print position to the bottom of the screen by jumping to the print at subroutine. Line 4090 clears the keyboard buffer.
Lines 4100-4120 allow the input of either M or G. If M is pressed, the program jumps back to the mix subroutine. If G is pressed, the program continues.
Line 4155 sends the print coordinates for the bottom of the screen to the print at subroutine. Lines 4160-4190 check the guess against the actual word and send control to the particular subroutine.
Lines 4500-4570 contain the success routine, which simply increases the score by one and temporarily makes the background green in line 4540.
Lines 4600-4670 contain the mistake routine. This turns the background colour to red.
Lines 5000-5090 clear the screen and print the score.
Line 6020 is the print at subroutine, which sends the coordinates of the next print position to the operating system kernal.
Lines 9000-9120 contain all the words used. Any twenty words can be
used here, but avoid those with more than 10 letters, as they will extend on
to the next line.
68
Page 81
CHAPTER 4
Making Friends with your Keyboard
Tackling entry errors menus making problems foolproof moving the character set BigL: writing large on the screen Key board: learning the keys Masking: changing binary numbers.
A computer can receive information from a variety of sources. These include cassettes, disks, joysticks, light pens, even the telephone line and, of course, the keyboard. In a program, the most common methods of entering data are via INPUT and GET statements.
You have already seen INPUT used on several occasions. Program exe cution halts until some information is entered by pressing the [RETURN] key. GET does not halt the program, so its sensible to put it in a loop to wait until a key is pressed. When you press a key, the code of that key is stored in the keyboard buffer, a type of temporary memory store. To make sure that only the input you want is left in the buffer, you can clear it by POKE 198,0 just before the GET loop.
It is also quite easy to ensure that any input is within the limits you set. A program that only requires figures can use GET and check the code of keys that are pressed. If any other key is pressed, the program can loop back to request another input. This program does just that:
10 0 REM * *** * *** * * *** * * ** * * * ** * * * ** * * * *
110 R EM SE L E CT I VE INPUT R O UT I NE 120 E EM * * *** * ** * * ** * * ** * * *** * * *** * * ** * 130 P O K E 198,0 140 GE T A*:IF A$ = "" THE N 140 150 IF AS C ( A$ ) <48 OR A S C <A* )>5 7 T HE N P RI N T "NOT A
NUM BE R" 155 IF A S C <A* ) < 48 O R A 5 C < A $ ) > 57THE N GO TO 130 160 P RI NT "YOU PRE S SE D NU MB E R "A$ 170 S TO P
READY.
Line 150 checks the ASCII code to make sure it lies between 48 (0) and 57 (9).
69
Page 82
Programming for Education on the Commodore 64
A routine like this can also make a program easier for the user. One of the most frustrating things for an inexperienced user is for error messages to appear on the screen when they are half-way through the program. Shielding the user from the operating system is an important area of pro gramming, for, even if a program works perfectly, it can be spoiled by lack of foresight on the part of the programmer.
Firstly, the program needs to be self-explanatory, as far as that is pos sible. If someone puts a cassette in and presses SHIFT and RUN/ STOP, instructions on use should be the first thing that appears after the title. If the program is easy and pleasant to use, it is usually termed user- friendly. If the program has several options, it is a good idea to make it menu-driven to increase its friendliness. This means that a list of options appears on the screen, and the user can make his selection from the displayed menu, eg
Press a number to choose:
1. Instructions
2. Easy Problems
3. Harder Problems
4. Difficult Problems
If any other key apart from 1, 2, 3 or 4 is pressed, the computer should ignore it. This can be achieved by using a GET routine as explained earlier.
Another advantage is that the user does not have to press the RETURN
key.
In a program requesting the date of birth, INPUT can be used instead of GET, but the manner in which the data is entered should be shown by an example, then the input checked by the program.
1000 REM BIRTHDATE SUBROUTINE 1010 PRINT‘‘ENTER YOUR DATE OF BIRTH 1020 PRINT‘‘USE THE FOLLOWING ORDER - 1030 PRINT*DATE, MONTH, YEAR 1040 PRINT‘‘E.G. 4TH MARCH 1956 WOULD BE
1050 PRINT 4<RETURN > 1060 PRINT 3<RETURN> 1070 PRINT 1956< RETURN>
1080 PRINT* *DATE - ; 1090 INPUT DATE 1100 IF DATE < 1 OR DATE >31 THEN 1090
1110 PRINT*MONTH- 1120 INPUT MONTH
70
Page 83
Chapter 4 Making Friends with your Keyboard
1130 IF MONTH < 1 OR MONTH > 12 THEN 1120 1140 PRINT YEAR- 1150 INPUT YEAR 1160 IF YEAR < 1880 OR YEAR > 1984 THEN 1150
The example suggests the entry format, so the user knows the correct order
of input, and also that he is expected to enter figures and not words.
If impossible dates, such as -4/15/1983, are entered, they are outside the permitted range of inputs. If letters are entered, eg APRIL instead of 4, the 64 generates an error message REDO FROM START so the input
loop is repeated once more. If the RETURN key is pressed with nothing
entered, the loop is also repeated.
The way to make the routine check that only figures are entered would
be to make the input lines contain string, rather than numeric, variables, and check their ASCII codes. This would be a useful exercise for you to try.
In this chapter, we are including some programs that help children find
their way round the keyboard. They also introduce ideas that you may find useful in your own programs.
Keyboard encourages fast entry of all the characters on the keyboard.
The instructions are presented as a subroutine and the TIME function in the machine starts when the space bar is pressed. A large version of the character appears on the screen and three attempts are allowed to find the correct key.
In the 64, the character definitions are stored at memory location 53248
onwards. Each character on the screen consists of eight rows of eight posi tions, like a chess board. Each row is represented by a binary number. If you imagine a black character on a white background, then ones stand for blacks, and zeros for whites, so a totally filled-in character would contain 255 in each row, as the binary code for 255 is 11111111.
On some computers, it would be easy to find the character definitions by
PEEKing the locations where they are stored. The 64, however, uses a system that is more complicated, in order that more is available to the user. It reads its character set from ROM which shares the same addresses as user memory and some input/output routines. Every time the 64 needs to display a character, a pointer in another part of the memory sends it to the character ROM. If we want to read the definitions, or indeed to change the set to display our own definitions, the easiest thing is to copy the entire character set into user RAM (which can be altered). The 64 must also have the pointer changed so that it points to the new character set.
The following program copies the character set from ROM into RAM at
location 12288. If this is all that is done, your BASIC program may over write the new set at a later stage. The 64 must be told that it cannot use this space, so the pointers to string variable space and the highest address used
by BASIC have to be changed. Then the keyscan interrupts have to be
71
Page 84
Programming for Education on the Commodore 64
turned off and the 64 told to look at the character ROM rather than RAM. At this point, all the necessary characters in the ROM can be scanned and their definitions POKEd into their new locations in RAM.
If you were to halt the program at this point, you would find that the
keyboard had died. Even the RUN/STOP and RESTORE method would not retrieve your program, as the keyboard is not being scanned. With the characters in their new location, the thing to do is first to switch in the input/output routines, then to start the keyboard scan routine. Now that the character set is in user RAM, you can look at it easily, or alter it as necessary.
Once the character definitions are available, it is possible to PEEK them,
and use the codes to produce large versions of them on the screen.
The way this is achieved is by storing the definitions in arrays that hold
the code for each line as a binary number; so 170 is stored as its binary equivalent, 10101010. This number is now scanned. If a 1 is found, a solid block is printed; if a 0 is found, a space is printed. This results in a character sixty-four times normal area being printed on the screen. The actual pro gram runs through the entire character set and prints the value held in each location so you can see how the character is formed.
BigL
1000 R EM ** * L AR GE C HAR A CT E R P RO G RAM ** * 1010 R EM
1020 R EM *** M OV E C H A RA C TER SE T >RAM *** 1030 RE M
1040 P R I N T C H R $ (142) 1050 P 0 K E 5 2 ,48 1060 P 0 K E 5 6 ,48 1070 C LR 1080 P 0 K E 5 6 3 3 4 ,P EE K < 5 6 3 3 4 )AN D2 5 4 1090 P O K E 1,P EE K <1)A N D 2 5 1 1100 F O R I = 0 TO51 1 1110 P O K E I +1 2 2 88 , P E E K (I+53248) 1120 NEXT 1130 P O K E 1,P E E K (1)0 R4 1140 P 0 K E 5 6 3 3 4 ,P E E K (56 334)O R 1 1150 R EM 1160 R EM ** * P RI NT L ARG E L E TT E R S * ** 1170 R EM 1180 Z = 1228 8 1190 P G K E 5 3 2 8 1 ,0
1 20 0 D I M B (8 ) s D I M C (8 ,8 )
1220 FO R X= 0 TO 511 ST EP 8 1230 P R I N T " LS H I F T/CL R / H O M E 1 "
72
Page 85
Chapter 4 Making Friends with your Keyboard
1240 FO R N =0 T O7
1250 P RI N TZ+ X +N , P E E K (Z+X+N)
1260 B (N )= PE EK (Z+N+X)
1270 FOR J= 0 TO 7
1280 C<N ,J )= INT(B (N ) /2'*<7-J) )
1290 B ( N)= ( ( B(N ) /2 " ( 7-J ) > -C < N ,J) ) * 2 s <7~J)
1300 IFC < N ,J )=1 T H E N P O K E (10 6 4 + N * 4 0 )+15+J, 81s POKE
< 5 533 6 + N * 4 0 ) + 15 +J ,0 1310 NEX TJ 1330 NE X TN 1340 NE XTX
READY.
Commentary
Line 1040 clears the screen. Lines 1050 and 1060 tell the 64 where to store
strings and the BASIC program so that it wont overwrite the new char acter set.
Line 1070 frees all available memory space. Line 1080 turns off the key board scan. Line 1090 selects the character ROM. Lines 1100 to 1120 copy the contents of the first section of the character ROM into RAM. Line 1130 deselects the character ROM. Line 1140 turns on the keyboard scan. Line
1180 sets Z equal to the start of the location of the copied character set.
Line 1190 sets the background colour to green. Line 1200 DIMensions space for the arrays necessary to hold large versions of the character definitions.
Lines 1220-1340 contain the loop to go through the entire 64 copied char acters. Lines 1240-1330 contain the loop to display each line of the character.
Lines 1250 and 1260 store the decimal (normal) value of each line in a variable, B(N).
Lines 1270 to 1310 contain a routine to convert the number in B(N) to its binary equivalent. This is stored as a series of zeros and ones in array C(N,J).
It can be seen, if you run this program, that it provides an introduction to how a keyboard trainer program operates. In the next program, the char acters are printed in a large version on the screen. These are presented to the
73
Page 86
Programming for Education on the Commodore 64
user in a random fashion, and they have to input the correct letter from the
keyboard as fast as they can. If they are successful, another letter appears and the time taken is added to their total time.
Keyboard
100 R EM *** KEY BOA R D T RA INE R *** 110 R EM
120 R EM *** A.J.S . 4/ 1 1/ 8 3 ** * 130 REM
140 REM ** * * ** * * ** * * * ** * * *** * * *** * 150 R EM 190 G OT O 1000 s REM MO VE C HAR A CT ER SET 200 GOS U B 30 0 0 :R E M I NS TRU CT IO N S 210 G OSU B 40 0 0 :R E M TE ST MOD UL E 220 GOS U B50 0 0:R E M SC OR E MO DU LE 230 GO TO 20 0 240 R EM
1000 RE M L AR GE C H AR A C TE R PRO GR A M
1010 RE M
1020 RE M R E LO C AT E C HA R AC T E R SET IN R AM
1030 REM
1040 PR I NT CH R$ <1 47 )
1050 P OK E S 2 ,48
1060 P 0 K E 5 6 ,48
1070 CLR
1080 P 0 K E 5 6 3 3 4 ,P E E K (563 34)AN D 25 4
1090 P O K E 1,P E E K (1)A N D 2 5 1
1100 F O R I = 0 T O 5 1 1
1110 POK EI + 1 228 8 , PE EK <I+53248 )
1120 NE XT
1130 P O K E 1,P E E K (1)0R4
1140 P 0 K E 5 6 3 3 4 ,P E E K ( 563 34 )O R 1
1150 GO TO 20 0
1990 RE M 200 0 R EM ROU TI N E TO P RI NT L AR GE L E TT E RS 2010 R EM 2020 Z = 122 88 202 5 R = I N T ( R N D (0)* 26)+ 1 2060 P RI NT CH R$<14 7) 207 0 F O RN = 0TO 6 209 0 B <N) - P E E K (Z +N+R*8) 210 0 F O RJ= 0TO 7 21 10 C (N , J ) = I N T (B < N ) / 2 * (7J ) )
212 0 B(N) = <(B(N )/2 ( 7~J ) ) - C < N, J ) ) * 2 S (7-J)
213 0 IFC < N ,J > =1 T H E N P O K E (10 6 4 + N * 4 0 ) +15 + J ,81
214 0 IFC < N ,J )= 1T H E N P O K E (55 3 3 6 + N * 4 0 ) +1 5+ J ,0
74
Page 87
Chapter 4 Making Friends with your Keyboard
215 0 216 0 217 0
300 0 301 0 3020
303 0 3040
305 0
306 0
307 0
308 0
309 0
310 0
3110 3120
313 0
314 0
3150
316 0
317 0
400 0
401 0
4020
403 0
404 0
4050
406 0
407 0
408 0
409 0
410 0
411 0
412 0
413 0
414 0
415 0
416 0
417 0
50 00
5010
50 20
5030
50 40
505 0
50 60
507 0
508 0
509 0
NEX TJ NE XT N RETU RN REM REM *** I N ST RU CTI ON S * **
REM P R I N T C H R * ( 147 ):P 0 K E 5 3 2 8 1, 5: P 0 K E 6 4 6 ,6 PRINT PRIN T" YOU A RE A BO UT TO BE SHOWN" PRIN T PRI NT" 10 LE T TE R S A ND OT HER SYMB OL S" PRIN T PRINT" THAT AR E A LL O N T HE KEYBOARD " PR IN T PRI NT " FIN D TH E C OR R EC T KE Y A ND PRE SS" PRIN T PRINT" IT AS QU IC K LY AS YO U CAN" FOR X = 1 TO 8: P RI NT : N EX T X PRIN T" P R ES S THE SPA CE BA R TO GO" GET Z*:IF ZS="" T HE N 3 16 0 RE TUR N REM REM *** T ES T M ODU LE ** * REM TS = TI M E:S C =0 FOR G= 1 TO 10 GO S UB 2 00 0 P 0 K E 7 B 2 ,5:P 0 K E 7 8 1, 20:SY S65 5 20 PR I NT "T HE L ET T ER IS
--------
>"5
P O K E 198,0 GET Z* :IF Z$="" T HE N 4 09 0 PR IN T Z$;
IF AS C ( Z$ ) =R+ 6 4 TH EN SC =S C+1 IF AS C (Z * ) =R + 6 4 TH EN PRI NT " YES"
IF A S C ( Z $ ) O R + 6 4 T HE N PRIN T" NO, "CHR* (R+ 64 ) FOR PA US E=1 TO 1 00 0: NE XT NEX T G T T = I N T ( (TI ME- TS)/60) RE TU RN REM REM *** S CO R E M OD U LE *** REM PR IN TC HR *( 14 7) PRINT PR INT " YO U S CO RED "SC" CO RR EC T" PRINT PR INT " AN D T OO K "TT" S EC ON DS " PRINT PR I N T" P RES S SP AC E BAR TO G O AG AIN"
75
Page 88
Programming for Education on the Commodore 64
51 00 GE T Z$ :I F Z$ = ,,M T HE N 5 10 0 52 00 RET UR N
READY.
Commentary
Lines 190-230 contain the control module.
Line 230 loops the program through the instructions and the test for as many times as is necessary.
In order that the character set can be examined, it is copied from ROM in
the same manner as that used in the last program.
Lines 1000-1150 are responsible for the moving. When you RUN the pro gram, you will have to wait for about ten seconds while this is achieved. The subroutine is the same as the routine in BigL, so refer to that program
for more detailed notes.
Note that this is not a subroutine. If GOSUB 1000 is used in line 190, and
RETURN in line 1150, the program will stop with an error message in line
1150 (that there is a RETURN with no GOSUB). This is because the CLR in line 1070 resets the stack containing the RETURN addresses.
The routine from line 2000 to line 2170 is also similar to that in BigL, and
is used to print a large version of the selected letter on the screen.
Line 2025 chooses this letter at random from the data.
Lines 3000-3170 contain the instructions.
Lines 4000-4170 contain the test module.
Line 4030 reads the current internal clock time into variable TS, and sets the score counter, SC, to zero.
Lines 4090 and 4100 check the keyboard for an input, and lines 4110-4130 check this input against the selected letter. If it is correct, the score is incre mented and YES printed.
Line 4140 provides a short pause so that the screen can be read.
After ten attempts, line 4160 is used to calculate how many ticks have been produced by the jiffy clock, and this number is divided by 60 and rounded off to produce the number of seconds taken.
76
Page 89
Chapter 4 Making Friends with your Keyboard
Lines 5000-5200 contain the score module which simply clears the screen
(in line 5030) and prints the details of the test on the screen.
Masking
In the last program, and in one or two others, you may have found a parti cular feature in a line that seemed to make little sense. A typical line might be
POKE 56334,PEEK(56334)AND 254
It looks as though it means POKE a number with the number already there plus 254’. This is not true, however, as AND is a logical operator, not the same as +.
Logical operators are fairly easy to understand when they are used to
compare numbers.
10 INPUT A = ;A 20 INPUTB = ;B 30 IF A = 1 AND B = 1 THEN PRINT YES : GOTO 50 40 PRINT NO 50 STOP
AND TRUTH TABLE
A B
0
0 0 1
1
1 1 1
0 ie FALSE 0 ie
FALSE
0 0 ie FALSE
ie TRUE YES printed
This program would print YES if both conditions were true. If either or both of them were false, it would print NO.
10 INPUT A = ;A 20 INPUTB = ;B 30 IF A = 1 OR B = 1 THEN PRINT YES: GOTO 50 40 PRINT NO 50 STOP
77
Page 90
Programming for Education on the Commodore 64
OR TRUTH TABLE
A B 0 0 0 1 1
1 0 1
1 1 1 ie
0
ie
FALSE ie TRUE ie TRUE
TRUE
This program would print YES if either of the two conditions were true and the other were false, or if both of the conditions were true. If both the conditions were false, then NO would be printed.
When logical operators are used to compare numbers, which they can in the range -32768 to 32767, then they compare the way the numbers are stored in two bytes.
Bytes store numbers using binary digits, ie using only ones and zeros.
DECIMAL BINARY
0 0
1 1 2 10 3 11 4 100 5 101 6 110 7 111 8 1000
If two numbers are compared using AND, the individual bits of each number are compared one by one, and the results are the same as in the truth tables above.
Suppose the two numbers are 23 AND 254.23 is 00010111 in binary, and 254 is 11111110. Placing the numbers on top of each other and using AND, we get:
00010111
11111110
00010110 You will notice, in the result line, the only bit to have altered is the last one
on the right. If the two numbers to be compared are 19 OR 4, the result is:
00010011
00000100
00010111
78
Page 91
Chapter 4 Making Friends with your Keyboard
The technique of masking relies on this effect, to ensure that just one bit of a number is set to a one or a zero. All the other binary digits remain the same.
In the examples, it can be seen that, to ensure that the third bit from the right is set to one, the number should be ORed with 4 (as 100 is 4 in binary).
To alter the fourth bit from the right, the number would have been ORed
with 8 (as 1000 is 8 in binary).
To ensure that the last bit on the right is set to zero, the number should be ANDed with 255 - 1, ie 254. This is because the binary reverse of 1 is 254. (Zeros instead of ones, and vice versa.)
One is 00000001 and 254 is 11111110. To alter the fourth bit from the right, the number should be ORed with 255 - 8, ie 247. This is because the binary reverse of 8 is 247. 8 is 00001000 and 247 is 11110111.
Why all this complexity to achieve such a small thing, you may wonder. The reason is that, in order to use its memory carefully, several functions in the 64 are controlled by single bits in a memory location, rather than by the whole byte. There are several examples of this method being used in the graphics and sound chips. For instance, location 53269 controls whether the graphic characters called sprites are turned on. The eight bits of that location each control one sprite. In a program, you might want to turn one on and leave the others unaltered. The best way to do this is by ORing location 53269; eg to turn on the first sprite, without affecting the others, you would enter:
POKE 53269,PEEK(53269)OR 1
To turn it off without affecting the others you would use:
POKE 53269,PEEK(53269)AND(255 - 1)
79
Page 92
Page 93
CHAPTER 5
Story Time
Creativity artificial intelligence words at random Story: a
program to help with creative writing and adjective use interactive
use of computers Gosh: more stories.
Can a computer be creative? To answer this question would take too long in discussing the meaning of creativity and the future prospects of artificial intelligence. At the end, there might not be any firm conclusions. What would be agreed is that, given sufficient information, computers could shuffle this information and display it in all possible ways, including some that might appear creative and original.
For parents and teachers, the problem with childrens creativity is often one of encouraging the child to start writing. Once the initial paragraph has been written, the material begins to flow more easily.
The first program does not do anything that could not be achieved using pencil and paper, but it does produce a result in far less time. Various subjects and locations are held in DATA lines. If the user does not like the combination selected, the program produces more at random when a key is pressed.
The READ statement is a very useful feature of the BASIC language. Instead of entering all the data during the running of a program, it can be stored in DATA lines, usually placed at the end of the program. When data is required, the statement READ is used, followed by a numeric or string variable. When the program is RUN, a pointer moves along the DATA lines, each time they are called. During the course of the program it is possible to send this pointer back to the start of the DATA lines by using the command RESTORE.
Enter this program if you arent sure about using READ, DATA and RESTORE.
10 READ A
20 READ X$
30 READB
40 RESTORE
50 READ C,Y$,D
81
Page 94
Programming for Education on the Commodore 64
60 PRINT A,X$,B,C,Y$,D
70 DATA 1,HELLO,37.5
Some BASICs allow you to RESTORE to a particular program line, but in Commodore BASIC you have to resort to tricks if you want to use data
from the middle of DATA lines. For instance, to reach the fourth piece of DATA you could have:
10 FOR LOOP = 1 TO 4
20 READ A
30 NEXT LOOP
40 PRINT A
50 DATA 1,2,3 60 DATA 4,5,6
Data can occupy several lines, and often looks neater in listings presented in this way. The 64 sees it as one big block of data. Be careful you dont attempt to READ words into numeric variables, or you will get an error message.
Story
In the following program, the child is requested to enter adjectives to des cribe the subject and the location. On completion, the program produces the opening paragraph of a story written around the data that has been entered.
In terms of program complexity, this does not rank very highly, but it does show the way in which apparently original text can be generated. It also requires a lot of thought on the part of the child to enter suitable descriptions that fit the mood of the opening sentence.
1000 REM *** STOR Y * **
1010 R EM 1020 REM *** AJS 9 /1 0/ 8 3 *** 1025 REM 1040 GO S UB3 0 00 : RE M GET WO RD S 1050 GO S UB4 00 0 :R E M PRI NT T EXT 1060 GOT O 10 40 :R EM L OOP P RO G RA M
202 0 REM 300 0 REM 301 0 R EM *** GET WO R DS *** 302 0 REM 303 0 RESTORE. 304 0 F O R N = 1 TO IN T(RN D (0)*5)+1
82
Page 95
Chapter 5 Story Time
305 0 R EAD S TART# 306 0 NEXT 307 0 R ES TO RE 308 0 FQRN=1 TO 5 + I N T (R N D (0 )*20)+1 309 0 R EA D P ER S# 310 0 NEXT 311 0 R ES TO RE 312 0 F O R N = 1 TO 25 + 1N T ( R N D (0)*20)+1 313 0 RE AD P LA CE # 314 0 NE XT 399 0 R ET UR N
400 0 R EM
401 0 RE M *** WRITE I NTRO *** 402 0 RE M
403 0 P R I N T C H R # ( 14 7) s PRI NT n * Y OUR S T O R Y * " s PRI NT 404 0 P RINT S T A R T # ; " "; 405 0 P RI NT PE RS # 406 0 P RI NT " WA S SEEN NEAR" 407 0 P RI NT P LA CE # 408 0 P RINT 409 0 P R IN T "E N TE R 5 A D JE C TI V ES T HAT BEST D ESC RI BE " 410 0 P RI N TP E RS # 411 0 F O R N = 1 TO 5 412 0 INPUT PA DJ#( N) 413 0 N EX T Ns P RI NT 414 0 P R IN T "E N TE R 5 AD JE C TI V ES T HA T B EST DES CRI BE " 415 0 P RIN TP L AC E # 416 0 F O R N = 1 TO 5 417 0 INPUT LA#(N) 418 0 NEXT Ns PRI NT 419 0 P R IN T "P R ES S SP AC E BAR TO C ON TI NUE" 420 0 G E T Z#sI F Z # = " " THE N 4200 421 0 P R I N T C H R # (147) 422 0 P R I N T S T A R T # ;" ";PE RS # 4230 P R IN T" W AS S EE N N EA R "PLACE # 424 0 P RI NT" IF Y OU H AV E E VE R S EE N "PERS# 425 0 P R IN T "Y O U W IL L K NO W T HEY ARE " P A # ( D 4260 PR I NT " AN D " P A # (2) 427 0 P R IN T"T HIS ONE WA S A LSO RAT HE R " P A # (3) 428 0 P R IN T" A ND D ID N' T W AN T T O EN TE R " 428 5 P RI N TP LAC E# 4290 P R INT "W H ER E IT W AS "LA# <1)" AN D "L A # (2) 430 0 P RIN T "C E RT A IN L Y IT WAS NO P LA CE FOR" 431 0 P RI N TP E RS # 432 0 P R IN T"S OME CRE ATU RE S ENJO Y P LA C ES TH AT ARE" 433 0 P R I N T L A # (3)" AND " L A # (4) 4340 P RI N T" HO WE VE R, AN ADV EN TUR E HAS TO S TART " 435 0 P RI NT "S OME WH ER E, SO S OO N T HE " P A # (5) 436 0 P R IN T "F I GU R E OF "PE RS#
83
Page 96
Programming for Education on the Commodore 64
437 0 PRI NT " CO U LD BE SE EN AB OU T TO EN TER" 438 0 P RI NT PL ACE*" W HI CH APPE AR ED " 439 0 P RI N T" E VE N MO RE " L A * (5)" T HA N B EFO RE" 440 0 P RI N TS PR I NT 441 0 P R IN T "P R ES S SP AC E BAR TO GO A GAIN" 442 0 GE T 2*sIF Z*="" T HE N 4 420 499 0 RET UR N 899 0 R EM 89 95 R EM *** O P E NI N GS *** 9000 D AT A ON CE UP O N A TI ME,A L ON G T IM E A GO 901 0 D AT A IN L ON G A GE S P AS T ,M A NY Y E AR S AGO 902 0 D ATA AB OU T 200 YE AR S A GO 9025 R EM * ** P E OP LE *** 903 0 D AT A A V ER Y O LD MA N, A Y OU NG GIRL
904 0 D AT A A ST RA NG E F IG URE ,A S HA M BLI NG V IS IO N 905 0 D AT A A BE A UT IFU L P R IN C ES S ,A W IC KED MAG IC I AN 906 0 D AT A A FI ER CE D RA G ON , A D AR K S TR A NG E R 907 0 D AT A A H A ND S O ME PR INC E, A HO OD E D FIGU RE 9080 D AT A AN E VI L W I TC H ,A M YS T ER I OU S VI SI TOR 9090 D AT A A LO ATH SOM E MO NST ER , A TE R RI FYI NG S HA PE 910 0 D AT A A SL IM Y T O A D ,A M AG IC S NA KE 911 0 D AT A AN E N OR M OU S W ORM ,A RE P ULS I VE A NI MA L 912 0 DATA AN E LE P HA N T ,A MO US E 913 0 R EM *** P L AC ES *** 914 0 DATA A D AR K F ORE ST , A DEE P C AVE RN 915 0 D AT A AN OLD C A S T L E ,A S HA DE D R A VI NE 916 0 D AT A A DEEP H OLE IN T HE G R OU N D, A HI DD EN MI NE 917 0 D AT A A DE SE RT ISLAND ,A TIN Y VI L LA GE
918 0 D AT A A H A UN TE D HO U SE ,AN EE RI E MA NS ION 919 0 D AT A A DA N GE R OU S M A R S H ,A L A RG E C OUN TR Y HO US E
920 0 D AT A A M YS T IC STONE CI R CL E,A N A R AB IAN BA Z AA R
921 0 D ATA A S TE A MI N G JU NG L E, A DE SER TE D TE MP LE
922 0 D AT A A LOS T C I TY , AN EM PT Y TOWN
923 0 DATA A BU RNI NG D E S E R T ,A S T EE P -SI D ED C AN YO N
READY.
Commentary
Lines 1040 to 1060 contain the control module. Line 1060 loops the pro gram back to the start for a new story.
Lines 3000 to 3990 choose words at random. Line 3040 chooses an intro duction from the first five phrases held in data. Line 3080 misses the first
five items of data and chooses a character at random from the next 20
items. Line 3120 misses the first 25 items of data and chooses a place at random from the last 20 items.
Lines 4000 to 4200 print the chosen introduction and request the input of
suitable adjectives to describe the character and the place.
84
Page 97
Chapters Story Time
Line 4220 onwards prints a framework of text in which the adjectives are
combined to make the first paragraph of a story.
Lines 9000 onwards contain the data lines. The data may be changed pro
vided the number of items in each section remains the same.
Gosh
After the last program, the time has come for a serious program to demon
strate the potential of the computer for creating original masterpieces.
Unfortunately, nothing of such high calibre was available!
We could attempt to disguise this program in educational language, to make it seem highly motivating and wonderful at increasing cognitive skills. When it comes down to it, however, its really just a lot of fun.
Gosh is a computerised version of Consequences, and expects inputs of people, sayings, places and results. It contains some items in data, and what it lacks in educational terms should be made up for by the amusement it causes.
1000 REM BOSH - PAT HALL, JAM '84 1010 REM
1020 GOSUB
1030 1040 1050 1060 1070 1080 1090
1100
GOSUB GOSUB GOSUB GOSUB GOSUB GOSUB GOSUB
GOSUB 2430
1150
1250
1390
1530
1670
1810
1950
2090
m
REM
REM s 5
REM PLACES :
0
0 REM HER REPLY
0
0 REM 0
s
INITIALI SATION
MALE NAMES
REM FEMALE NAMES
REM
HIS SPEECH
THE CONSEQ UENCE REM COMBINE AT RANDt REM
PAUSE
1110 G OTO 1090 1120 END 1130 REM 1140 REM IN ITIA LIS ATI ON 1150 DIM MT < 15 ) s DIM FT ( 15) 1160 DIM PT< 15 ) : D IM ST < 15 ) 1170 DIM RT< 15 ) : DIM C T ( 15 ) 1180 R EM WH ITE SC REE N 1190 P OKE 53280, 1 : PO KE 53281, 1 1200 REM BL UE TEXT 1210 PR INT CH RT ( 31 ) 1220 R ETUR N
85
Page 98
Programming for Education on the Commodore 64
1230 REM 1240 REM MALE NAMES 1250 PR I NT M C SHI FT / CL.R / HOME 1" 1260 X = 7 : Y = 5 : GOSUB 2470 1270 PRINT "TYPE IN FIVE B OY'S NAMES" 1280 FOR I = 1 TO 5 1290 Y = 5 + I * 2 : GOSUB 2470 1300 GOSUB 2520 1310 MT( I > = AT 1320 NEXT I 1330 FOR I = 6 TO 15 1340 READ AT ; M T ( I ) = AT 1350 NEXT I 1360 RETURN 1370 REM 1380 REM FEMALE NAMES 1390 PRINT "" 1400 Y = 5 ; GOSUB 2470 1410 PRINT "TYPE IN FIVE G IRL'S NAMES" 1420 FOR I = 1 TO 5 1430 Y = 5+1*2: GO SU B 2470 1440 GOSUB 2520 1450 F T ( I ) = AT 1460 NEXT I 1470 FOR I « 6 TO 15 1480 READ AT : FT ( I ) = AT- 1490 NEXT I 1500 RETURN 1510 REM 1520 REM PLACES 1530 PRINT "C S HIFT /CLR /HOM EI"
1540 Y = 5 : GOSUB 2470 1550 PRINT "TYPE IN FIVE PLACES" 1560 FOR I = 1 TO 5
1570 Y = 5 + I * 2 : GOSUB 2470 1580 GO SUB 2520 1590 PT( I ) = AT 1600 NEXT I 1610 FOR I = 6 TO 15 1620 READ AT : P T ( I ) = AT 1630 NEXT I 1640 RETURN 1650 REM 1660 REM HIS SPEECH
86
Page 99
Chapter 5 Story Time
1670 PRINT " I! SHI FT / CLR / HOME I " 1680 Y - 5 : GOSUB 2470 1690 PRINT" TYPE IN FIVE THINGS HE SAYS" 1700 FOR I = 1 TO 5 1710 Y « 5 + I * 2 : GOSUB 2470 1720 GOSUB 2520 1730 S T ( I ) = AT .1.740 NEXT I 1750 FOR I = 6 TO 15 1760 READ AT : S T ( I ) = AT 1770 NEXT I 1780 RET URN 1790 REM 1800 REM HER REPLY 1810 PRINT " CSHI FT/C LR/H OME ]" 1820 Y « 5 s GOSUB 2470 1830 PRINT "TYPE IN FIVE REPLIE S MADE" 1840 FOR I = 1 TO 5 .1.850 Y =5+1 * 2 ; GOSUB 2470 1860 GOSUB 2520 1870 RT< I ) = AT 1880 NEXT I 1890 FOR I = 6 TO 15 1900 READ AT ; R T ( I ) = AT 1910 NEXT I 1920 RET URN 1930 REM 1940 REM THE CONSEQUE NCE 1950 PRINT " C SHIF T/C LR/H OME3 " 1960 Y = 5 : GOSU B 2470 1970 PRINT "TYPE. IN FIVE CONSEQUENCES " 1980 FOR I = 1 TO 5
1990 Y =5+1*2: GOSUB 2470 2000 GOSUB 2520 2010 CT < I ) = AT 2020 NEXT I 2030 FOR I = 6 TO 15 2040 REA D AT : C T < I ) = AT 2050 NEXT I 2060 RETURN 2070 REM
2080 REM COMBINE AT RANDOM 2090 PRINT "C S HIFT /CL R/HO ME3" 2100 GOSUB 2390
87
Page 100
Programming for Education on the Commodore 64
2110 Y - 7 s GOSUB 2470 2120 PRINT MTC N ) 2130 Y = 8 ; GOSUB 2470 2140 PRINT "MET" 2150 GO SUB 2390 2160 Y =* 9 : GOSUB 2470 2170 PRINT F T ( N 2180 GO SUB 2390 2190 Y 10 s GOSU B 2470 2200 PRINT PTC N ) 2210 Y - 11 ; GOSUB 2470 2220 PRINT "HE SAID TO H E R: " 2230 GOSUB 2390
2240 Y = 12 s GOSUB 2470
2250 PRINT STC N )
2260 Y - 13 : GOSUB 2470 2270 PR INT "SHE R E P L I E D : " 2280 GOSUB 2390 2290 Y == 14 : GOSUB 2470 2300 PRINT ETC N ) 2310 Y == 15 s GOSUB 2470 2320 PRINT "AND THE CONSEQ UENCE WAS" 2330 GO SUB 2390
2340 Y = 16 : GOSUB 2470 2350 PRINT CT C N ) 2360 RETURN 2370 REM
2380 REM PRODUCE RANDOM NUMBE R 2390 N = INTC RMDC 1 ) * 15 ) + 1
2400 RETURN
2410 REM
2420 REM PAUSE
2430 FOR I = 1 TO 6000 s NEXT I
2440 RETURN
2450 REM
2460 REM PRINT TAB ROUT IN E
2470 POKE 782, X s POKE 781, Y
2480 SYS 65520
2490 RETURN
2500 REM
2510 REM CHECK INPUT FOR LENGTH
2520 GOSUB 2470
2530 PRINT "
2540 PRINT "
88
Loading...