A downloadable game

guess the fruit


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):
string arrays are initialised with the null string, numeric arrays are initialised with 0
9 nodes (4 questions, 5 fruit) in the example binary tree
read them in
set the starting node to the root node
main game loop
if the current node is a question then ask it, the next node depends on the yes/no reply
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$
prompt for the y/n reply for the new fruit
61PRINT"What is the reply for "+a$;
create a new node for the question
set the yes/no node links
link the previous question to the new one
increment node count
create a new node for the fruit
set the yes/no links to 0
increment node count
start another game
function y gets a valid y/n reply and returns 1 if reply is y and 0 if n
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


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

Leave a comment

Log in with itch.io to leave a comment.