home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Transactor
/
Transactor_13_1986_Transactor_Publishing.d64
/
animals
(
.txt
)
< prev
next >
Wrap
Commodore BASIC
|
2023-02-26
|
5KB
|
179 lines
100 rem "animals" ai program
110 rem a simple expert system
120 rem run 51000 to create file
130 rem run 51100 to initialize file
140 rem run 50000 to print file
150 rem save"@0:animals",8
160 :
170 z$=chr$(0)
180 sp$=" "
190 rl=44: rem rel record size-1
200 input"[206]ame of data file animals.dat[157][157][157][157][157][157][157][157][157][157][157][157][157]";f$
210 :
220 rem* main program loop *
230 open 15,8,15
240 open 1,8,9,f$
250 print#15,"p";chr$(9)chr$(1)chr$(0)chr$(1)
260 rem first record holds next available record
270 get#1,m1$,m2$: rem in low, hi format
280 m1$=left$(m1$+z$,1): m2$=left$(m2$+z$,1)
290 max=asc(m1$) + 256*asc(m2$)
300 :
310 print"[147]** [212]hink of an animal **";
320 print"** [193]nswer questions with 'y' or 'n' **"
330 rp=2: rem point to first question
340 :
350 r=rp: gosub 20000'read in (NULL)
360 if yes=0 and no=0 then 460'end of chain
370 rem chain to next branch
380 print m$;"? ";
390 gosub 10000: rem get y/n response
400 bp=rp: remember old record #
410 if yn$="y" then a$="yes": rp=yes: ob=0
420 if yn$="n" then a$="no" : rp=no : ob=1
430 print a$: rem yes or no
440 goto 350
450 :
460 rem end of chain - give guess
470 print"*** [201]t might be a ";m$
480 print"> [201]s that correct (y/n) ";
490 gosub 10000'get answer yes or no
500 if yn$="y" then 950'found answer, wrap up
510 rem got wrong answer, let's learn from it
520 print" no": print"[207][203], what were you actually thinking of"
530 input" >";animal$
540 print"[215]hat yes/no question could [201] ask"
550 print"to distinguish a"
560 print" ";m$
570 print"from a"
580 print" ";animal$
590 input q$
600 print"[193]nd regarding a"
610 print" ";m$;",": print q$
620 print"> (y/n)";
630 gosub 10000'get yes/no
640 rem create new question pointing to current or new animal
650 if yn$="y" then a$="yes": yn=rp: nn=max+1: rem new yes/no pointers
660 if yn$="n" then a$="no" : yn=max+1: nn=rp: rem new yes/no pointers
670 print a$: rem yes or no
680 rr=max: gosub 40000'(NULL)#(max)
690 n=yn: gosub 30000: yn$=lh$: rem convert n to lh$ (low+hi)
700 n=nn: gosub 30000: nn$=lh$: rem convert n to lh$ (low+hi)
710 print#1,yn$;nn$;left$(q$+sp$,rl-4)
720 gosub 40000're-position to foil bug
730 rem point old question to new
740 r=bp : gosub 20000'read in old question
750 n=max: gosub 30000'find low,hi of new (NULL) position
760 if ob=0 then yes$=lh$: rem point 'yes' ptr to new question
770 if ob=1 then no$ =lh$: rem point 'no' ptr to new question
780 rr=bp: gosub 40000'(NULL)#1,(bp)
790 print#1,yes$;no$;m$: rem re-write modified record
800 gosub 40000're-position to foil bug
810 rem now put new animal in next available record
820 rr=max+1: gosub 40000'(NULL)#1,(max+1)
830 print#1,z$;z$;z$;z$;left$(animal$+sp$,rl-4)
840 gosub 40000're-position to foil bug
850 rem now update max. record pointer
860 rr=1: gosub 40000'1st rec has ptr
870 max=max+2 : rem 2 records have been added to file
880 n=max: gosub 30000'convert to 2-byte pointer
890 print#1,lh$;left$(sp$,rl-2): rem pad with spaces
900 gosub 40000're-position to foil bug
910 print: print"[212]hank you for teaching me a new animal!"
920 goto 970
930 :
940 rem got right answer, wrap up
950 print" yes": print"[193]lright! [199]uess [201]'m pretty smart."
960 :
970 close 1: close 15
980 input"play again y[157][157][157]";yn$
990 if yn$="y" then 230
1000 end
1010 :
1020 :
10000 rem* subroutine to accept y or n
10010 k=0: for i=0 to 1
10020 get yn$
10030 rem flash fake cursor
10040 print mid$("[146]",sgn(k and 8)+1,1);" [157]";
10050 k=(k+1) and 255
10060 i=-(yn$="y"or yn$="n"): next
10070 rem until 'y' or 'n' pressed
10080 print"[146] ";: rem erase cursor
10090 return
10100 :
10110 :
20000 rem subroutine to read record# (r) in yes$,no$,m$
20010 rr=r: gosub 40000: rem record#1,(r)
20020 get#1,y1$,y2$,n1$,n2$
20030 yes$=left$(y1$+z$,1) + left$(y2$+z$,1)
20040 no$ =left$(n1$+z$,1) + left$(n2$+z$,1)
20050 yes=asc(y1$+z$) + 256*asc(y2$+z$)
20060 no =asc(n1$+z$) + 256*asc(n2$+z$)
20070 input#1,m$
20080 rem strip trailing spaces
20090 lc=0: for k=1tolen(m$):if mid$(m$,k,1)<>" "then lc=k
20100 next: if lc then m$=left$(m$,lc)
20110 return
20120 :
20130 :
30000 rem* subroutine to convert 16-bit 'n' to low,hi 'lh$' *
30010 hh%=n/256: ll%=n-256*hh%: lh$=chr$(ll%)+chr$(hh%)
30020 return
30030 :
30040 :
40000 rem* subroutine to simulate 'record#1,(rr)' using basic 2.0 *
40010 rh%=rr/256: rl%=rr-256*rh%
40020 print#15,"p";chr$(96+9)chr$(rl%)chr$(rh%)chr$(1)
40030 return
40040 :
40050 :
50000 rem** dump relative file
50010 open 15,8,15
50020 input"[206]ame of data file animals.dat[157][157][157][157][157][157][157][157][157][157][157][157][157]";f$
50030 open 1,8,9,f$
50040 z$=chr$(0)
50050 print#15,"p";chr$(9)chr$(1)chr$(0)chr$(1): get#1,l$,h$
50060 nr=asc(l$+z$)+256*asc(h$+z$)-1
50070 print " 1 :"nr"records in file"
50080 rl=2: rh=0: rem record #, lo/hi
50090 for i=2 to nr
50100 print#15,"p";chr$(9)chr$(rl)chr$(rh)chr$(1)
50110 get#1,x1$,x2$,x3$,x4$: input#1,a$
50120 p1=asc(x1$+z$)+256*asc(x2$+z$)
50130 p2=asc(x3$+z$)+256*asc(x4$+z$)
50140 print i;":";p1,p2,"["a$"]"
50150 rl=rl+1: if rl>255 then rl=0: rh=rh+1
50160 next: close 15
50170 end
50180 :
50190 :
51000 rem** create new animal file
51010 input"[206]ame of data file animals.dat[157][157][157][157][157][157][157][157][157][157][157][157][157]";f$
51020 input"maximum number of records 2000[157][157][157][157][157][157]";m
51030 open 15,8,15
51040 open 1,8,9,f$+",l,"+chr$(45): rem rec len = 45
51050 rr=m: gosub 40000
51060 print#1,left$(sp$,44)
51070 close 1: close 15: goto 51110
51080 input"[206]ame of data file animals.dat[157][157][157][157][157][157][157][157][157][157][157][157][157]";f$
51090 :
51100 rem** teach first two animals
51110 r1$="[196]oes it live in the water":rem first question
51120 r2$="[198]ish" : rem 'yes' answer
51130 r3$="[200]orse": rem 'no' answer
51140 z$=chr$(0)
51150 sp$=" "
51160 open 15,8,15
51170 open 1,8,9,f$
51180 rr=1: gosub 40000
51190 print#1,chr$(5);z$;left$(sp$,40)
51200 gosub 40000: rr=rr+1: gosub 40000
51210 print#1,chr$(3);z$;chr$(4);z$;left$(r1$+sp$,40)
51220 gosub 40000: rr=rr+1: gosub 40000
51230 print#1,z$z$z$z$;left$(r2$+sp$,40)
51240 gosub 40000: rr=rr+1: gosub 40000
51250 print#1,z$z$z$z$;left$(r3$+sp$,40)
51260 close 15
51270 end