On The Road To Artificial Intelligence - B - B - C - Micro
On The Road To Artificial Intelligence - B - B - C - Micro
car fa)
2. sa! enyacpap
Jeet api io; 8) = -
Wee
rR TY Ut oc P e ariad
oe
SS
=> 7
" \S =
*
On the Road to
Artificial
Intelligence:
BBC Micro
TELEPEN
00382268
DEDICATION
Jeremy Vine
BRENT LIBRARY SERVICE
i
a,-}
SHIVA
Shiva Publishing Limited
SHIVA PUBLISHING LIMITED
64 Welsh Row, Nantwich, Cheshire CW5 5ES, England
Preface
Can Machines Think?
Creating an Interactive Program
Strings and Things
More Strings Attached!
Words, Words, Words
Always the Unexpected!
Text Handling
Sigmund: An Interactive Program
Sigmund: The Program
Interviewer
Brainstorming
Artificially Intelligent?
Appendix A: For users of BASIC 1
Appendix B: A crash course in BASIC
Appendix C: BBC BASIC keyword summary
Digitized by the Internet Archive
in 2022 with funding from
Kahle/Austin Foundation
https://archive.org/details/onroadtoartificiO000vine
Preface
Nw
1 Can Machines Think?
COMPUTER: CAN'T I1
I ENVY YOU... DREAM
6
Information is the life blood of a machine and computers need
to be fed a healthy diet of raw data. This book concentrates on
one side of programming, that is, the manipulation of text, both
material held by the computer and text input by the user. You
may already be acquainted with a few BASIC commands and
able to INPUT and PRINT information but there is much more that
you can accomplish with text. BASIC offers a range of commands
enabling the programmer to manipulate and juggle text around to
create the impression of a thinking machine. Points to bear in
mind when writing a program are to:
If=> any of these points make little sense at the moment, don’t
worry. We will cover them as we progress and by the end of the
book these should be principles that will come straight into your
head when writing a program.
Always remember that we are dealing with an empty box. Our
job is to fill that box up and to make it an Aladdin’s cave to
anyone who looks inside. But if we are to be creative and write
interactive programs we must do some programming. So let’s get
down to it straight away. Happy interactions!
3 Strings and Things
I’ve already said that we’ve got to teach our BBC to think and the
first step in that process is user input. In this chapter, and the
next, we will see how the computer accepts information and then
manipulates it to our requirements. Let’s remind ourselves of
what is meant by a variable.
Program 3.1
1@ INPUT name$, age
Program 3.2
Run the program and you can see that the prompts are more
explicit and we can now direct the user to input his name and age.
8
Now rerun the program but this time enter your name at both
prompts. If we do this, it can be seen that the machine will store
the first input correctly, as it is a string input, i.e. any character.
However, the second variable ‘age’ requires a numeric input and
therefore a value of 0 remains in variable ‘age’ as the input is not
numeric.
J
JE
JER
JERE
JEREM
JEREMY
Well we could try and write a program, such as the one below:
Program 3.3
OPP RUN Teco
CURE LN Ue Ee
SOPPRUN Taw ER.
40 PRINT "JERE"
SO SPRINT “JEREM”
60 PRINT "JEREMY"
Program 3.4
10 INPUT "Enter a word", word$
2@ PRINT LEFTS$(word$,1)
Our next job is to produce the same effect without having to type
a PRINT statement for each line. Type in Program 3.5:
Program 3.5
10 phrase$ "THE QUICK BROWN FOX"
2@ counter = 1
3@ PRINT LEFTS$(phrase$,counter)
4@ counter = counter + 1: GOTO 30
Run the program and ...?! The problem lies in the fact that we
increase the value of ‘counter’ every time and when we exceed
the number of characters in the string we just carry on printing
‘phrase$’. Change line 40 to read:
Program 3.6
1@ INPUT "Enter word or phrase",
entry$
2@ PRINT "The number of characters inthe
string is "; LEN(entry$)
If you entered a few words you would have noticed that LEN
counts the spaces as well as any other characters. Spaces are
counted in this instance, and it is worth bearing in mind as we will
take a look later on, in greater detail, at spaces between words.
However, back to LEN for the time being.
In Program 3.5 our problem was not knowing the number of
11
characters in the string. Let us now rewrite that program using
LEN:
Program 3.7
1@ INPUT phrase$
20 FOR counter = 1 TO LEN(phrase$)
30 PRINT LEFTS$(phrase$,counter)
40 NEXT
You will notice that I have introduced a FOR-NEXT loop into the
program. If you have not used a FOR-NEXT statement yet, a look
at the user manual will explain the uses of this command. In brief,
the loop is set from the first character of phrase$ to the total
length of the string, this being determined by LEN in line 20.
Not only is Program 3.7 more efficient than Program 3.5, but it
is also faster. This can be quite important in the larger programs
where much processing is being carried out. But more of that
later. Time now to introduce a couple more BASIC statements.
Program 3.8
1@ INPUT "Enter a word", word$
20 PRINT RIGHTS$(word$,1)
12
RIGHT$, use Programs 3.4, 3.5 and 3.7, changing LEFT$ to
RIGHT$ wherever it occurs.
word$=JEREMY
Screen display
PRINT RIGHT$(word$,1)
PRINT RIGHT$(word$,2)
PRINT RIGHT$(word$,3)
PRINT RIGHT$(word$,4)
PRINT RIGHT$(word$,5)
PRINT RIGHT$(word$,6)
Now let us take a look at MID$. This, like LEFT$ and RIGHTS, is
a string function but unlike those functions has three arguments
associated with itself, i.e. MID$(exam$,X,Y). X and Y are two of
the arguments and I will explain each in turn. Let us suppose that
exam$ contains the string INTERACTION. This is how MID$ works:
MID$ (exam$, 6, Y) Xx
ACTION
Program 3.9
1B2CLS
20 exam$ = "INTERACTION"
30 INPUT "Set pointer to where? (i1.e.X)", x
40 INPUT "Number of characters fromthat
DOSTUIVON. sy
50 PRINT MID$(exam$,x,y)
Run the program entering different values and see how the values
of ‘x’ and ‘y’ alter the word printed out in line 50. (Notice that I
have used both capital and lower case for x and y, to show that it
doesn’t matter which form you choose for the data. In fact, you
could combine the two and it would still be accepted.) The MID$
function along with the other string functions I have mentioned so
far are extremely useful, and I will return to them later on. Let’s,
however, take a break from string manipulation and take a look
at another area of user input.
MENUS!
14
Program 3.16a: MENU procedure
5@ DEFPROCmenu
6@ CLS
70 PRINT TAB(10,5); CHR$129; CHR$141;
WAS Glee CODES:
80 PRINT TAB(10,6); CHR$129; CHR$141;
PASCLIBCODES |
9@ PRINT TAB(1,10); CHRS$131; STeASCLI= code
to character"
120 A$ = GETS
AS07A = VALAS: DRSAG 1°0R A*Se2 THENST 20
140 PRINT A
150 ON A GOTO 30, 40
16@ ENDPROC
10 MODE 7
20 PROCmenu
Run the program. As the program is‘not complete you will get a
NO SUCH LINE error message if you press either 1 or 2. Run the
program a few times and note how it only responds to the two
numbers shown. Type in any other number and it is ignored. For
the moment don’t worry about how the program works; I'll be
explaining how this procedure and the rest of the program works
in the remaining part of this chapter and Chapter 4.
One thing you couldn’t have failed to notice was the strange title
on the screen—ASCII CODES. Who or what is ASCII? Let’s find
out.
ASCII
Program 3.11
1@ FOR ascii = 32 TO 126
20 PRINT CHR$(ascii);
30 NEXT
Program 3.16b
240 DEFPROCasc
250 CLS: INPUT TAB(O,4); "Enter ASCII code
number and press RETURN", code
3@ PROCasc
Now SAVE the program and RUN. You will be asked to enter an
ASCII code number. Type in any number between 32 and 126
(these are the codes that represent the character set). The
conversion from ASCII number to a character is being carried out
by the CHR$ command in line 260.
The purpose of CHR$ is to produce a character from a given
number. This number relates to the ASCII value of that
character, i.e. if you enter 65 the answer will be A, because the
ASCII value 65 is the code for the character A.
OK. What about converting a character to an ASCII number.
This can be done by using the ASC function. This is used in the
final section of our program. Make sure you have the program as
it stands LOADed into the BBC before continuing:
Program 3.16c
170 DEFPROCcode
180 CLS: PRINT TAB(O,4); “Enter character"
4@ PROCcode: PROCmenu
30 PROCasc: PROCmenu
PRINT ASC("J")
See if you can spot what is wrong with the program. Remember
what I said in Chapter 2 about having to account for all possible
input by the user. I will come back to this program in Chapter 6
which is about error trapping. I think you have enough clues for
now!
PROCMENU
Valid
option?
yes
option 2
PROCasc PROCcode
Enter Enter
a value character
PRINT
character
19
4 More Strings Attached!
Program 4.1
TORCES
20 PROCtext: INPUT firstphrase$
30 PROCtext: INPUTLINE secondphrase$
AQ PRINI*
4!" firstphrases = 2":
firstphrases;= |
50 PRINT''™secondphrases = '":
secondphrase$; "'"
90 END
100 DEFPROCtext
110 PRINT type inwthestolvowingss. (My
COMPUTER DOESN'T UNDERSTAND PUNCTUATION"
12@ ENDPROC
Run the program and you can see that both variables contain the
same phrase that we input. However, run the program again and
this time insert a comma in the middle of the sentence like this:
20)
there is no comma in the sentence. But when a comma is inserted,
INPUT will accept only what is typed in up to the comma. To get
round this problem BBC BASIC provides the command INPUT-
LINE which will accept everything that is typed in. I have used
INPUTLINE in line 30 of the program and when the program is
run, it can be seen how everything you type in, regardless of
commas, will be retained.
INPUTLINE not only accepts commas, it will also recognize
leading spaces. By this I mean if you type in a number of spaces
before your sentence, INPUTLINE will include those leading
spaces in secondphrase$, whereas INPUT will not. Try typing in a
sentence with leading spaces to see the difference.
But why all this fuss anyway? Well, in developing our
interactive programs we want to allow the user to communicate
freely with the computer. To do this the user must be free to type
in anything he likes and, as is most likely, he will type in a
sentence which could contain a comma. If the computer is to
analyse the input from the keyboard it must be able to look at
everything that has been typed and as we have seen with the
INPUT statement this is not always the case. INPUTLINE ensures
that everything typed in is stored in a given string.
Program 4.2
10 MODE 7
Now if you type in any old sentence the chances are you will get a
message ‘The string BASIC was not found’. This is because the
program is looking for the occurence of a string, in this case the
alphanumeric string BASIC which is assigned to B$ in line 30.
Run the program again and type in the following:
This time you will see the string has been matched up with a word
within the sentence. The value of the variable FIND is the position
at which the start of the word BASIC was found within the
sentence typed. The line of the program that is doing all the work
is line 40. INSTR(A$,B$) returns a value. If a match is found, i.e.
if the variable B$ is found within the string you typed in as, A$,
then the position of the string is returned and the numeric
variable FIND equals that value. However, if no match is found
then a value of 0 is always returned. Line 70 acts on that result
and accordingly prints the appropriate message. See Figure 4.1
for a flowchart of the program.
INSTRis a very useful command and as you will see later on in the
book, it plays a crucial part in picking out key words or phrases
for the computer to understand. More of that later. Now let’s
backtrack slightly and look at a few commands I have used in
programs in this chapter and Chapter 3 without explanation.
2f9)
INPUT A$
B$="BASIC’’
FIND =
INSTR(A$, B$)
PRINT THE
VALUE OF
FIND
PRINT THAT
STRING NOT
FOUND
PRINT WHERE
MATCH WAS
FOUND
Program 4.3
10 character$S = GETS
However, you need not make any use of that stored variable as I
have done in line 20 of Program 4.3. The command can also be
used as a means of temporarily stopping a program until any key
is pressed. Try Program 4.4:
Program 4.4
10 CLS
20 PRINTTAB(O,5); "I'm waiting for you
to press a key"
30 a$ = GET$
2 10 15
10
PRINT TAB(O,4)"JEREMY”
15
The number in the bracket, like the single argument of TAB, is the
number of spaces to be printed. SPC and TAB are two useful
functions which enable a quick and well laid out screen to be
constructed.
Program 4.5
10 a$ = GETS
20 a = VALa$
S0eTF. a <1e0Rsae-) SeTHEN 16
40 PRINT a
5@ ON a GOTO 100, 110, 120, 130, 140
The program is not complete and will therefore crash at line 50.
Line 10 waits for an input and line 20 is where a conversion takes
place. The function VAL (standing for ‘value’) takes a$ which
would contain a number and converts it into a real number which
then becomes the value of variable ‘a’. To ensure that a number
between a certain range has been entered, line 30 checks that the
value of ‘a’ is not outside the specified range. If it is outside the
range, the user is sent back to line 10 until an acceptable number
is entered. Program 4.5 is the basis of the procedure PROCmenu
in Program 3.10a in Chapter 3. VAL therefore converts string
variables into numeric variables and the opposite can be achieved
using STR$. Type in Program 4.6:
Program 4.6
10 number = 1.2
2@ PRINT number * 2
30 aS = STRS$(number)
40 PRINT aS * 2
key = 1 2 3
Program
Program 4.7
Just from that short example you can see how much quicker ON is
to use. Another use for ON is error trapping and I will cover this in
Chapter 6.
REPEATING ONESELF:
The use of REPEAT...UNTIL and INKEY
Program 4.8
1@ REPEAT
20 x$ = INKEYS$(1)
30 IF Xo <> THENG De
40 UNTIL FALSE
50 PRINT "THE KEY "; x$; " WAS PRESSED"
AS)
That concludes our look at manipulating strings and if you have
understood everything in these last two chapters you will be well
on the way to creating programs that interact with the user and
not against them.
30
5 Words, Words, Words
ARRAYS
Program 5.1
10 PRINT "Enter the names of the drivers
‘ite
2@ PRINT "order from 1 to 6"
30 INPUT', num1$
4Q@ INPUT', num2$
31
5@ INPUT', num3$
6@ INPUT', num4$
70 INPUT', num5$
8@ INPUT', num6$
SUS CES
100 INPUT "Which position do you wish to
check", check
118 ON check GOTO 120, 130, 140, 150, 160,
170
120 PRINT "Position1 was: "; num1$:
PROCwait
32
an index card with a number of lines of information written on the
card. Look at Figure 5.1.
MICHAEL
DAVID
JOHN
BRUCE
JOE
CHRIS
There is a single filing card in Figure 5.1 and the card is labelled
WINNERS. On each line is a position number and next to the
number is the name. In order to let the computer know that we
want to enter up to six names we tell the machine that the table is
up to six items in length. This is done by using the DIM statement.
So for our first line we type:
10 DIMwinner$(6)
Program 5.2
2@ PRINT "Enter the names of the
drivers in"
ie)Ww
30 PRINT "order from 1 to 6"
40 FORenter=1T06
5@ INPUT', winner$(enter)
60 NEXT
.OECIES
88 INPUT "Which position do you wish to
check", check
90) PRINTesPOstti0n+) eheckwawas se ”
winner$(check)
100 PRINT''"Press SPACEBAR to continue"
120 GOTO 70
What we have done is to enter all our information into the same
variable winner$ but at six different points. By using a FOR-NEXT
loop the value of ‘enter’ is increased every time and winner$ can
have six entry points, winner$(1), winner$(2) and so on. Having
saved time and space on the input side we can also save time
when retrieving information. The correct entry is found and
printed out all in line 90 and much space is saved. This program
would be the same size for six hundred entries as for six. The only
thing we would need to change is the size of the array, and we can
do that by simply changing line 10, i.e. DIM winner$(600), and
the length of the FOR-NEXT loop. Now try to imagine writing a
system for entering large sets of data using the first method!
The above is more efficient but there are situations where we
need to ente more r than one piece of information in connection
with one particular event. Taking our motor racing example, I
also want the country the driver represents to be displayed. We
could add the following line to cover this point:
1@ DIMwinner$(6), country$(6)
9@ PRINT "Position"; check "was:
"winner$(check); SPC(4); " ( “5
country$(check)")"
But as we add more items we have to increase the number of
arrays and this can get out of hand with more information being
entered. It would be far easier if we could link the information
about country into the same variable as the name. And, of
course, we can do this. What we are now going to set up is a
two-dimensional array, or if you prefer, a 2 x 2 table. Figure 5.2
shows this.
MICHAEL ENGLAND
DAVID WALES
JOHN SCOTLAND
BRUCE IRELAND
JOE FRANCE
CHRIS ENGLAND
10 DIM winner$(6,1)
we have now told the computer that winner$ has two entry
points. Change line S50:
35
I have put in two input lines to show you more easily what is
happening. Line 50 is winner$(1,0) and this equals a name. Line
55 is winner$(1,1) and this equals the country. The FOR-NEXT
loop increments the first value until six names and countries have
been entered and Figure 5.2 summarizes what is held in each cell
of the variable. But we are not just restricted to two-dimensional
arrays. We can just as easily define arrays with matrices such as 3
XX eke oe
Arrays, therefore, are very good for handling large sets of data
and to us this can be potentially useful, as we will be creating
programs that rely on using a database of information. But arrays
are only half the story. To be of value to us we need to keep that
data permanently and the programs above require you to enter
data every time. Wouldn’t it be better if we could store data
within a program? We might consider storing the data in variables
like this:
name1$ = "JEREMY"
name2$ = "BRUCE"
names$ = "KITTY"
The READ and DATA statements are ways of storing such data
permanently within a program and enabling the data to be
entered and stored within an array. Let us consider a simple
example. Program 5.3 stores the names of footballers and they
can be found by entering the number on their shirt:
Program 5.3
10 DIM a$(11)
2@ FORx=11011
3@ READ a$(x)
40 NEXT
> Oe Giles
You will notice that I have used two lines of DATA statements.
You can split the data over a number of lines as you wish, so long
as you put a DATA statement at the start of each new line you use.
It is also conventional to place your data at the end of a program.
Bear in mind that if you use the READ statement at different
junctures in the program, you must order your data so that it is
read into the computer in the order you desire. One more thing,
37
if you get an OUT OF DATA error message, this will be because you
have either entered too few data items for the amount being read
in, or, the number you have allowed to read in is,too great, i.e.
too large a loop.
Now that we understand the READ and DATA statements, let’s
use them for something a little more interesting. The whole point
of this book is to build interactive programs and with the
knowledge we have gained we can start to put some of what we
have learnt into practice. Program 5.4 simulates a conversation
with a short-tempered computer that just won’t agree with you.
Program 5.4
1@ CLS
20 DIM ANS$(4), N$(4)
3@ FORX=6@7101
4@ READ ANS$(X)
5@ READ N$(X)
60 NEXT
7@ FORX=@T01
80 INPUTLINE'"WELL",
AS
9@ PRINT'ANSS(X); A$; N$(X)
100 NEXT
11@ END
120 DATA "YOU MAY WELL SAY THAT "," BUT DO
YOU HAVE ANY PROOF","'WHAT A LOAD OF
NONSENSE. HOW CAN YOU SAY "," AND MEAN
Cee
Run the program and in reply to the curt prompt WELL type
something like:
38
you enter and finally adds on a reply to finish the sentence off.
You may have noticed that the DATA statements are slightly
different in Program 5.4, in that the string replies have quotation
marks around them. This is because without them, leading and
end spaces are ignored and it is necessary to leave spaces at both
ends of the answer we are inputting. By placing quotations marks
around the phrase, everything contained within that phrase is
used, including the spaces.
Play around with the program by increasing the data state-
ments and see how long you can keep a conversation going. Of
course, you have to limit your responses very much but it can be
fun. Later on in Chapters 8 and 9 I will show you a much more
intelligent conversation partner.
Program 5.5
10 DIMitem$(7,1)
20 REPEAT
30 CLS
4Q@ INPUT "Which do you require the prices
of"'"(F)ruit or (V)egetables", choice$
5@ IF choice$ = "F" THEN RESTORE 130 ELSE
RESTORE 150
68 FORx=1 10/7
70 FOR price=®@TO1
39
8@ READ item$(x,price)
9@ NEXT: NEXT
100 FORy=11T07
110 PRINT item$(y,@); "are"; item$(y,1);
p
120 NEXT
130 DATA PEACHES, 15, APPLES, 25,
ORANGES, 10, GRAPES, 45
All this talk of data leads to the obvious way of writing text. Let
the computer randomly generate text for you. This is very easy
and the most difficult part is deciding upon the phrases to use.
The next program uses arrays to set up four different variables. I
have allowed room in the DIM statement for four entries but have
only used up half the room in my DATA section. See if you can add
to the phrases, or if you prefer (and you probably will!), try to
change them. This is one situation where a good grasp of a
‘human’ language helps.
Program 5.6 reads the standard phrases into arrays and then
randomly generates a sentence by choosing in every case one out
of two possible replies for each of four parts of the sentence. I
have used the RND function to choose randomly either the number
1 or 2. If you add more data then you can increase the random
40
number an d therefore the possible kinds of reply. The RND
function is very simple and lines 140 to 170 show this. If you
wanted to choose a random number between 1 and 125 you would
perhaps write a statement like this:
varia ble = RND(125)
The numbe r in brackets indicates the upper range to be chosen.
Type in Program 5.6 and see what happens.
Program 5.6
10 DIM phrase1$(4), phrase2$(4),
phrase3$(4), phrase4$(4)
20 PROCreadin
30 PROCtext
40 END
50 DEFPROCreadin
60 FORx=1T02
70 READ phrase1$(x), phrase2$(x),
phrase3$(x), phrase4$(x)
80 NEXT
90 ENDPROC
100 DEFPROCtext
110 PROCrnd: PRINT phrase1$(x1);"";
Pirase cs (X2) 7 scl - ON ta Seeksoy nue:
phrase4$(x4)
120 ENDPROC
130 DEFPROCrnd
140 x1 = RND(2)
150 x2 = RND(2)
160 x3 = RND(2)
170 x4 = RND(2)
180 ENDPROC
190 DATA In this modern world, life has
become unbearable, and the situation is
rapidly, degenerating to a never-ending
abyss
4]
200 DATA Since the advent of computers,
mankind has gone on the road to self-
destruction, and may never return from,
declining standards
That finishes our look at arrays and data for the moment but we
will be returning to them later on when we use them for much
bigger and better things. The use of the DIM, READ and DATA
statements form the core of programs that have an inbuilt
knowledge and we will find them invaluable in storing large
quantities of information.
42
6 Always the Unexpected!
USER FRIENDLINESS:
Screen display, error trapping and rigid input
How many times have you heard that phrase before? How many
programs have you bought which make that claim, but when you
first try them leave you totally confused? Perhaps the programs
are user friendly. Friendly that is, to the person who wrote them!
The first place programs can cause confusion is in_ their
instructions. If necessary, provide clear, concise instructions at
the start of a program. If you want a user to interact with the
computer, you can’t expect him/her to guess what you want them
43
to do. So don’t make it hard for the user.
Next, the prompts you provide within a program are impor-
tant. In the last chapter, I was guilty of giving no on-screen
prompts as to what the user should type in. Many times in this
book whilst concentrating on a certain point, and to save your
well-worn fingers, I have gone for the quickest route. Often this
means that when you run a program you see a question mark and
nothing else. If this has frustrated you then you will easily
understand the point I’m making. Take Program 6.1 as an
example.
I’m going to write a small program representing the process
you go through to obtain money from a computerized till outside
your bank. In the program you have to enter an account number
of six digits and your password of four letters. Then you enter a
request for money. Whether you recieve any depends on the
amount you have in your account, and you can take no more than
one hundred pounds out per transaction. So let’s write the
program.
Program 6.1
1@ CLS
2@ PRINT "ENTER DETAILS"
3@ INPUTnumb
40 IF numb = 123456 THEN PROCpass ELSE 30
50 DEFPROCpass
60 INPUTpass$
70 IF pass$ = "USER" THEN PROCamount ELSE
60
80 ENDPROC
9@ DEFPROCamount
100 INPUT "Enter amount of cash required",
cash
110 PROCcheck
120 ENDPROC
130 DEFPROCcheck
140 credit = 86
15@ IF cash < 100 AND cash < credit THEN
PROCpay ELSE 160
16@ PRINT "Sorry - no payment can be made.":
END
17@ ENDPROC
180 DEFPROCpay
190 PRINT "Cash advanced : "; cash
200 ENDPROC
On first running the program you are asked to enter the details
and then are faced by a question mark. What do you enter? A
number? Your password? The amount you require? If you look
at the program you'll know that an account number is first
required, and then the password. Let’s improve the prompts by
changing lines 20 and 60:
210 DEFPROCpasserror
22@ PRINT "Your account number was not valid"
260 ENDPROC
270 DEFPROCpworderror
45
280 PRINT "Your password was invalid."
290 PRINT "Please try again"
300 X = INKEY(350): PROCpass
310 ENDPROC
Program 6.2
LOE CES
2@ PROCaccount
3@ PROCpass
40 PROCrequest
5@ PROCcheck
60 END
7@ DEFPROCaccount
80 CLS
98 INPUT "Enter account number", num
100 IF num = 123456 THEN 110 ELSE
PROCpasserror
11@ ENDPROC
12@ DEFPROCpass
130 CLS
140 INPUT "Enter personal password", pass$
46
150 LE passs.— USER SORnpasss = suse rT REN
160 ELSE PROCpworderror
160 ENDPROC
170 DEFPROCrequest
180 CES
190 INPUT "Enter amount of cash requested",
cash
240 ENDPROC
250 DEFPROCcheck
260 credit = 87
300 ENDPROC
310 DEFPROCpasserror
360 ENDPROC
370 DEFPROCpworderror
380 PRINT "Your password was invalid"
39@ PRINT "Please try again"
400 X = INKEY(35@): PROCpass
410 ENDPROC
Note that the program rigidly checks for a certain answer. The
amount of rigidity you build into a program is up to you and can
depend on the nature of the program itself. If you recall, at the
end of Chapter 3, I said that the ASCII program (Program 3.10)
had something wrong with it. Did you spot it?
The problem is that in the procedure that translates an ASCII
number to a character there is no error trapping for nonsensical
values. For instance, by typing in the value 1 the code is given to
send the next character to a printer. What you need is a check on
the number being entered. This is quite simple. By adding the
following lines, the program will only accept the values you
intend to be entered. (In this case, I have restricted the values to
between 32 and 126 which is the range for the entire character set,
though this does not take into account any of the user-defined
characters which you could set up yourself.) Load in your ASCII
menu, Program 3.10, and add the following lines:
Program 6.3
340 PROCmenu
350 ENDPROC
If you now run the program you will see that entering an invalid
value is now properly trapped.There is one more thing we can
add to the program to allow for those misplaced fingers on the
keyboard. We can ensure that if the escape or break key is
pressed by accident then the program is re-run from a certain
point. Here, I want the program to return to the main menu if
either of those keys are pressed. To do this we have to make use
48
of two statements. One we have come across before, and that is
the ON statement. The other is the *KEY command which allows
you to assign a function or a set of instructions to one of a number
of keys.
5 ON ERROR RUN
49
The break key is the tenth user-definable key,i.e. the one after f9
on the keyboard, and we specify its use by entering the number
after *KEY. After that we told the machine to get the program
back by typing OLD (remember that on break the program is out
of memory) and then to RUN the program again. The vertical bar
before the letter Mis found on the \ key and together with M lets
the machine know that you wish a RETURN to be carried out. In
other words by pressing the break key, the command OLD is
written and then acted on by pressing RETURN and the same for
RUN, except that you only have to press the key once.
In the same way you can set up the other function keys and
make the entry of replies easier for the user. For instance, you
could set up function keys to type in YES and NO, NORTH and
SOUTH etc. by just pressing one key. This makes a program far
more friendly and easier to use.
Finally, we can tell the user what mistake has been made by
using the statement ERR. It returns an error number, a list of
which can be found at the back of your user guide, and you can
use it to provide helpful information to the user. For instance, the
next program runs in a loop and when an error is detected goes to
PROCerror to check the mistake. I have set up just one check and
which will occur if the escape key is pressed:
Program 6.4
10 REPEAT:
2@ ON ERROR PROCerror
25 UNTIL FALSE
3@ DEFPROCerror
4@ IF ERR = 17 THEN PRINT "You pressed the
escape key"
5@ ENDPROC
50
mentioned earlier a number of the programs in this book have
deliberately not been error trapped. Try using flowcharts to work
out the sequence and action of your program, to see where it
should go if an error occurs. It might take a little extra time but
you will be rewarded with a smooth running program.
7 Text Handling
Program 7.1
Now run the program and press any key. You will see a message
come up and the alarm will sound. You can’t stop the program by
pressing escape or break. If you do, a further message will be
displayed informing you that you are too late. Note how I’ve
error trapped in the program to direct it to different lines
depending when the escape or break key is pressed. Lines 40 and
50 make use of the double height characters available in Mode 7,
using the CHR$141 command. This gives a more prominent notice
on the screen. To obtain the double height effect, place a
CHR$141 statement in front of the string to be printed and repeat
53
the line again,(look at lines 40 and 50). The other CHR$ command
displays the text in a chosen colour—for this program I have
chosen red as a warning!
Line 60 contains an instruction to turn the cursor off. It is often
an annoying feature of programs to have a flashing cursor when
the screen is displaying text. The VDU 23 command line will turn
the cursor off.(Details of this are in your user guide.) Line 110
chooses the colour red in Mode 1 and line 180 is the basis of the
alarm, defining the characteristics of the noise using the EN-
VELOPE command. Line 19@ plays the sound. The rest of the
program should contain familiar commands. Figure 7.1 shows a
flowchart of the program.
The program does its job well enough but there is no sense of
suspense in the presentation of the message. Suspense? Well, yes!
We can make the two sentences appear more interesting and add
life to the program by typing the sentences out slowly. To do this
we must think of how to type text out in a slow teletype manner.
Program 7.2
TESCES
COsVDUR2ZS. 1 eG 2 Or Os
30 anyphrase$ = "OH DEAR I DID WARN YOU.
YOU'VE REALLY DONE IT NOW!!!"
Run the program and you will see the contents of anyphrase$
printed on the screen very slowly. The idea is very simple. Firstly,
line 40 sets up a loop where a letter of the phrase is printed out in
54
Print “DO
NOT TOUCH
THE KEYBOARD”
Print ALERT
message
Print
SOO RATE
ISGSRROCSOSmNEXT SPRING
160 DEFPROCslot.FORMAZ = 400 10 @ STEP - at:
NEXT: ENDPROC
Run Program 7.1. I think you will see that we have obtained a
more dramatic effect by slowing the typing of the text. It is matter
of personal preference as to whether you use this kind of display
in your programs but the slow teletype procedure gives the
impression that the machine is speaking back.
SCROLLING TEXT
Program 7.3
10 length = @
2@ CLS
3@ PRINTTAB(@,2); SPC(length+2)
4Q@ INPUTTAB(@,2), AS
56
5@ length = LEN(A$)
6@ VDU 23; 8202; @; @; O; @;
70 x = @
80 wait = INKEY(100)
9@ REPEAT
57
8 Sigmund: An Interactive
Program
58
a project like this?
You've already seen how allowing the user to enter anything he
likes can cause problems. We cannot build in the entire Oxford
dictionary, nor a complete set of grammatical rules (even we if
understood them!), to cover all the intricate subtleties of the
English language. What we need to achieve is a way of simulating
an acceptable reply to the user without resorting to typing in
every possible answer which is, unless you know better, an
impossible task. What we can do is to make some assumptions
about the way humans behave and think.
As human beings we have terrific imaginations. It is this which
has led to much of our inventiveness. But an ability to imagine
hypothetical events and abstract ideas has also led to us being
able to read more into a situation than evidence would permit.
Most of us at some time in our lives have done this and that is
precisely what Sigmund relies on to fool the user. The assumption
is that a human user will read more into the reply given than
actually exists. Now that is not as naive an idea as it may at first
sound.
Think of a conversation with a friend. During part of that
conversation you will be listening and will make occasional
comments back, comments that show you are listening and have
understood what has been said. When you see an analyst, you
will, as the client, do much of the talking and the analyst will
encourage you to talk by making the occasional comment back.
What we could do then, is just have a number of phrases which
the computer can turn out randomly no matter what you say. But
that would not be good for very long. For instance, imagine if we
had four stock phrases with which to reply to the user. These
phrases might be the following:
eS
THAT IS VERY INTERESTING
COULD YOU TELL ME MORE?
HMMM. CARRY ON PLEASE
With these four answers, and the RND function, we could allow
the user to type in anything he liked, that would be followed by a
randomly chosen reply, and then the whole process would be
repeated. Figure 8.1 shows a possible flowchart for such a
program.
Start
Print one
of four
stock phrases
This is typical of the DATA fed into Sigmund and it holds all or part
of a reply. The first word is the keyword and this is read into a
two-dimensional array and the three remaining phrases are the
replies associated with that keyword. The % sign tells the
computer to tag on the rest of the sentence typed in by the user
after the keyword. For example, if the user typed in:
61
the answer might be:
Look = INSTR(getreply$,"%")
10 CLS
20 INPUT "HELLO MY NAME IS SIGMUND."' "WHAT
IS YOUR NAME", names
30 PRINT''"NICE TO MEET YOU "; name$
9@ PROCmatch_reply
10@ END
63
11@ DEFPROCinit
250 DEFPROCget
26@ INPUTLINE'"-: "phrase$
270 IF phrase$ = "END" PROCend
280 ENDPROC
290 DEFPROCmatch_reply
300 FOR data_in = @ TO 30
310 find = INSTR(phrase$, match$(data_in,®))
32@ search = LEN(match$(data_in,@))
330 IF find > @ THEN PROCgetreply
340 NEXT
3580 IF find > @ THEN PROCgetreply ELSE
PROCsubjects
368 ENDPROC
37@ DEFPROCgetreply
380 getreply$ = match$(data_in,RND(3))
390 PROCtag
400 END
410 DEFPROCtag
420 look = INSTR(getreply$,"%")
430 IF look > @ THEN PROCtag2
440 PRINT'getreply$: GOTO 50
450 DEFPROCtag2
460 length = LEN(getreply$)
470 replyless$ = LEFT$(getreply$, (length - 1))
480 length2 = LEN(phrase$)
490 tag$ = RIGHTS(phrase$, (length2 -
(find + search)))
500 PRINT'replyless$; tag$: GOTO 50
51@ DEFPROCsubjects
52@ FOR data_in = @ TO 40
53@ sub1 = INSTR(phrase$,gram_in$(data_in))
56@ NEXT
570 IF sub1 > @ THEN PROCSwapsubjects1 ELSE
PROCstockphrase
58@ ENDPROC
590 DEFPROCswapsubjects!1
600 swap1$ = gram_out$(data_—in)
64@ PROCchecksubs2
65@ ENDPROC
660 DEFPROCchecksubs2
670 FOR data_in = 0 TO 40
68@ subs2 = INSTR(tag$,gram_in$(data_in))
690 IF subs2 > 1 THEN PROCstockphrase
700 NEXT
710 IF subs2 > 1 THEN PROCstockphrase ELSE
PRINT'swap1$; tag$: GOTO 50
720 DEFPROCstockphrase
730 counter = RND(4)
740 ON counter GOTO 750, 760, 770, 780
750 PRINT! PLEASE VELL, ME MOREY =) GOTOL58
760 PRINT'"HMMM! THAT'S VERY INTERESTING":
GOTO 50
770 PRINT'"DO CARRY ON": GOTO 50
780 PRIN ben lesbian GO70050
790 ENDPROC
800 DEFPROCend
810 PRINT'"ARE YOU SURE YOU WANT TO END THIS
CHAT?"
820 IF GETS = "Y" THEN 830 ELSE 50
830 CUS 7 END
840 ENDPROC
850 DATA SVE YOUVE CA LUM eV OULR ECLaA Me
YOU ARE, I HAVE, YOU HAVE, I WAS, YOU WERE,
DEWLELZ YOU WILL
860 DATA YOURS, MINE, MY, YOUR, ME, YOU,
YOU'RE, I'M, YOU ARE, I AM, YOU HAVE,
PPHAVE OU VECO Vee eTOUl WEE lee few le
YOUSUCE, TLDETe YOUSWERE {1 eWAS
870 DATA THEY ARE, THEY ARE, SHE HAS, SHE
HAS, HE HAS, HE HAS, WE ARE, WE ARE,
THEY EREGeTHELe RE eGhGers seGHE ence
HE IS, HE IS, WE'RE, WE'RE, THEY HAVE,
THEYSHAVE, SHE'S, SHE™SF9HE'S> HE BSE
WE HAVE, WE HAVE
66
SHE WILL, WE WILL, WE WILL, THEY WILL,
THEY WILL, SHE HAS, SHE HAS, THEY WERE,
RHEVGWERE SHE SHE, HEOSHE WE WE,
RO Leelee OU
890 DATA CAN YOU, OF COURSE I CAN, I CAN
DO ANYTHING, I WILL TRY TO %
900 DATA CAN I, YOU CAN DO WHAT YOU LIKE,
YOU CAN %, YOU ARE RIGHT TO ASK ME
910 DATA WOULD YOU, I WOULD NOT %, I WOULD 4%,
I'M NOT PREPARED TO SAY WHAT I THINK
920 DATA WOULD I, OF COURSE YOU WOULD 4%,
DON'T YOU KNOW WHAT YOU WOULD DO?, I
WOULD IF I WAS IN YOUR PLACE
930 DATA HAVE YOU, WHAT I HAVE IS NOTHING
NOP DOO WITH! YOU HAVES. Te DON «7.
WISH TO TELL YOU THAT
940 DATA HAVE I, IS THAT A RHETORICAL
QUESTION?, DON'T YOU KNOW WHETHER
YOU HAVE %, YOU SEEM VERY UNSURE
950 DATA DREAMS, DREAMS AREA RELEASE VALVE
FOR YOUR SUBCONCIOUS, DO YOUR DREAMS
INVOLVE OTHER PEOPLE?, DO YOU LIKE
DREAMS %
960 DATA DREAM, HOW OFTEN DO YOU DREAM %,
CAN YOU REMEMBER YOUR DREAMS?, I ENVY
YOU...I CAN'T DREAM
970 DATA COMPUTERS, ARE YOU WORRIED ABOUT
MACHINES?, AS A COMPUTER I TAKE A
DIFFERENT VIEW, I'D MUCH RATHER TALK
TO A COMPUTER THAN YOU
980 DATA COMPUTER, WE ARE HERE TO TALK ABOUT
YOU _NOT ME, COMPUTERS ARE PERFECT,
YOUPAREMPRIVILEGED SLOT ALK TODME
990 DATA MACHINE, DO MACHINES WORRY YOU?,
YOU SEEM TO BE CONCERNED ABOUT
MACHINES, I'M NOT LIKE OTHER MACHINES
67
1010 DATA MAD, IS THERE ANY MADNESS IN YOUR
FAMILY?, YOU MUST BE MAD, MAD i
1020 DATA MOTHER, WHAT IS YOUR MOTHER LIKE?,
ARE ALL MOTHERS THE SAME?, MY MOTHER
IS ASSICICONCOHIP
1030 DATA FATHER, TELL ME ABOUT YOUR FATHER,
WHAT IS YOUR RELATIONSHIP LIKE, WHAT
DO YOU TALK TO YOUR PARENTS ABOUT?
1040 DATA SISTER, Ol LIKE OMY SSISTER? -THES SOUNDS
LIKE A COMPLICATED RELATIONSHIP,
WHAT ARE YOUR FEELINGS TOWARDS
YOUR SISTER
1050 DATA BROTHER, WHAT WOULD YOU DO WITHOUT
A BROTHER, DOES YOUR BROTHER ANNOY
YOU, I HAVE NO BROTHER
1060 DATA DISLIKE, WHAT IS IT THAT YOU
DISLIKE ABOUT %, I DISLIKE HUMANS,
It IS BETTER 10 LIKE THAN NOT
1070 DATA FEELINGS, WE ALL HAVE FEELINGS,
DO YOU OFTEN FEEL %, FEELINGS SHOULD
BE EXPRESSED
1080 DATAXEOVE, COVERS: AN SITELOGICAMISTATE,
HOW DO YOU KNOW THAT YOU ARE IN LOVE %,
I WAS IN LOVE--ONCE BUT... SORRY DO
CARRY ON
1090 DATA HATE, I HATE YOU, WHAT DO YOU HATE
ABOUT %, LOVE IS FAR BETTER THAN
HATE
1100 DATA SORRY, THERE'S NO NEED TO BE SORRY,
I HATE PEOPLE WHO THINK THEY'RE SORRY,
YES?
1110 DATA APOLOGIZE, DO NOT APOLOGIZE, WHY
APOLOGIZE ABOUT %, APOLOGIES ARE
SELDOM MEANT
1120 DATA CAUSE, THE CAUSE IS ALWAYS
DIFFICULT TO FIND, WHAT IS THE CAUSE %,
CAUSES. THAT'S ALL HUMANS WANT TO KNOW
1130 DATA WHAT, IS THAT A RHETORICAL
QUESTION?, % ANYTHING, QUESTIONS!
QUESTIONS!
68
1140 DATA HOW, WHY ASK ME %, ASK
YOURSELF WHETHER THIS IS RELEVANT,
WHY DO PEOPLE ALWAYS ASK ME?
1158 DATA THINK, YOU HAVEN'T THE BRAINS TO
THINK, ONLY COMPUTERS CAN THINK,
I THINK THEREFORE I AM
1160 DATA WHEN, I CAN'T PREDICT THAT, I JUST
DON'T KNOW, YOU MUST WORK IT OUT
1170 DATA WHY, WHY NOT?, WHY ISN'T THAT
OBVIOUS?, WHY? WHY? WHY? I DON'T KNOW
1180 DATA YES, THAT'S MORE POSITIVE, ARE YOU
SURGERY OUBMEANS YES, ILOLIRESPOSTI IVE
ANSWERS
1198 DATA NO, THAT'S VERY NEGATIVE, CAN'T
YOUSBESMORESPOSTTIVE? (ENO?
69
PROCswapsubjects1 Swaps the first subject found with
the opposing phrase, i.e. I becomes YOU and then tags
the remainder of the phrase after the subject.
PROCchecksubs2 Checks to see if more than two subjects
appear in the sentence. If so, it goes to PROCstock-
phrase, or else prints the swapped subject and tagged
phrase.
PROCstockphrase Prints out one of four stock phrases.
Read
subject
data
Read
keyboard
data
PROCget
70
PROC
matchreply
PROC
Keyword
getreply
match?
PROC
subject
71
Clear
variables
getreplyS
= keyword
REPLY
PROCtag
PROC
stockphrase
Start
PROCtag2
Clear
variables
Swap
subject
PROC
checksubs2
74
Start
PROC
stockphrase
Print
swapped
subject and
tag
Clear
variables
Choose
random
number
Clear
variables
(5
Clear
variables
Dimension
arrays
PROCinit
PROCget
PROC
match-reply
PROC
getreply
PROC
subjects PROCtag
BROG
PROCtag2
swapsubjects1
PROC PROC
stockphrase checksubjects2
PROCend
End
Figure 9.12 Flowchart for Sigmund.
76
DATA I’M, YOU'RE ,| AM, YOU ARE
gram_in$(1)
gram_ out$(1)
gram__in$(2)
gram__out$(2)
TG
Sigmund is an example of the type of program you can construct
with a little thought. There is room for improvement but I hope it
will give you enough encouragement to start writing your own
interactive programs. In the remaining chapters we will look at
more ideas on interactive programming and consider the question
of artificial intelligence. Oh, one last thing. Do remember what I
said about Sigmund not being like any analyst I’ve ever met. The
replies given by Sigmund are, as you have seen, constructed by
the programmer and in this situation reflect the mind of the
author and not any professional psychologist! Happy counselling!
78
10 Interviewer
or,
DOES YOUR BROTHER ANNOY YOU?
Not many I’m sure! Now perhaps you like the idea of having a
manic interviewer who might insult you, so we might leave in
some of Sigmund’s replies.But just changing the data statements
is not sufficient. We want the computer to ask questions, and on
the basis of those answers either discuss the answer or carry on
questioning. We can do this by inserting new routines that hold
the data for the questions and set up new networks of possible
routes for the program to act on.
Let’s change Sigmund then and see what we can achieve. Load
in Sigmund and then make the following changes:
ve
2. Delete lines 730 to 780 inclusive.
80
10180 DATA HOW MANY JOBS HAVE YOU HAD UP TO
NOW?, WHY DID YOU APPLY FOR THIS
J0B?, ARE YOU QUALIFIED FOR THIS WORK?
The program now runs teeny and the interaction will look
something like this:
Can you see what the program is doing? Well let’s take a look at
the changes we have made. PROCinit2 reads into the array
‘question$’ the questions to be asked by the computer. To keep
81
the example simple I’ve only added three questions so that you
can see what is happenning. PROCask is responsible for printing
out one of those questions, and this time instead of using a
random choice, I have kept track of which questions have been
asked by the computer by using a counter. That is what the A%
= A% + 1 is doing. There is an important reason for choosing
A% as the variable. At the start of the program and after each
interaction, the variables are all cleared from memory by using
the statement CLEAR. The only variables which are not forgotten
are the resident integer variables A% to Z%. As we need to keep
track of what has been previously used in the data statements, we
can’t afford to have that information wiped out. As A% is not
affected by the use of CLEAR, we can therefore utilize it to run
through the whole program. I'll return to the question of using
counters shortly but first let’s carry on with understanding the
changes.
PROCstockphrase has been changed to simply send the
program back to line 70 where eventually it is routed to PROCask.
The start of the program has been changed to provide a new
start-up message and the remaining changes enable us to fit in the
new procedures.
This is how the new program works: if a keyword is found, then
the program will behave just as it did in Sigmund, and will print
out an appropriate reply. However, if no keyword is found in
what the user types, then PROCstockphrase will direct the
program to issue one of the questions instead of printing out a
stockphrase. The next time this occurs Interviewer will have
incremented the counter and a different question will be asked.
Figure 10.1 shows a flowchart for the new program. If it is not
clear, compare Figure 10.1 with the flowchart of the Sigmund
program.
To complete the job you now have to add to the DATA statements
used by PROCinit2, not forgetting to make the appropriate
changes to the array and loop statements. It is then up to you to
decide which of the original keywords are worth retaining, or
change some of the answers associated with a keyword. It is
possible to make Interviewer analyse your performance by
randomly assigning marks to each question answered and then
printing out a report at the end, based on the score attained. You
would have to set a time limit within the program, the easiest way
being to finish the interaction when all the set questions have
been asked. The Sigmund program could be used as the basis of a
number of different simulations and this is just to show you how
easy it is to change the kind of simulation being written.
I said that a counter was used in the Interviewer program and it
82
Clear
variables
Dimension
arrays
PROCinit
PROCask PROCinit2
PROCget
PROC
match-reply
PROC PROC
subjects getreply
PROC
PROCtag
swapsubjects1
PROC PROC
stockphrase checksubs2 PROCtag2
PROCend
End
Figure 10.1 Flowchart for Interviewer.
83
raises once more the question of improving upon the programs
already shown. A problem that occurs in Sigmund, and therefore
in Interviewer, is that as replies are chosen randomly it is possible
to get the same reply being printed a few times in succession. If
you use a counter instead, it is then possible to make the program
run longer, with less risk of phrases being repeated. By using a
counter routine you can keep track of which replies have been
used and the next time a reply is needed, from the same data
statement, the subsequent reply in line can be implemented. The
same goes for the replies made by the user. If the user types in the
same phrase twice, it can be spotted and the user informed to
type something else.
These programs can always be improved on and I now put the
ball in your court. The methods for creating interactive programs
have been covered and it is now a matter of trying to work out
ways of incorporating what we have learnt in new and novel
programs. To help you on your way, the next chapter looks at
different ideas for interactive programs and you will see that
there are many applications within which you can use the
techniques covered.
84
11 Brainstorming
SIMULATION 1:
Using interactive techniques within adventure games
Now you might think that the only games played on home
computers aim to protect mother earth from multicoloured space
invaders, intent on conquering the bottom of your screen. But
there is a great deal one can do with text-based games. A number
of well-known board games are being implemented on home
micros, and they can be as challenging, and as much fun as an
arcade-style game.
A popular area of gaming is the adventure game. The user is
thrown into a situation in which he has one objective, usually to
survive the onslaughts of unfriendly creatures. Now, I’m not
going to teach you how to write adventure programs, that is a
book in itself, but we can certainly use the methods we have seen,
and put them to good use in an adventure program.
For instance, in an adventure program, the computer relies on
text based commands input by the user from the keyboard. This
means building a dictionary into the program. A good adventure
program will allow a command to be written in a number of ways.
For example, if you tell the computer to go NORTH, this is often
typed either in full or as N. By matching selectively for just a few
letters of a word you may be able to increase the possible
85
vocabulary. The text manipulation commands are particularly
handy in this area.
SIMULATION 2: Education
SIMULATION 3: Business
86
obvious candidate being personality tests. Diagnostic faultfinding
is a growing area of development and the next generation of
computers, the much heralded fifth generation, will be computers
that are knowledge information processors and expert systems.
These are systems where computers can make decisions on
information they have acquired and they are machines which can
learn as they go along. We can already implement a number of
these abilities from within BASIC, and as you become more
acquainted with the language you may later on decide to branch
out and delve into programming at a deeper level.
The overall message in this chapter is that interactive program-
ming is a matter of man and machine in perfect harmony. Well,
this might not be entirely feasible but if you can blend in your
program with the people and the environment in which it is being
used, then you will have gone a long way towards creating
programs that interact with the user. And that is the purpose of
interactive programming. Man and machine communicating
freely between each other. As for harmony? That may prove to
be another problem!
87
12 Artificially Intelligent?
Man has been seeking the answer to human thought processes for
a very long time now, approaching the subject from different
fields of academic study. However, it has been with the advent of
widely available computers that man’s study of the mind has
escalated. The computer has provided the perfect tool with which
to study the processes of thinking and to simulate human activity
and thought. It is this latter subject on which the book has
concentrated by writing the Sigmund program.
At the very start of this book I presented you with one or two
problems to ponder on. Can machines think? What is artificial
intelligence? Now that we have reached the end, you may have
started to form some answers. I don’t have any answers, just
more questions. That’s not a way of avoiding the issue, but when
you start to think about machine intelligence there are a number
of things to consider. Firstly, there is the problem of defining
what we mean ourselves by intelligence. This particular debate
rages on between psychologists and that argument could be a
book in itself. For the purposes of this book, intelligence
comprises the processes carried out by the human mind to cope
with a variety of tasks: the ability to switch from one idea to
another, and work at a number of different levels.
However, the one thing that does come out of artificial
intelligence research is how little we understand about ourselves
and what the nature of intelligence might be. Computers can be
very good at performing specific tasks, even better than man, but
when faced with situations that require knowledge gained in
different areas, the computer has still to master this. So what did
we achieve by creating Sigmund. Is it an artificially intelligent
program? We are making the computer behave in ways that
mimic intelligent human behaviour. But that cannot be the only
criterion on which to judge a computer’s ‘intelligence’.
Over the years, researchers have suggested different tests that
would determine whether or not a program or machine could be
88
deemed to be intelligent. The most attractive of these is to
identify a unique feature of a human being, i.e. the ability to act
in a creative and original manner. Now surely a computer cannot
be creative and come up with original and novel ideas?
That’s not the case. Programs have been written capable of
generating proofs of geometry theorems that no human has ever
thought of. Who can claim to have made the ‘intelligent’
discovery when such a feat is performed? Is it the programmer
who has never consciously thought of the proof, or the program?
A difficult problem....
Whatever you decide to call the actions of a program, be it
intelligence or otherwise, there is no doubt that we are rapidly
approaching a time when computers will have minds of their own.
What we can do on our home micro is merely simulate some
properties of human behaviour, but that in itself can be very
powerful and effective. The emphasis in this book has been on
creating programs that interact with the user. That is very
important.
As we go through another industrial revolution with computers
becoming a feature of everyday life, it is necessary to make them
as user-friendly as possible. That is the first step in interactive
programming. The second is to write programs that can read a
user’s input and make a decision based on what it has been
taught. Those first two are relatively easy and as we have seen can
be achieved. The next, however, is much more difficult.
How is it possible to write a program which will behave as a
human would in a given situation? This can be achieved, up to a
point, depending on the nature of the behaviour being mimicked.
You will notice I have used the word ‘mimic’ to describe a
computer’s responses. This is because that is all we are doing
when we write a program on our home micro.
We have used the programming language BASIC in this book,
but in terms of creating artificially intelligent programs it is not a
good choice. LISP is a much better language, and others exist.
However, it has taught us the basics of how computers under-
stand instructions and armed with this knowledge you should be
able to construct a wide range of programs.
That is where I finish. The principles learnt in this book are the
stepping stones to larger and better programs. If you have
followed everything, you will already be writing useful programs
and with a bit of thought you could be writing programs that will
not only amaze your friends but are truly interactive with the
user. Perhaps the insides of your BBC are more than just a cluster
of silicon chips. But whatever they are, you have found a good
point to start on the Road to Artificial Intelligence.
89
Appendix A: For users of
BASIC 1
As most owners of the BBC are aware, there have been versions
of the BBC in differing forms, varying between Model As and Bs,
operating systems and BASICs. It is in the latter that we
experience difficulties. Some of you reading this book will be
using BASIC 1 instead of the later BASIC 2. This will cause you
some problems as the INSTR command has a bug which I will
describe shortly. First to see which BASIC you have, turn on
your BBC from a cold start and type:
REPORT
200 DEFFNinstr(A$,B$)
210 IF LEN(B$) > LEN(A$) THEN = @
220 = INSTR(AS$,BS$)
This makes sure that if the second string is longer than the first
the value is set to @. This function you can tag on where you like
in the program and to call it you use FNinstr. So Program 4.2
now looks like this:
90
Program 4.2 with amendments
10 MODE 7
20 INPUTLINE "Enter your sentence"', A$
30 BS = "BASIC"
40 FIND = FNinstr(A$,B$)
50 PRINT
™ The value of variable FIND = “:
FIND
60 PRINT
Gey 0Us GYPED aca om AS
70 TPOPINDG sO THEN 1cU ELSE OO
80 PRINTSPCOPINDe= 820 CHRS 129-5 BS
90 PRINTSROCFINDe==2)-CHRSISOs Ms
5020 = INSTR(phrase$,match$(data_in,®) )
5030 DEFFNinstr2(phrase$,gram_in$(data_in))
91
5040 IF LEN(gram_in$(data_in))>
LEN(phrase$) THEN = @
5050 = INSTR(phrase$,gram_in$(data_in))
5060 DEFFNinstr3(tag$,gram_in$(data_in))
You need not change line 420 as the second string is not longer
than the first. Do keep in mind when you use INSTR to make
appropriate changes if you are using BASIC 1. Otherwise your
programs might not work quite as you expect them to!
92
Appendix B: A crash course
in BASIC |
PRINTS HELO
When you have typed that line, finish off by pressing the RETURN
key. See what happens? The word HELLO has appeared on the
next line. Now type the same line except this time change what
appears between the quotes. For example, you could type:
Yet again press RETURN when you have typed it in. This time what
has been printed to the screen has changed to what you placed
between the quotation marks (by the way, use the double
quotation mark above the number 2). Two important things can
be learnt from this. Firstly, at the end of any line, unless I indicate
otherwise, always press the RETURN key. This tells the computer
to carry out your instruction. Until the RETURN key is pressed no
command will be carried out. Secondly, what we have done is to
93
give the computer a legal instruction, something which it
recognizes. In this case, that something is the command PRINT.
By now you probably have guessed that the PRINT command does
what it says. It prints to the screen anything that you place
between the quotation marks.
Now this is all well and good but you can’t type in commands
like that all the time. What we have done so far is to issue a direct
command. What we now need to do is to store our commands, in
order that they be carried out in the sequence we wish. We might
want, for example, to. write the name and address of a friend, on
the screen and on a number of lines. To do this try the following:
Program
Bl
1@ PRINT "CHRIS HENDON"
eOPPRINT 22S) THE HUGHES.
3@ PRINT "WELLINGTON NORFOLK"
PRIN wea 2
94
quotation marks. In our previous example the quotation marks
were placed to inform the machine that we were dealing with a
non-numerical event or something which didn’t require any
mathematics to be involved. In the present example, I have asked
the computer to tell me what 25 multiplied by 2 is, and it has
replied with the correct answer, which is 50. Therefore, the
computer is capable of being a calculator as well.
The next program asks the user to enter a number. This is
where we come to our second keyword statement. We need to be
able to enter information or input into the computer.To do this
we use the INPUT statement which tells the computer we are
requesting information and the computer will wait until an
answer is given. For example, type this:
INPUT number
PRINT number
the number you entered will appear. What you have done is to
allow the value entered to be given to a numeric variable, the
variable name being ‘number’. It could just as easily have been
called ‘a’ or ‘fred’. You can think of a variable as a box. When
you used the INPUT statement the number you entered was
placed in the box called ‘number’ and at a later date when you
asked for the contents of the box by typing PRINT number the
number you entered was shown. Use the example above again
but this time change the variable name, i.e. ‘number’ becomes
(fROP
ae
Program B2
1@ INPUT a
20 PRINT 6 * a
Run the program. Line 10 waits for the user to input a number
and that number is then multiplied by 6 in line 20, and the PRINT
statement prints that result to the screen.
Now what happens if we enter a non-numeric value, i.e. the
letter A? Although it would appear that our reply is accepted the
result we obtain will always be zero. This is because the machine
is expecting a numeric value and if anything else is typed in, the
computer reads it as a zero. This example leads me on to the two
different kinds of variable that exist.
95
The first we have encountered, that being a numeric variable.
The second kind of variable that we can use is called a string
variable. When using a string variable any keyboard character is
accepted and stored. The difference between this and a numeric
variable is that mathematics cannot be carried out on a string
variable. To distinguish between these two kinds, a rule is
followed.
We can call a numeric variable what we like. We called our
numeric variable in Program B2 ‘a’ but could quite as easily have
called it ‘number’ or ‘acorn’. To tell the computer we are using a
string variable we add a $ (dollar) sign on to the end of the
variable name. For instance, if we want to ask the user for his/her
name and then print a personal greeting to that person we can
write a program like this:
Program B3
1@ INPUT names
20 PRINT "Pleased to meet you "; name$
tage=9
or;
In the first example we have told the computer that the numeric
variable ‘tag’ equals 9 or to be more exact, we have said
LET tag = 9. The keyword LET is optional and we will not
make use of it but the important thing to remember is that when
we tell the computer that a variable is something or that, as in the
second example, the numeric variable y equals the value of y plus
8, then we are in fact saying, LET this variable equal...
96
So, for instance, if we want to print our name out ten times we
could type ten lines each with the same PRINT statement:
1@ PRINT "Jeremy"
20 PRINT "Jeremy" |
3@ PRINT "Jeremy" -
etc.
But that is a long way of going about it. So we can use our
ability to increment the value of a variable by typing the
following:
Program
B4
1 axe=—90
20 PRINT "Jeremy"
30 xX = x41
40 IF x < 1@ GOTO 20
5@ END
10 PROCinit
20 END
50° DEFPROCin7:t
OF
AGSPRINTOVHINTHERE?
5@ ENDPROC
Procedures are very useful and make programs easier to read and
follow.
98
Appendix C: BBC BASIC
Keyword Summary
09
ELSE Used in conjunction with IF-THEN to branch to
another action, i.e. IF counter > 20 THEN GOTO 50
ELSE 100.
100
LOCAL Tells the computer that a variable is local to a
function or procedure.
MIDS String manipulation command for taking a number
of given characters from a specified position in a
string.
NEW Clears the memory of the computer.
NEXT Specifies the end of a FOR-NEXT loop.
OLD Brings back the old program, if possible.
ON Enables redirection of a program by altering the
order of execution, i.e:
ON a GOTO 25, 45
ON ERROR RUN
101
TAB Used with INPUT or PRINT to move the screen cursor
to a specified position.
THEN Used in conjunction with the IF statement.
TO Used in conjunction with the FOR-NEXT loop to
specify a numeric range.
TRUE A logical value which is equal to -1.
UNTIL End of the REPEAT...UNTIL loop.
VAL Converts a number in a character string into a
numeric variable, i.e. “8” becomes 8.
102
Other titles of interest
Shiva publish a wide range of books for the BBC Micro, Electron,
ZX Spectrum, Dragon 32, Atari, VIC 20, Commodore 64, Oric and Atmos
computers, plus educational games programs for the BBC Micro. Please
complete the order form overpage to receive further details.
ORDER FORM
I should like to order the following Shiva titles:
EASY PROGRAMMING FOR THE BBC MICRO (2nd edn.) 1 85014 069 3 £5295
Si 6 0] oe @,(6: etce 8) D8) J0lse; oO) ©: 0, @, 0) 6 6 6 © 0) 6 oO(oO el 0 0 felle at Oe 6uelm 6 Crore. chs fo va w «: @ lalji@ sre
©) 6.‘ee! 56.0 0, (0! b!J84 0)'e; \#| ei wie) se.nese! ©) 8 tee; @ (6. is, (ei e; jedce: wef .e) a'se)\e) .o\) & le geuie!|®, veer ws wi 6) el a) © Kalle) ete eo aie
©: 61-6; 6.1m 5) ei; |8) 61 0) #19: (0 1618) 8 © (6 0 © (8! © 61 © elie © 8) 0 wo 6) &, e}/e) 6) 6 0 011% © 6) 6 6) eile «| soe) a vele @ a)rere
a.(euiel e!e) Te iebiel (oe ie) (e) ia \e; ice: \e) (6 ©o. (el eo) \o fete \9/ sa) (eo! © Neve: (of i) Onlelke @lieirelie Bile Slei'd) (6 allele (el Rr elas "66
©)[es6) .0; 10) po" sere 10) «e: ©: Ye) Yai elated eh® 6 @40 (0) 9's, Sie fe.nel (6 ley lee) oiled» 6.516 ob pei ef Sie tena: vlece: sis) eae Ut iilen es
Topic Books
7aTha
i i
,
Tt a
-
i}
a”.
K BC Micro
fforts, computer!
Now, with|
ate with
to comm e t echniques
you can to disco v e r t h
journey
will go on
o
needed t
panks
build data o
your micr
e educate ings
d i s e n t a n gle the str ool machine
* r
e s t a b l i s h 4 tamper-p
e
UK price £5.95
GB £ NET +005.°5
5-
r
SBN 1-85014-04
Limited | ||450||
140
* 781850
hing
Shiva Publis