A downloadable game

guess the fruit

instructions:

think of a fruit

the computer will try to guess it via a series of yes/no questions

if it doesn't guess it then you tell it what the fruit is and enter a question and it will remember it the next time

when prompted for a yes/no reply simply enter a y or n (in upper or lower case)

when prompted for a Y/N question you don't have to enter the ? at the end

The emulator that I used is an online one: JSBeeb - https://bbc.godbolt.org/

Simply copy and paste the source code into it and then type RUN

Technical information:

program structure (referring to the expanded version):

lines 10 to 16 - initialisation

lines 17 to 79 - main game loop

lines 80 to 84 - the y function

lines 90 to 100 - the questions and fruit data (the strings are encrypted in the 80 character version, otherwise you would be able to see all of the questions and fruit in the listing)

variables used:

q$(100) - array containing the questions and fruit

a(100,1) - array containing the yes/no links to the next question or fruit

together the q$ and a arrays make a binary tree where each node either has 2 children (the questions with yes/no links) or none (the fruit)

the binary tree can have up to a maximum of 101 nodes (50 questions and 51 fruits)

c - the number of nodes in the binary tree (total number of questions and fruit)

i - in initialisation section, loop counter to read the data into the q$ and a arrays

a$ - in initialisation section, the encrypted question/fruit string

h - in initialisation section, the length of the question/fruit string

j - in initialisation section, loop counter to decrypt the question/fruit string

i - in main program, the current node

h - in main program, the yes/no reply to the question (0 = no, 1 = yes)

j - in main program, the last question node visited

a$ - in main program, the reply to the "What is it?" question

b$ - in main program, the reply to the "Enter a Y/N question to differentiate between..." question

k - in main program, the reply to the "What is the reply for..." question (0 = no, 1 = yes)

y$ - in the y function, used to get a valid y/n reply


full listing with comments (for clarity the strings are stored as plain text in this version hence no need to decrypt them):
initialisation
string arrays are initialised with the null string, numeric arrays are initialised with 0
10DIMq$(100),a(100,1)
9 nodes (4 questions, 5 fruit) in the example binary tree
11c=9
read them in
12FORi=0TOc-1
13READq$(i),a(i,0),a(i,1)
14NEXT
15MODE7
set the starting node to the root node
16i=0
main game loop
17REPEAT
if the current node is a question then ask it, the next node depends on the yes/no reply
30IFa(i,0)THENPRINTq$(i);:h=FNy:j=i:i=a(i,h):UNTIL0
the current item is a fruit so guess it
31PRINT"is it "+q$(i);
if correct then start another game
40IFFNy THENPRINT"I got it!"':i=0:UNTIL0
if incorrect then prompt for the new fruit
41INPUT"I give up. What is it";a$
prompt for a Y/N question to allow differentiation between the guessed fruit and the new fruit
50PRINT"Enter a Y/N question to differentiate between "+q$(i)+" and "+a$
60INPUT""b$
prompt for the y/n reply for the new fruit
61PRINT"What is the reply for "+a$;
62k=FNy
create a new node for the question
63q$(c)=b$
set the yes/no node links
64a(c,k)=c+1
70a(c,1-k)=i
link the previous question to the new one
71a(j,h)=c
increment node count
72c=c+1
create a new node for the fruit
73q$(c)=a$
set the yes/no links to 0
74a(c,0)=0
75a(c,1)=0
increment node count
76c=c+1
77PRINT
start another game
78i=0
79UNTIL0
function y gets a valid y/n reply and returns 1 if reply is y and 0 if n
80DEFFNy
81REPEAT
82INPUTy$
83UNTILy$="Y"ORy$="y"ORy$="N"ORy$="n"
84=-(y$="Y"ORy$="y")
each node has 3 values, the question/fruit, the no node link and the yes node link
questions have 2 node links, fruit have none (i.e. the yes/no links are both 0)
the simplest tree (1 question and 2 items = 3 nodes and so c should be set to 3) is given by
DATAfirst question,1,2,item if reply is no,0,0,item if reply is yes,0,0
90DATAis it a citrus fruit,1,5,is it round,7,3,a lemon,0,0,does it have a stone
100DATA6,8,an orange,0,0,is it yellow,4,2,an apple,0,0,a banana,0,0,a peach,0,0

Download

Download
FRUIT.txt 755 bytes
Download
FRUIT80.txt 784 bytes
Download
Guess the fruit (instructions).txt 4 kB

Leave a comment

Log in with itch.io to leave a comment.