Animal Intelligence
- Type
- Education
- Category
- Learning guessing game (decision tree)
- Author
- Randy Deardorff
- Published in
- Antic, April 1986 (Vol. 4, No. 12)
- Year
- 1986
- Language
- Atari BASIC
- Requirements
- Any Atari 8-bit computer, any memory size, Atari BASIC; disk or cassette to save what it learns
About the program
"Guess the animal" is one of the oldest artificial-intelligence demonstrations: a program that asks yes/no questions and learns from its mistakes. Randy Deardorff noted that such programs are easiest to write in list-processing languages like LISP or Logo. He couldn't find a BASIC version for the Atari, so he wrote Animal Intelligence, which ran in Antic's April 1986 mathematics issue. He also mentioned Joseph Weizenbaum's 1960s Eliza and argued that this program is more advanced, because it builds up a real store of knowledge.
The program starts out knowing almost nothing: two animals (a horse and a crow) and one question, "Is it a mammal?" (lines 900–920). The article admitted that early conversations "can be unintentionally hilarious". The longer you play, the smarter it gets.
Typing it in
A few strings contain special characters. {CTRL-,} (CTRL with the comma key, character 0) marks empty branches in the tree in lines 130, 320–330 and 890–920, and {CTRL-A}–{CTRL-C} in line 920 are the starting tree's pointers, so type them exactly. {inv} … {/inv} means press the Atari (inverse) key before and after, and {BACK S} is ESC then BACK S. Antic printed TYPO II checksums beside each line of the magazine listing. They are not part of the program.
How to play
Think of an animal and answer the questions. Anything starting with Y or N will do (so "No way." counts). When the program runs out of questions it makes a guess. If it's wrong, it asks what your animal was, then asks you for a question whose answer is YES for your animal and NO for its guess. Think "zebra" when it guesses "horse", and you might teach it "Does it have stripes?".
Save its knowledge. That is what makes the game fun. Press CTRL-S at any prompt (or answer yes when asked at the end) and give a device and filename (C: for cassette, D:NAME for disk). CTRL-L loads a saved knowledge base.
How it works
Deardorff described the structure in the article. Atari BASIC has no arrays of strings, so the knowledge is held in three long strings. AX$ is a pseudo-array of 15-character animal names, QX$ holds 37-character questions, and TREE$ holds the tree itself, three bytes per entry.
- A node is a question pointer plus a YES pointer and a NO pointer (bytes 1–3).
- A termination is an animal pointer plus two zero bytes (
{CTRL-,}).
Play starts at the root and follows the pointers (lines 120–160) until it reaches a termination, which is the guess. When a guess is wrong, lines 300–330 turn that termination into a new node. Your question goes in, your animal goes on the YES branch, and the old animal moves to the NO branch, so the tree grows one level taller at that point. Because each pointer is one byte (CHR$/ASC), the tree is limited to about 250 entries ("Buffer full" in line 290).
Listing
10 REM ANIMAL
20 REM BY RANDY DEARDORFF
30 REM (c) 1985, ANTIC PUBLISHING
40 BRK=1:IF PEEK(53279)=5 THEN BRK=0
50 DPL=PEEK(10592):POKE 10592,255
100 GOTO 860
110 ? :? "Think of an animal. I will try to":? "guess it by asking questions.":? :N=N1:TRAP 40000
120 NODE$=TREE$(N*NL-2,N*NL):Q=ASC(NODE$(N1,N1)):Y$=NODE$(2,2):N$=NODE$(3,3)
130 IF Y$="{CTRL-,}" THEN A=Q:G=A:GOTO 170
140 Q$=QX$(Q*QL-36,Q*QL):? Q$:GOSUB 520
150 IF R$="y" THEN N=ASC(Y$):GOTO 120
160 N=ASC(N$):GOTO 120
170 A$=AX$(A*AL-14,A*AL)
180 T=LEN(A$):IF T>N1 AND A$(T,T)=" " THEN A$=A$(N1,T-N1):GOTO 180
190 T$=A$:GOSUB 550:? "Is it";P$;A$;"?":GOSUB 520:IF R$="n" THEN 230
200 ? "That was fun!"
210 ? "Want to try again?":GOSUB 520:IF R$="y" THEN 110
212 ? :? "Shall I SAVE this data?":GOSUB 520:IF R$="y" THEN K=19:? :GOTO 390
220 POKE 82,2:GRAPHICS 0:POKE 10592,DPL:END
230 ? "I give up. Just what sort of beast":? "did you have in mind?"
240 GOSUB 350:IF LEN(R$)>AL OR R$="" THEN ? "1 to 15 letters please.":GOTO 240
250 H$=R$:T$=R$:GOSUB 550:? "Please type a question whose answer is YES for";P$;H$;" and"
260 T$=A$:GOSUB 550:? "NO for";P$;A$;"."
270 GOSUB 350:IF R$(LEN(R$))<>"?" THEN ? "That's not a question!":GOTO 270
280 Q$=R$:NA=NA+N1
290 IF NA>MAX OR NN>251 THEN ? "Buffer full, record ignored":NA=MAX:GOTO 110
300 LQ=Q:Q=NA:GOSUB 600:A$=H$:A=NA:GOSUB 580
310 NODE$=CHR$(A):NODE$(2,2)=CHR$(NN+N1):NODE$(3,3)=CHR$(NN+2):GOSUB 620
320 NN=NN+N1:NODE$=CHR$(A):NODE$(2,3)="{CTRL-,}{CTRL-,}":N=NN:GOSUB 620
330 NN=NN+N1:NODE$=CHR$(G):NODE$(2,3)="{CTRL-,}{CTRL-,}":N=NN:GOSUB 620:GOTO 210
340 REM USER INPUT
350 C=N0:X$=">"
360 R$="":L=N0:POSITION C,PEEK(84):? X$;
370 IF BRK THEN POKE 16,112:POKE 53774,112
375 GET #2,K:IF K=155 THEN ? :GOTO 480
380 IF K=126 THEN 450
390 IF K=19 THEN T=8:R$="{inv}SAVE{/inv}":GOTO 670
400 IF K=12 THEN T=4:R$="{inv}LOAD{/inv}":GOTO 670
410 IF K<32 OR K>122 THEN POKE 694,N0:GOTO 370
420 IF L=QL THEN 370
430 L=L+N1:? CHR$(K);:IF K>64 AND K<91 THEN K=K+32
440 R$(L,L)=CHR$(K):GOTO 370
450 IF R$="" THEN 370
460 ? "{BACK S}";:IF L=N1 THEN 360
470 L=L-N1:R$=R$(N1,L):GOTO 370
480 IF L=N0 THEN RETURN
490 IF R$(LEN(R$))="?" THEN K=ASC(R$(N1,N1)):IF K>96 THEN K=K-32:R$(N1,N1)=CHR$(K)
500 RETURN
510 REM YES/NO
520 GOSUB 350:IF R$>"" THEN R$=R$(N1,N1):IF R$="y" OR R$="n" THEN RETURN
530 ? "Come on, yes or no.":GOTO 520
540 REM A/AN
550 P$=" a ":X$=T$(N1,N1):IF (X$="a" OR X$="e" OR X$="i" OR X$="o" OR X$="u") THEN P$=" an "
560 RETURN
570 REM FILE ANIMAL
580 T$=A$:L=AL:GOSUB 640:AX$(A*AL-14,A*AL)=T$:RETURN
590 REM FILE QUESTION
600 T$=Q$:L=QL:GOSUB 640:QX$(Q*QL-36,Q*QL)=T$:RETURN
610 REM FILE KNOWLEDGE NODE
620 TREE$(N*NL-2,N*NL)=NODE$:RETURN
630 REM PAD WITH BLANKS
640 T=LEN(T$):IF T<L THEN T$(T+N1)=BL$(N1,L-T)
650 RETURN
660 REM LOAD/SAVE
670 POP
675 ? R$;" Device:filename";:C=21:X$=" ":GOSUB 360:TRAP 110
680 IF L>=2 AND R$(2,2)=":" THEN 710
690 IF L>=3 AND R$(3,3)=":" THEN 710
700 Q$=R$:R$="D:":R$(3)=Q$:L=L+2
710 FOR X=N1 TO L:B=ASC(R$(X,X)):IF B>96 THEN R$(X,X)=CHR$(B-32)
720 NEXT X:POKE 195,N0:TRAP 790:OPEN #N1,T,N0,R$
730 IF T=8 THEN QLEN=LEN(QX$):ALEN=LEN(AX$):? #N1,NA:? #N1,NN:? #N1,QLEN:? #N1,ALEN:GOTO 760
740 INPUT #N1,NA:INPUT #N1,NN:INPUT #N1,QLEN:INPUT #N1,ALEN
750 QX$=" ":QX$(QLEN-N1)=QX$:QX$(2)=QX$:AX$=QX$(N1,ALEN):TREE$=" ":TREE$(765)=TREE$:TREE$(2)=TREE$
760 ADDR=ADR(QX$):SIZE=QLEN:GOSUB 810
770 ADDR=ADR(AX$):SIZE=ALEN:GOSUB 810
780 ADDR=ADR(TREE$):SIZE=765:GOSUB 810
790 CLOSE #N1:X=PEEK(195):IF X=N0 THEN ? "Okay.":GOTO 110
800 ? "ERROR ";X:GOTO 110
810 IOCB=848:POKE IOCB+2,T+3
820 HI=INT(ADDR/256):LO=ADDR-HI*256:POKE IOCB+4,LO:POKE IOCB+5,HI
830 HI=INT(SIZE/256):LO=SIZE-HI*256:POKE IOCB+8,LO:POKE IOCB+9,HI
840 TJ=USR(ADR("hhh{inv}*{/inv}LV{inv}d{/inv}"),16):RETURN
850 REM INITIALIZE
860 N0=0:N1=1:GRAPHICS 0:POKE 710,12:POKE 712,12:POKE 709,0:POKE 82,N1:POKE 83,39
865 MAX=128:QL=37:AL=15:NL=3:CLOSE #2:OPEN #2,4,N0,"K:"
870 DIM QX$(QL*MAX),AX$(AL*MAX),TREE$(766),Q$(QL),A$(AL),NODE$(NL)
880 DIM H$(AL),R$(QL),T$(QL),BL$(QL),Y$(N1),N$(N1),P$(4),X$(N1)
890 TREE$="{CTRL-,}":TREE$(NL*255-N1)=TREE$:TREE$(2)=TREE$:BL$=" ":BL$(QL-N1)=BL$:BL$(2)=BL$
900 A$="horse":A=N1:GOSUB 580:A$="crow":A=2:GOSUB 580:NA=A:NN=3
910 Q$="Is it a mammal?":Q=N1:GOSUB 600
920 NODE$="{CTRL-A}{CTRL-B}{CTRL-C}":N=N1:GOSUB 620:NODE$="{CTRL-A}{CTRL-,}{CTRL-,}":N=2:GOSUB 620:NODE$="{CTRL-B}{CTRL-,}{CTRL-,}":N=3:GOSUB 620
930 GOTO 110
Download
The original program from the Antic disk, to LOAD or ENTER instead of typing it: ANIMAL.BAS.
Credits
- Program listing: Animal Intelligence — program listing (Antic, April 1986 (Vol. 4, No. 12)) — Randy Deardorff
- Picture: Every game starts with the same question, until the program learns more. — Randy Deardorff (program); screenshot by the site
- Antic, April 1986 - Vol. 4 No. 12: scan of the printed magazine, PDF pages 12-13 (article) — Internet Archive, 1986
- Antic disk, April 1986 - Vol. 4 No. 12: ANIMAL.BAS (detokenized listing) — Randy Deardorff, 1986
