Hi,
I'm wanting to create a magic 8 ball game to use as a teaching project for kids at school whereby it asks a question to be typed and then will randomly select 1 of 20 possible answers to attach as an answer. I am conscious in python this can be done via elif statements and answers = random.randint, or an import random. How can a similar thing be achieved without overcomplicating things, taking into account this is for kids aged between 10-12 years old.
Many thanks
https://en.wikipedia.org/wiki/Magic_8_Ball
PRINT "WELCOME TO THE MAGIC 8 BALL"
WAIT 100
PRINT
PRINT "USING MY MAGICAL POWERS, I AM HERE TO HELP YOU ANSWER ANY QUESTIONS"
WAIT 200
PRINT
REM ACTIVITY 2 CODE BELOW
PRINT "TYPE IN YOUR QUESTION AND THEN PRESS THE ENTER KEY TO GET YOUR ANSWER"
PRINT
INPUT QUESTION$
PRINT
WAIT 100
PRINT "THIS IS YOUR QUESTION "
PRINT QUESTION$
PRINT
Magic 8 Ball game
Re: Magic 8 Ball game
Hi mrsallu,
BBC BASIC doesn't have ELIF, but you could certainly do it with nested IF ... THEN ... ELSE statements in the same way. Another, probably better, option would be to use a CASE statement:
BBC BASIC has the RND statement - no need to import an extra library! Note that lines starting REM are remarks (i.e. will not be executed).
Here's another simple implementation, using a slightly different approach (storing all the answers in an array and then choosing one based on the random number:
Hope that's helpful!
D
BBC BASIC doesn't have ELIF, but you could certainly do it with nested IF ... THEN ... ELSE statements in the same way. Another, probably better, option would be to use a CASE statement:
Code: Select all
REM Simple "8-Ball" implementation
REM Set up a game loop, asking a question, and choosing and displaying a response
REPEAT
INPUT "What is your yes/no question (Just press 'Enter' to quit)",q$
IF q$ <> "" THEN selected_answer% = RND(20) ELSE selected_answer% = 0
CASE selected_answer% OF
WHEN 1: PRINT "It is certain."
WHEN 2: PRINT "It is decidedly so."
WHEN 3: PRINT "Without a doubt."
WHEN 4: PRINT "Yes definitely."
WHEN 5: PRINT "You may rely on it."
WHEN 6: PRINT "As I see it, yes."
WHEN 7: PRINT "Most likely."
WHEN 8: PRINT "Outlook good."
WHEN 9: PRINT "Yes."
WHEN 10: PRINT "Signs point to yes."
WHEN 11: PRINT "Reply hazy, try again."
WHEN 12: PRINT "Ask again later."
WHEN 13: PRINT "Better not tell you now."
WHEN 14: PRINT "Cannot predict now."
WHEN 15: PRINT "Concentrate and ask again."
WHEN 16: PRINT "Don't count on it."
WHEN 17: PRINT "My reply is no."
WHEN 18: PRINT "My sources say no."
WHEN 19: PRINT "Outlook not so good."
WHEN 20: PRINT "Very doubtful."
OTHERWISE: PRINT "So long, and thanks for all the fish!"
ENDCASE
UNTIL selected_answer% = 0
END
Here's another simple implementation, using a slightly different approach (storing all the answers in an array and then choosing one based on the random number:
Code: Select all
REM Set up an empty array to hold 20 strings
DIM answers$(19)
REM Read in the possible answers from the data statements below
REM (Could allocate directly to the array, but that's messy for lots of strings)
REM Note that arrays are 0-based (though we COULD choose to ignore position 0 if that was easier)
FOR x% = 0 TO 19
READ answers$(x%)
NEXT x%
REM Set up a game loop, asking a question, and choosing and displaying a response
REPEAT
INPUT "What is your yes/no question (Just press 'Enter' to quit)",q$
REM Note RND used with a parameter n > 1 returns a random number in the range 1 to n...
selected_answer% = RND(20)
REM ... so when we select our answer from the array we need to subtract 1
IF q$ <> "" THEN PRINT "My answer is: " + answers$(selected_answer% - 1)
UNTIL q$=""
PRINT "So long, and thanks for all the fish!"
END
REM Here we store the answers (copied from Wikipedia) in DATA statements. Could have more than one per line,
REM but this is easy to read and amend! Strings could be quoted, but don't have to be (usually)
DATA It is certain.
DATA It is decidedly so.
DATA Without a doubt.
DATA Yes definitely.
DATA You may rely on it.
DATA As I see it, yes.
DATA Most likely.
DATA Outlook good.
DATA Yes.
DATA Signs point to yes.
DATA Reply hazy, try again.
DATA Ask again later.
DATA Better not tell you now.
DATA Cannot predict now.
DATA Concentrate and ask again.
DATA Don't count on it.
DATA My reply is no.
DATA My sources say no.
DATA Outlook not so good.
DATA Very doubtful.
D
Re: Magic 8 Ball game
However it does have ELSEIF, which is the same thing but with a different spelling (at least, it is so long as you include the trailing semicolon)! This is perfectly valid code in BBC BASIC for Windows, BBC BASIC for SDL 2.0 and the BBC BASIC Console Mode editions:
Code: Select all
IF condition1 THEN
REM Act on condition 1
ELSEIF condition2 THEN;
REM Act on condition 2
ELSEIF condition3 THEN;
REM Act on condition 3
ENDIF
An alternative way of doing the same thing is the CASE TRUE OF construction:
Code: Select all
CASE TRUE OF
WHEN condition1: REM Act on condition 1
WHEN condition2: REM Act on condition 2
WHEN condition3: REM Act on condition 3
ENDCASE
-
- Posts: 2
- Joined: Wed 06 Jul 2022, 13:53
Re: Magic 8 Ball game
Thanks all, I will have a play around with this and get back to you in due course if I have any further questions.
Cheers!
Cheers!