Magic 8-ball: Difference between revisions

no edit summary
m (→‎version 2: added "possible")
No edit summary
 
(43 intermediate revisions by 26 users not shown)
Line 10:
{{Template:strings}}
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">V s = [‘It is certain’, ‘It is decidedly so’, ‘Without a doubt’, ‘Yes, definitely’,
‘You may rely on it’, ‘As I see it, yes’, ‘Most likely’, ‘Outlook good’,
‘Signs point to yes’, ‘Yes’, ‘Reply hazy, try again’, ‘Ask again later’,
‘Better not tell you now’, ‘Cannot predict now’, ‘Concentrate and ask again’,
‘Don't bet on it’, ‘My reply is no’, ‘My sources say no’, ‘Outlook not so good’,
‘Very doubtful’]
 
Set[String] qs
 
L
V question = input(‘Ask your question:’)
I question.empty
L.break
 
V answer = random:choice(s)
 
I question C qs
print(‘Your question has already been answered’)
E
qs.add(question)
print(answer)</syntaxhighlight>
 
=={{header|8080 Assembly}}==
 
 
<langsyntaxhighlight lang="8080asm">bdos: equ 5 ; CP/M calls
puts: equ 9
gets: equ 10
Line 97 ⟶ 122:
xabcdat: equ buf + 60 ; Point RNG data into uninitialized memory,
; to make it more exciting.
</syntaxhighlight>
</lang>
 
=={{header|8086 Assembly}}==
This procedure when run displays a random Magic 8-Ball phrase (the list was shortened to 16 different phrases to simplify the random number generation) and outputs it to the console, then exits back to MS-DOS. A 32 bit [[wp:Xorshift|Xorshift]] routine was used to create the random numbers, and it was seeded using the computer's current time.
<syntaxhighlight lang="asm">
.model small
.stack 1024
 
.data
UserRam BYTE 256 DUP(0)
xorshift32_state_lo equ UserRam
xorshift32_state_hi equ UserRam+2
 
Ans0 byte "It is certain.",0
Ans1 byte "It is decidedly so.",0
Ans2 byte "Signs point to yes.",0
Ans3 byte "You can rely on it.",0
Ans4 byte "Most likely.",0
Ans5 byte "Cannot predict now.",0
Ans6 byte "Reply hazy, try again.",0
Ans7 byte "Outlook not so good.",0
Ans8 byte "My sources say no.",0
Ans9 byte "Very doubtful.",0
AnsA byte "Without a doubt.",0
AnsB byte "Outlook good.",0
AnsC byte "Ask again later.",0
AnsD byte "Better not tell you now.",0
AnsE byte "Don't count on it.",0
AnsF byte "Yes.",0
 
AnswerLookup word Ans0,Ans1,Ans2,Ans3,Ans4,Ans5,Ans6,Ans7
word Ans8,Ans9,AnsA,AnsB,AnsC,AnsD,AnsE,AnsF
.code
 
start:
 
mov ax,@data
mov ds,ax
mov ax,@code
mov es,ax
 
CLD ;have LODSB,MOVSB,STOSB,etc. auto-increment
mov ax,03h
int 10h ;sets video mode to MS-DOS text mode. Which the program is already in, so this just clears the screen.
mov ah,2Ch
int 21h ;returns hour:minute in cx, second:100ths of second in dx
mov word ptr [ds:xorshift32_state_lo],dx
mov word ptr [ds:xorshift32_state_hi],cx
call doXorshift32 ;uses the above memory locations as input. Do it three times just to mix it up some more
call doXorshift32
call doXorshift32
mov ax,word ptr[ds:xorshift32_state_lo] ;get the random output from the RNG
and al,0Fh
;keep only the bottom nibble of al, there are only 16 possible messages.
mov bx,offset AnswerLookup
call XLATW
;translates AX using a table of words as a lookup table.
mov si,ax ;use this looked-up value as the source index for our text.
call PrintString ;print to the screen
mov ax,4C00h
int 21h ;return to DOS
 
XLATW:
;input: ds:bx = the table you wish to look up
;AL= the raw index into this table as if it were byte data.
;So don't left shift prior to calling this.
;AH is destroyed.
SHL AL,1
mov ah,al ;copy AL to AH
XLAT ;returns low byte in al
inc ah
xchg al,ah ;XLAT only works with AL
XLAT ;returns high byte in al (old ah)
xchg al,ah
;now the word is loaded into ax, big-endian.
ret</syntaxhighlight>
 
{{out}}
 
This is the output after three separate runs of the program.
<pre>
Cannot predict now.
Signs point to yes.
Better not tell you now.
</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC Main()
DEFINE PTR="CARD"
DEFINE COUNT="20"
CHAR ARRAY s(256)
PTR ARRAY a(COUNT)
BYTE i
 
a(0)="It is certain."
a(1)="It is decidedly so."
a(2)="Without a doubt."
a(3)="Yes - definitely."
a(4)="You may rely on it."
a(5)="As I see it, yes."
a(6)="Most likely."
a(7)="Outlook good."
a(8)="Yes."
a(9)="Signs point to yes."
a(10)="Reply hazy, try again."
a(11)="Ask again later."
a(12)="Better not tell you now."
a(13)="Cannot predict now."
a(14)="Concentrate and ask again."
a(15)="Don't count on it."
a(16)="My reply is no."
a(17)="My sources say no."
a(18)="Outlook not so good."
a(19)="Very doubtful."
 
DO
PrintE("Enter your question or blank to exit:")
InputS(s)
IF s(0)=0 THEN
EXIT
FI
i=Rand(COUNT)
PrintE(a(i))
PutE()
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Magic_8-ball.png Screenshot from Atari 8-bit computer]
<pre>
Enter your question or blank to exit:
Do you know me?
Signs point to yes.
 
Enter your question or blank to exit:
Is Action! your favorite language?
Yes.
 
Enter your question or blank to exit:
Is Atari the best 8-bit computer?
Very doubtful.
 
Enter your question or blank to exit:
Are you kidding me?
Concentrate and ask again.
</pre>
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">
<lang Ada>
 
with Ada.Text_IO; use Ada.Text_IO;
Line 156 ⟶ 332:
end Main;
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 172 ⟶ 348:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN
[]STRING answers = ("It is certain.", "It is decidedly so.",
"Without a doubt.", "Yes - definitely.", "You may rely on it.",
Line 186 ⟶ 362:
print((answers[ENTIER (random * UPB answers) + 1], new line))
OD
END</langsyntaxhighlight>
 
=={{header|AppleScript}}==
<langsyntaxhighlight AppleScriptlang="applescript"> set the answers to {"It is certain", "It is decidedly so", "Without a doubt", "Yes, definitely", "You may rely on it", "As I see it, yes", "Most likely", "Outlook good", "Signs point to yes", "Yes", "Reply hazy, try again", "Ask again later", "Better not tell you now", "Cannot predict now", "Concentrate and ask again", "Don't bet on it", "My reply is no", "My sources say no", "Outlook not so good", "Very doubtful"}
 
display dialog some item of the answers</langsyntaxhighlight>
=={{header|Applesoft BASIC}}==
{{trans|Commodore BASIC}}
<syntaxhighlight lang="gwbasic"> 0 DIM F$(19): FOR I = 0 TO 19: READ F$(I): NEXT :M$ = CHR$ (13): DEF FN U(K) = K - 32 * (K > 95): FOR Q = 0 TO 1: HOME : PRINT M$" PRESS ANY KEY TO REVEAL YOUR FORTUNE."M$M$" PRESS Q TO QUIT."M$M$" ";:FO = INT ( RND (1) * 20): GET K$
1 K$ = CHR$ ( FN U( ASC (K$))):Q = K$ = "Q": IF NOT Q THEN HOME : PRINT M$" YOUR FORTUNE READS:"M$M$ SPC( 5);F$(FO)M$M$" AGAIN? (Y/N) ";: GET K$:K$ = CHR$ ( FN U( ASC (K$))):Q = K$ < > "Y"
2 NEXT Q: DATA "IT IS CERTAIN.","IT IS DECIDEDLY SO.","WITHOUT A DOUBT.","YES DEFINITELY.","YOU MAY RELY ON IT.","AS I SEE IT, YES.","MOST LIKELY.","OUTLOOK GOOD.","YES.","SIGNS POINT TO YES."
3 DATA "REPLY HAZY, TRY AGAIN.","ASK AGAIN LATER.","BETTER NOT TELL YOU NOW.","CANNOT PREDICT NOW.","CONCENTRATE AND ASK AGAIN.","DON'T COUNT ON IT.","MY REPLY IS NO.","MY SOURCES SAY NO.","OUTLOOK NOT SO GOOD.","VERY DOUBTFUL."
</syntaxhighlight>
=={{header|Arturo}}==
<langsyntaxhighlight lang="rebol">answers: [
"It is certain" "It is decidedly so" "Without a doubt"
"Yes, definitely" "You may rely on it" "As I see it, yes"
Line 208 ⟶ 391:
input "Ask a question: "
print sample answers
]</langsyntaxhighlight>
 
{{out}}
Line 218 ⟶ 401:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">answers := [
(join
"It is certain", "It is decidedly so", "Without a doubt","Yes, definitely",
Line 234 ⟶ 417:
MsgBox % answers[rnd]
gosub, Ask
return</langsyntaxhighlight>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f MAGIC_8-BALL.AWK
BEGIN {
Line 273 ⟶ 456:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 283 ⟶ 466:
?
</pre>
 
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|QBasic}}
<syntaxhighlight lang="basic256">dim answer$(20)
answer$[0] = "It is certain."
answer$[1] = "It is decidedly so."
answer$[2] = "Without a doubt."
answer$[3] = "Yes - definitely."
answer$[4] = "You may rely on it."
answer$[5] = "As I see it, yes."
answer$[6] = "Most likely."
answer$[7] = "Outlook good."
answer$[8] = "Yes."
answer$[9] = "Signs point to yes."
answer$[10] = "Reply hazy, try again."
answer$[11] = "Ask again later."
answer$[12] = "Better not tell you now."
answer$[13] = "Cannot predict now."
answer$[14] = "Concentrate and ask again."
answer$[15] = "Don't count on it."
answer$[16] = "My reply is no."
answer$[17] = "My sources say no."
answer$[18] = "Outlook not so good."
answer$[19] = "Very doubtful."
 
print "Q to quit."
while True
input string "What would you like to know? ", question$
if upper(question$) = "Q" then exit while
print answer$[int(rand * answer$[?])]
print
end while
end</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{trans|Commodore BASIC}}
<syntaxhighlight lang="qbasic">100 cls
110 data "It is certain.","It is decidedly so."
120 data "Without a doubt.","Yes - definitely."
130 data "You may rely on it.","As I see it, yes."
140 data "Most likely.","Outlook good."
150 data "Yes.","Signs point to yes."
160 data "Reply hazy, try again.","Ask again later."
170 data "Better not tell you now.","Cannot predict now."
180 data "Concentrate and ask again.","Don't count on it."
190 data "My reply is no.","My sources say no."
200 data "Outlook not so good.","Very doubtful."
210 dim m8ball$(20)
220 for i = 0 to 19
230 read m8ball$(i)
240 next i
250 randomize timer
260 input "What would you like to know? ",q$
270 print m8ball$(int(rnd(20)))
280 end</syntaxhighlight>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 PROGRAM "Magic8.bas"
110 RANDOMIZE
120 STRING ANSWER$(1 TO 20)*26
130 FOR I=1 TO 20
140 READ ANSWER$(I)
150 NEXT
160 CLEAR SCREEN
170 PRINT "Magic 8-ball":PRINT "Q to quit.":PRINT
180 DO
190 INPUT PROMPT "What would you like to know? ":QUESTION$
200 IF LCASE$(QUESTION$)="q" THEN EXIT DO
210 PRINT ANSWER$(RND(20)+1):PRINT
220 LOOP
230 DATA It is certain.,It is decidedly so.,Without a doubt.
240 DATA Yes - definitely.,You may rely on it.,"As I see it, yes."
250 DATA Most likely.,Outlook good.,Yes.
260 DATA Signs point to yes.,"Reply hazy, try again.",Ask again later.
270 DATA Better not tell you now.,Cannot predict now.,Concentrate and ask again.
280 DATA Don't count on it.,My reply is no.,My sources say no.
290 DATA Outlook not so good.,Very doubtful.</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
{{trans|Commodore BASIC}}
<syntaxhighlight lang="qbasic">100 CLS
110 DATA "It is certain.","It is decidedly so."
120 DATA "Without a doubt.","Yes - definitely."
130 DATA "You may rely on it.","As I see it, yes."
140 DATA "Most likely.","Outlook good."
150 DATA "Yes.","Signs point to yes."
160 DATA "Reply hazy, try again.","Ask again later."
170 DATA "Better not tell you now.","Cannot predict now."
180 DATA "Concentrate and ask again.","Don't count on it."
190 DATA "My reply is no.","My sources say no."
200 DATA "Outlook not so good.","Very doubtful."
210 DIM m$(20)
220 FOR i = 0 TO 19
230 READ m$(i)
240 NEXT i
260 INPUT "What would you like to know? "; q$
270 PRINT m$(INT(RND(20)))
280 END</syntaxhighlight>
 
==={{header|QBasic}}===
{{works with|QBasic}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">DIM answer$(19)
FOR i = 0 TO UBOUND(answer$): READ answer$(i): NEXT i
RANDOMIZE TIMER
 
PRINT "Q to quit."
DO
INPUT "What would you like to know? ", question$
IF UCASE$(question$) = "Q" THEN EXIT DO
PRINT answer$(INT(RND * UBOUND(answer$)))
PRINT
LOOP
END
 
DATA "It is certain.","It is decidedly so."
DATA "Without a doubt.","Yes – definitely."
DATA "You may rely on it.","As I see it, yes."
DATA "Most likely.","Outlook good.","Yes."
DATA "Signs point to yes.","Reply hazy, try again."
DATA "Ask again later.","Better not tell you now."
DATA "Cannot predict now.","Concentrate and ask again."
DATA "Don't count on it.","My reply is no."
DATA "My sources say no.","Outlook not so good."
DATA "Very doubtful."</syntaxhighlight>
 
==={{header|Quite BASIC}}===
The [[#MSX Basic|MSX Basic]] solution works without any changes.
 
==={{header|True BASIC}}===
{{trans|QBasic}}
<syntaxhighlight lang="qbasic">DIM answer$(20)
FOR i = 1 to ubound(answer$)
READ answer$(i)
NEXT i
DATA "It is certain.", "It is decidedly so."
DATA "Without a doubt.", "Yes – definitely."
DATA "You may rely on it.", "As I see it, yes."
DATA "Most likely.", "Outlook good.", "Yes."
DATA "Signs point to yes.", "Reply hazy, try again."
DATA "Ask again later.", "Better not tell you now."
DATA "Cannot predict now.", "Concentrate and ask again."
DATA "Don't count on it.", "My reply is no."
DATA "My sources say no.", "Outlook not so good."
DATA "Very doubtful."
 
RANDOMIZE
PRINT "Q to quit."
DO
INPUT prompt "What would you like to know? ": question$
IF ucase$(question$) = "Q" then EXIT DO
PRINT answer$(int(rnd*ubound(answer$)))
PRINT
LOOP
END</syntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|QBasic}}
<syntaxhighlight lang="yabasic">dim answer$(19)
for i = 0 to arraysize(answer$(),1): read answer$(i): next i
 
print "Q to quit."
do
input "What would you like to know? " question$
if upper$(question$) = "Q" then end : fi
print answer$(int(ran(arraysize(answer$(),1))))
print
loop
 
data "It is certain.","It is decidedly so."
data "Without a doubt.","Yes – definitely."
data "You may rely on it.","As I see it, yes."
data "Most likely.","Outlook good.","Yes."
data "Signs point to yes.","Reply hazy, try again."
data "Ask again later.","Better not tell you now."
data "Cannot predict now.","Concentrate and ask again."
data "Don//t count on it.","My reply is no."
data "My sources say no.","Outlook not so good."
data "Very doubtful."</syntaxhighlight>
 
 
=={{header|C}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Line 314 ⟶ 681:
if (question) free(question);
return 0;
}</langsyntaxhighlight>
 
{{output}}
Line 337 ⟶ 704:
 
=={{header|C++}}==
<langsyntaxhighlight Cpplang="cpp">#include <array>
#include <cstdlib>
#include <ctime>
Line 380 ⟶ 747:
std::cout << answers[std::rand() % answers.size()] << '\n';
}
}</langsyntaxhighlight>
 
=={{header|C#}}==
<langsyntaxhighlight lang="csharp">
using System;
 
Line 434 ⟶ 801:
 
 
</syntaxhighlight>
</lang>
 
=={{header|CFEngine}}==
 
<syntaxhighlight lang="cfengine3">
#!/var/cfengine/bin/cf-agent --no-lock
bundle agent __main__
{
methods: "rosettacode_org:magic_8_ball";
}
body file control
{
namespace => "rosettacode_org";
}
bundle agent magic_8_ball
{
vars:
"_responses"
slist => {
"It is certain", "It is decidedly so", "Without a doubt",
"Yes definitely", "You may rely on it", "As I see it yes",
"Most likely", "Outlook good", "Signs point to yes", "Yes",
"Reply hazy try again", "Ask again later",
"Better not tell you now", "Cannot predict now",
"Concentrate and ask again", "Don't bet on it",
"My reply is no", "My sources say no", "Outlook not so good",
"Very doubtful",
};
"_selected_response"
int => randomint( 0, length( _responses) ),
unless => isvariable( "$(this.namespace):$(this.promiser)" );
 
"_consideration_time"
int => randomint( 3, 5),
unless => isvariable( "$(this.namespace):$(this.promiser)" );
 
commands:
"/bin/sleep $(_consideration_time)"
inform => "false",
handle => "consider",
depends_on => { "think" },
comment => "This magic 8 ball takes a few seconds to consider the answer
after you bring your question to mind.";
 
reports:
"Think about your question ..."
handle => "think";
 
"Response: $(with)"
with => nth( _responses, $(_selected_response) ),
depends_on => { "consider" };
}
</syntaxhighlight>
 
See https://docs.cfengine.com/docs/master/examples.html for a more complete example and introduction.
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">
(def responses
["It is certain" "It is decidedly so" "Without a doubt"
Line 452 ⟶ 873:
(read-line)
(println (rand-nth responses)))
</syntaxhighlight>
</lang>
 
=={{header|CMake}}==
<langsyntaxhighlight CMakelang="cmake">CMAKE_MINIMUM_REQUIRED(VERSION 3.6)
 
PROJECT(EightBall)
Line 493 ⟶ 914:
 
ADD_CUSTOM_TARGET(${PROJECT_NAME} ALL)
</syntaxhighlight>
</lang>
No question specified:
<pre>$ cmake -H. -Bbuild
Line 522 ⟶ 943:
-- Configuring done
-- Generating done</pre>
 
=={{header|COBOL}}==
<syntaxhighlight lang="COBOL">
IDENTIFICATION DIVISION.
PROGRAM-ID. 8BALL.
AUTHOR. Bill Gunshannon
INSTALLATION.
DATE-WRITTEN. 12 March 2024
************************************************************
** Program Abstract:
** Just ask the Magic 8 Ball and all will be revealed.
************************************************************
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
 
01 ANSWER-TABLE.
10 ANSWER01 PIC X(40)
VALUE "As I see it, yes ".
10 ANSWER02 PIC X(40)
VALUE "Ask again later ".
10 ANSWER03 PIC X(40)
VALUE "Better not tell you now ".
10 ANSWER04 PIC X(40)
VALUE "Cannot predict now ".
10 ANSWER05 PIC X(40)
VALUE "Concentrate and ask again ".
10 ANSWER06 PIC X(40)
VALUE "Don't bet on it ".
10 ANSWER07 PIC X(40)
VALUE "It is certain ".
10 ANSWER08 PIC X(40)
VALUE "It is decidedly so ".
10 ANSWER09 PIC X(40)
VALUE "Most likely ".
10 ANSWER10 PIC X(40)
VALUE "My reply is no ".
10 ANSWER11 PIC X(40)
VALUE "My sources say maybe ".
10 ANSWER12 PIC X(40)
VALUE "My sources say no ".
10 ANSWER13 PIC X(40)
VALUE "Outlook good ".
10 ANSWER14 PIC X(40)
VALUE "Outlook not so good ".
10 ANSWER15 PIC X(40)
VALUE "Reply hazy, try again ".
10 ANSWER16 PIC X(40)
VALUE "Signs point to yes ".
10 ANSWER17 PIC X(40)
VALUE "Very doubtful ".
10 ANSWER18 PIC X(40)
VALUE "Without a doubt ".
10 ANSWER19 PIC X(40)
VALUE "Yes ".
10 ANSWER20 PIC X(40)
VALUE "Yes, definitely ".
10 ANSWER21 PIC X(40)
VALUE "Yes, probably not ".
10 ANSWER22 PIC X(40)
VALUE "You may rely on it ".
10 ANSWER23 PIC X(40)
VALUE "Your question has already been answered ".
01 PRINT-ANSWER REDEFINES ANSWER-TABLE OCCURS 23 TIMES.
10 THE-BALL-SPEAKS PIC X(40).
 
01 RND PIC 99999.
01 QUESTION PIC X(72).
01 GREETING PIC X(30)
VALUE "Ask and all will be revealed!!".
PROCEDURE DIVISION.
Main-Program.
DISPLAY GREETING.
ACCEPT QUESTION.
MOVE FUNCTION CURRENT-DATE(1:16) TO RND.
PERFORM 8-BALL.
 
STOP RUN.
 
8-BALL.
COMPUTE RND =
FUNCTION RANDOM(RND) * 11111.
DISPLAY PRINT-ANSWER(FUNCTION MOD(RND, 23)).
END-PROGRAM.
 
</syntaxhighlight>
 
=={{header|Commodore BASIC}}==
<syntaxhighlight lang="commodorebasicv2">
<lang CommodoreBASICv2>
10 dim f$(19):for i=0 to 19:read f$(i):next
20 print chr$(147);chr$(14)
Line 546 ⟶ 1,062:
1080 data "My sources say no.","Outlook not so good."
1090 data "Very doubtful."
</syntaxhighlight>
</lang>
 
=={{header|Craft Basic}}==
<syntaxhighlight lang="basic">title "Magic 8 Ball"
 
resize 0, 0, 340, 150
center
 
bgcolor 0, 0, 255
cls graphics
 
formid 1
formtext "Think of a question and select the magic button."
staticform 10, 10, 310, 20
bgcolor 0, 0, 0
fgcolor 255, 0, 255
colorform
 
formid 2
formtext "Magic Button"
buttonform 115, 50, 100, 20
bgcolor 255, 0, 0
fgcolor 0, 0, 255
colorform
 
do
 
 
if forms = 2 then
 
gosub magicbutton
 
endif
 
button k, 27
 
wait
 
loop k = 0
 
end
 
sub magicbutton
 
let r = int(rnd * 20) + 1
 
if r = 1 then
 
alert "It is certain."
 
endif
 
if r = 2 then
 
alert "It is decidedly so."
 
endif
 
if r = 3 then
 
alert "Without a doubt."
 
endif
 
if r = 4 then
 
alert "Yes – definitely."
 
endif
 
if r = 5 then
 
alert "You may rely on it."
 
endif
 
if r = 6 then
 
alert "As I see it", comma, " yes."
 
endif
 
if r = 7 then
 
alert "Most likely."
 
endif
 
if r = 8 then
 
alert "Outlook good."
 
endif
 
if r = 9 then
 
alert "Yes."
 
endif
 
if r = 10 then
 
alert "Signs point to yes."
 
endif
 
if r = 11 then
 
alert "Reply hazy", comma, " try again."
 
endif
 
if r = 12 then
 
alert "Ask again later."
 
endif
 
if r = 13 then
 
alert "Better not tell you now."
 
endif
 
if r = 14 then
 
alert "Cannot predict now."
 
endif
 
if r = 15 then
 
alert "Concentrate and ask again."
 
endif
 
if r = 16 then
 
alert "Don't count on it."
 
endif
 
if r = 17 then
 
alert "My reply is no."
 
endif
 
if r = 18 then
 
alert "My sources say no."
 
endif
 
if r = 19 then
 
alert "Outlook not so good."
 
endif
 
if r = 20 then
 
alert "Very doubtful."
 
endif
 
return</syntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight Dlang="d">import std.random, std.stdio, std.string;
 
const string[] responses = ["It is certain", "It is decidedly so",
Line 584 ⟶ 1,266:
}
}
</syntaxhighlight>
</lang>
Output:
<pre>Welcome to 8 ball! Please enter your question to find the answers you seek.
Line 596 ⟶ 1,278:
{{libheader| windows}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Magic_8_ball;
 
Line 647 ⟶ 1,329:
ClearScreen;
end;
end.</langsyntaxhighlight>
{{out}}
<pre>Magic 8 Ball! Ask question and hit ENTER key for the answer!</pre>
Line 655 ⟶ 1,337:
 
(Hit ENTER key to ask again)</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
answers$[] = [ "It is certain" "It is decidedly so" "Without a doubt" "Yes, definitely" "You may rely on it" "As I see it, yes" "Most likely" "Outlook good" "Signs point to yes" "Yes" "Reply hazy, try again" "Ask again later" "Better not tell you now" "Cannot predict now" "Concentrate and ask again" "Don't bet on it" "My reply is no" "My sources say no" "Outlook not so good" "Very doubtful" ]
print "q to quit."
repeat
print "What would you like to know? "
q$ = input
until q$ = "q"
print answers$[randint len answers$[]]
.
</syntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: io kernel random sequences ;
IN: rosetta-code.magic-8-ball
 
Line 674 ⟶ 1,368:
 
[ "? : " write flush readln empty? [ f ]
[ phrases random print t ] if ] loop</langsyntaxhighlight>
{{out}}
<pre>
Line 681 ⟶ 1,375:
Yes
? : Are cats secretly alien spies?
 
Signs point to yes
? : Am I a genius?
Line 689 ⟶ 1,382:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">dim as string answer(0 to 19) = { "It is certain.", "It is decidedly so.", "Without a doubt.", "Yes – definitely.",_
"You may rely on it.", "As I see it, yes.", "Most likely.", "Outlook good.",_
"Yes.", "Signs point to yes.", "Reply hazy, try again.", "Ask again later.",_
Line 704 ⟶ 1,397:
print answer(int(rnd*20))
print
loop</langsyntaxhighlight>
 
=={{header|FOCAL}}==
{{works with |FOCAL| for 6502 processors, string handling may be different in PDP-8/11-type flavors.}}
{{based on the BASIC versions}}
<syntaxhighlight lang="focal">
 
 
1.09 S FRAN(-1);C randomize
1.10 S FISL(80,A$);C set length to 80 char.
1.20 T !"Magic 8-Ball (Q to quit)"!!
1.30 T "What would you like to know?"!
1.40 S FSTI(80,A$(0),13);C input from console to A$, CR delimited
1.42 S Z=A$(0);I (Z-81) , 50.1;C Capital Q, if first char = then branch
1.44 S Z=A$(0);I (Z-113) , 50.1;C lowercase q, if first char = then branch
 
1.50 T !
1.60 S R=FINT(FRAN(0)*20+1);C get a random integer 1-20
1.70 D R*2;C 'do' calculated response subroutine
1.80 T !!"What else would you like to know?"!;G 1.4;C loop up to A$ input
 
2.10 T "It is certain.";R;C print a response and return from subroutine
4.10 T "It is decidedly so.";R;C etc.....
6.10 T "Without a doubt.";R
8.10 T "Yes - definitely.";R
10.10 T "You may rely on it.";R
12.10 T "As I see it, yes.";R
14.10 T "Most likely.";R
16.10 T "Outlook good.";R
18.10 T "Yes.";R
20.10 T "Signs point to yes.";R
22.10 T "Reply hazy, try again.";R
24.10 T "Ask again later.";R
26.10 T "Better not tell you now.";R
28.10 T "Cannot predict now.";R
30.10 T "Concentrate and ask again.";R
32.10 T "Don't count on it.";R
34.10 T "My reply is no.";R
36.10 T "My sources say no.";R
38.10 T "Outlook not so good.";R
40.10 T "Very doubtful.";R
50.10 T !"Thanks for consulting Magic 8-Ball for your life choices."!
50.20 Q
 
</syntaxhighlight>
 
=={{header|Forth}}==
Line 712 ⟶ 1,449:
Responses are created as executable routines that return their Execution token to the Forth Data Stack.
The responses are retrieved from the data stack and compiled into a vector table, after which it is trivial for RANDOM to select any response.
<LANGsyntaxhighlight lang="forth">\ magic eight ball Rosetta Code
INCLUDE RANDOM.FS
DECIMAL
Line 747 ⟶ 1,484:
BEGIN CR ." ? :" PAD 80 ACCEPT 0>
WHILE CR 19 RANDOM MAGIC8BALL CR
REPEAT ;</LANGsyntaxhighlight>
Test at the console<pre> ok
GO
Line 766 ⟶ 1,503:
 
=={{header|Fortran}}==
<langsyntaxhighlight FORTRANlang="fortran">PROGRAM EIGHT_BALL
CHARACTER(LEN=100) :: RESPONSE
CHARACTER(LEN=100) :: QUESTION
Line 807 ⟶ 1,544:
PRINT*, "Response: ", TRIM(RESPONSES(FLOOR(R*20))), NEW_LINE('A')
ENDDO
END PROGRAM EIGHT_BALL</langsyntaxhighlight>
Output:
<pre> Welcome to 8 Ball! Ask a question to find the answers
Line 815 ⟶ 1,552:
Is the future bright?
Response: Signs point to yes</pre>
 
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 856 ⟶ 1,594:
log.Fatal(err)
}
}</langsyntaxhighlight>
 
{{out}}
Line 876 ⟶ 1,614:
? :
</pre>
=={{header|GW-BASIC}}==
<syntaxhighlight lang="gwbasic">0 DATA "It is certain.", "It is decidedly so."
20 DATA "Without a doubt.", "Yes - definitely."
30 DATA "You may rely on it.", "As I see it, yes."
40 DATA "Most likely.", "Outlook good."
50 DATA "Yes.", "Signs point to yes."
60 DATA "Reply hazy, try again.", "Ask again later."
70 DATA "Better not tell you now.", "Cannot predict now."
80 DATA "Concentrate and ask again.", "Don't count on it."
90 DATA "My reply is no.", "My sources say no."
100 DATA "Outlook not so good.", "Very doubtful."
110 DIM M8BALL$(20)
120 FOR I=0 TO 19
130 READ M8BALL$(I)
140 NEXT I
150 RANDOMIZE TIMER
160 INPUT "What would you like to know? ", Q$
170 PRINT M8BALL$(INT(RND*20))
180 END</syntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import System.Random (getStdRandom, randomR)
import Control.Monad (forever)
 
Line 909 ⟶ 1,667:
getLine
n <- getStdRandom (randomR (0, pred $ length answers))
putStrLn $ answers !! n</langsyntaxhighlight>
 
=={{header|J}}==
Any array should work as the possible answers for the left (x) argument to eight_ball .
<syntaxhighlight lang="j">
<lang J>
NB. translated from awk
 
Line 925 ⟶ 1,683:
end.
)
</syntaxhighlight>
</lang>
 
<pre>
Line 939 ⟶ 1,697:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.util.Random;
import java.util.Scanner;
Line 967 ⟶ 1,725:
}
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 984 ⟶ 1,742:
 
=={{header|JavaScript}}==
<syntaxhighlight lang="javascript">
<lang JavaScript>
//console
var answers = [ "It is certain", "It is decidedly so", "Without a doubt",
Line 1,002 ⟶ 1,760:
console.log(answers[Math.floor(Math.random()*answers.length)]);
}
</syntaxhighlight>
</lang>
 
{{output}}
Line 1,014 ⟶ 1,772:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">const responses = ["It is certain", "It is decidedly so", "Without a doubt",
"Yes, definitely", "You may rely on it", "As I see it, yes", "Most likely",
"Outlook good", "Signs point to yes", "Yes", "Reply hazy, try again",
Line 1,028 ⟶ 1,786:
println(rand(responses))
end
</langsyntaxhighlight>{{out}}
<pre>
Ask a question (blank to exit):
Line 1,037 ⟶ 1,795:
Reply hazy, try again
</pre>
 
=={{header|K}}==
{{works with|ngn/k}}<syntaxhighlight lang=K>ask:{` 0:*1?"/"\"It is certain/It is decidedly so/Without a doubt/Yes, definitely/You may rely on it/As I see it, yes/Most likely/Outlook good/Signs point to yes/Yes/Reply hazy, try again/Ask again later/Better not tell you now/Cannot predict now/Concentrate and ask again/Don't bet on it/My reply is no/My sources say no/Outlook not so good/Very doubtful"}</syntaxhighlight>
 
Examples:
 
<syntaxhighlight lang=K>ask "Will this work?"
Signs point to yes
ask "Was this worth doing?"
Without a doubt
ask "Can I have a cookie?"
Yes, definitely</syntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// Version 1.2.40
 
import java.util.Random
Line 1,063 ⟶ 1,833:
println("\n$answer")
}
}</langsyntaxhighlight>
 
{{output}}
Line 1,090 ⟶ 1,860:
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="lb">data "It is certain","It is decidedly so","Without a doubt","Yes - definitely",_
"You may rely on it","As I see it, yes","Most likely","Outlook good","Yes",_
"Signs point to yes","Reply hazy, try again","Ask again later","Better not tell you now",_
Line 1,108 ⟶ 1,878:
print "Answer: ";c$(d)
goto [loop]
</syntaxhighlight>
</lang>
{{output}}
Sample session :
Line 1,121 ⟶ 1,891:
=={{header|Locomotive Basic}}==
 
<langsyntaxhighlight lang="locobasic">10 mode 2:defint a-z:randomize time
20 input "Your question (hit Return to quit)";i$
30 if i$="" then print "Goodbye!":end
Line 1,146 ⟶ 1,916:
270 print "My sources say no":return
280 print "Outlook not so good":return
290 print "Very doubtful":return</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">math.randomseed(os.time())
answers = {
"It is certain.", "It is decidedly so.", "Without a doubt.", "Yes, definitely.", "You may rely on it.",
Line 1,160 ⟶ 1,930:
question = io.read()
print("A: "..answers[math.random(#answers)])
end</langsyntaxhighlight>
{{out}}
<pre>Q: Are you truly a magic 8-ball?
Line 1,168 ⟶ 1,938:
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Magic.8.Ball {
answers=("It is certain", "It is decidedly so", "Without a doubt", "Yes, definitely", "You may rely on it", "As I see it, yes", "Most likely", "Outlook good", "Signs point to yes", "Yes", "Reply hazy, try again", "Ask again later", "Better not tell you now", "Cannot predict now", "Concentrate and ask again", "Don't bet on it", "My reply is no", "My sources say no", "Outlook not so good", "Very doubtful")
Line 1,182 ⟶ 1,952:
}
Magic.8.Ball
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">answers = {"It is certain.", "It is decidedly so.",
"Without a doubt.", "Yes - definitely.", "You may rely on it.",
"As I see it, yes.", "Most likely.", "Outlook good.", "Yes.",
Line 1,195 ⟶ 1,965:
q = Input["Please enter your question"];
SeedRandom[Hash[q]];
Print[RandomChoice[answers]]</langsyntaxhighlight>
{{out}}
<pre>[Are you awesome?]Yes - definitely.</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
set_random_state(make_random_state(true))$
 
block(
responses:["It is certain","It is decidedly so","Without a doubt","Yes definitely","You may rely on it","As I see it, yes","Most likely","Outlook good","Yes","Signs point to yes",
"Reply hazy, try again","Ask again later","Better not tell you now","Cannot predict now","Concentrate and ask again",
"Don't count on it","My reply is no","My sources say no","Outlook not so good","Very doubtful"],
read("Ask a question"),
print(responses[random(20)])
);
</syntaxhighlight>
{{out}}
<pre>
"Ask a question"" ""Are you aware of yourself?";
"My sources say no"
</pre>
 
=={{header|min}}==
{{works with|min|0.19.6}}
<langsyntaxhighlight lang="min">randomize ; Seed the RNG with current timestamp.
 
(
Line 1,216 ⟶ 2,004:
(phrases dup size pred random get puts!) :answer
 
(true) ("Ask a question" ask answer) while</langsyntaxhighlight>
{{out}}
<pre>
Line 1,227 ⟶ 2,015:
 
=={{header|MiniScript}}==
<langsyntaxhighlight MiniScriptlang="miniscript">answers = ["It is certain", "It is decidedly so", "Without a doubt",
"Yes, definitely", "You may rely on it", "As I see it, yes",
"Most likely", "Outlook good", "Signs point to yes", "Yes",
Line 1,237 ⟶ 2,025:
print "Ask your question and the Magic 8 Ball will give you the answer!"
input "What is your question?"
print answers[rnd * answers.len]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,246 ⟶ 2,034:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import random, strutils
 
const Answers = ["It is certain", "It is decidedly so", "Without a doubt",
Line 1,271 ⟶ 2,059:
echo Answers[rand(19)], ".\n"
except EOFError:
exit()</langsyntaxhighlight>
 
{{out}}
Line 1,284 ⟶ 2,072:
 
=={{header|ooRexx}}==
<syntaxhighlight lang="oorexx">
<lang ooRexx>
/* REXX */
a=.array~of("It is certain", "It is decidedly so", "Without a doubt",,
Line 1,300 ⟶ 2,088:
Say a[random(1,20)]
Say ''
End</langsyntaxhighlight>
{{out}}
<pre>your question:
Line 1,311 ⟶ 2,099:
 
your question:</pre>
=={{header|Pascal}}==
==={{header|Free Pascal}}===
nearly copy of [[Magic_8-ball#Delphi|Delphi]]
<syntaxhighlight lang="pascal">
program Magic_8_ball;
{$IFDEF WINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
uses
SysUtils,crt;
const
answers: array[0..19] of string = ('It is certain.', 'It is decidedly so.',
'Without a doubt.', 'Yes - definitely.', 'You may rely on it.',
'As I see it, yes.', 'Most likely.', 'Outlook good.', 'Yes.',
'Signs point to yes.', 'Reply hazy, try again.', 'Ask again later',
'Better not tell you now.', 'Cannot predict now.',
'Concentrate and ask again.', 'Don''t count on it.', 'My reply is no.',
'My sources say no.', 'Outlook not so good.', 'Very doubtful.');
begin
Randomize;
repeat
writeln('Magic 8 Ball! Ask question and hit ENTER key for the answer!');
readln;
ClrScr;
writeln(answers[Random(length(answers))],#13#10);
writeln('Hit ESCape to leave');
repeat until keypressed;
ClrScr;
until readkey=#27;
end.
</syntaxhighlight>
{{out}}
<pre>
Magic 8 Ball! Ask question and hit ENTER key for the answer!
Will I find my mobile again :-(
 
Very doubtful.
 
Hit ESCape to leave </pre>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">@a = ('It is certain', 'It is decidedly so', 'Without a doubt', 'Yes, definitely',
'You may rely on it', 'As I see it, yes', 'Most likely', 'Outlook good',
'Signs point to yes', 'Yes', 'Reply hazy, try again', 'Ask again later',
Line 1,324 ⟶ 2,149:
last unless <> =~ /\w/;
print @a[int rand @a], "\n";
}</langsyntaxhighlight>
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
<!--<lang Phix>(notonline)-->
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/magic8ball.htm here].
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">answers</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"As I see it, yes"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Ask again later"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"You may rely on it"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"Without a doubt"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Don't bet on it"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Outlook not so good"</span><span style="color: #0000FF;">,</span>
Line 1,336 ⟶ 2,165:
<span style="color: #008000;">"My sources say no"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Yes, definitely"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Yes, probably not"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"Very doubtful"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Your question has already been answered"</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">vbox</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">question</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">answer</span>
<span style="color: #004080;">stringsequence</span> <span style="color: #000000;">sanswered</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">prompt_string</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Please enter your question or a blank line to quit:"</span><span style="color: #0000FF;">){}</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">clearkey</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">!=</span><span style="color: #008000;">""</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">answers</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">answers</span><span style="color: #0000FF;">))]})</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</lang>-->
<span style="color: #008080;">function</span> <span style="color: #000000;">key_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*dlg*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_ESC</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">IUP_CLOSE</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- (standard practice for me)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_F5</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- (let browser reload work)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_CR</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">q</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">question</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"VALUE"</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">reply</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">,</span><span style="color: #000000;">answered</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">reply</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Your question has already been answered"</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">answered</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">answered</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">reply</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">answers</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">answers</span><span style="color: #0000FF;">))]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">IupSetStrAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">answer</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">reply</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">clearkey</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">clearkey</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">question</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"VALUE"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">clearkey</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_CONTINUE</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">question</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupText</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"EXPAND=HORIZONTAL"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">answer</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupLabel</span><span style="color: #0000FF;">(</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"EXPAND=HORIZONTAL"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">vbox</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupVbox</span><span style="color: #0000FF;">({</span><span style="color: #000000;">question</span><span style="color: #0000FF;">,</span><span style="color: #000000;">answer</span><span style="color: #0000FF;">},</span><span style="color: #008000;">`MARGIN=40x40`</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">vbox</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`TITLE="Magic 8-ball",SIZE=250x100`</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetCallback</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"KEY_CB"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"key_cb"</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupHide</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</syntaxhighlight>-->
 
=={{header|PHP}}==
===CLI===
<langsyntaxhighlight lang="php"><?php
 
$fortunes = array(
Line 1,392 ⟶ 2,258:
 
echo 'A: ', $fortunes[ array_rand( $fortunes ) ], PHP_EOL;
</syntaxhighlight>
</lang>
 
{{Out}}
Line 1,411 ⟶ 2,277:
A: It is certain
</pre>
 
=={{header|PL/M}}==
<syntaxhighlight lang="PL/M">
100H:
/*
THE PROCEDURES CLOCK AND RND USE A FEATURE OF PICKLES AND TROUT CP/M2
AND ARE NOT LIKELY TO WORK ON OTHER VERSIONS OF CP/M
*/
 
BDOS: PROCEDURE (FN, ARG) BYTE; DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; MEMORY(0)=BDOS(0,0); END EXIT;
 
TTYIN: PROCEDURE BYTE; DECLARE C BYTE; RETURN BDOS(1,0); END TTYIN;
 
PRINT: PROCEDURE (S); DECLARE S ADDRESS; S=BDOS(9,S); END PRINT;
 
CLOCK: PROCEDURE;
DECLARE B$ADR ADDRESS; DECLARE B BASED B$ADR BYTE;
B$ADR=047H;
B = 0EH; GO TO 043H; END CLOCK;
 
RND: PROCEDURE ADDRESS;
DECLARE E$ADR ADDRESS; DECLARE E BASED E$ADR BYTE;
DECLARE D$ADR ADDRESS; DECLARE D BASED D$ADR BYTE;
DECLARE L$ADR ADDRESS; DECLARE L BASED L$ADR BYTE;
DECLARE H$ADR ADDRESS; DECLARE H BASED H$ADR BYTE;
DECLARE (NUM, DE, HL) ADDRESS;
E$ADR=048H; D$ADR=049H; L$ADR=04AH; H$ADR=04BH;
CALL CLOCK;
DE = D * 255 + E; HL = H * 255 + L; NUM = DE * HL; RETURN NUM;
END RND;
 
EIGHTBALL: DO;
 
 
DECLARE CR LITERALLY '13';
DECLARE CRLF(3) BYTE INITIAL (13,10,'$');
DECLARE CHR BYTE;
DECLARE NUM ADDRESS;
DECLARE GREETING(30) BYTE INITIAL
('ASK AND ALL WILL BE REVEALED $');
DECLARE MSG01(41) BYTE INITIAL
( 'AS I SEE IT, YES $' );
DECLARE MSG02(41) BYTE INITIAL
( 'ASK AGAIN LATER $' );
DECLARE MSG03(41) BYTE INITIAL
( 'BETTER NOT TELL YOU NOW $' );
DECLARE MSG04(41) BYTE INITIAL
( 'CANNOT PREDICT NOW $' );
DECLARE MSG05(41) BYTE INITIAL
( 'CONCENTRATE AND ASK AGAIN $' );
DECLARE MSG06(41) BYTE INITIAL
( 'DON''T BET ON IT $' );
DECLARE MSG07(41) BYTE INITIAL
( 'IT IS CERTAIN $' );
DECLARE MSG08(41) BYTE INITIAL
( 'IT IS DECIDEDLY SO $' );
DECLARE MSG09(41) BYTE INITIAL
( 'MOST LIKELY $' );
DECLARE MSG10(41) BYTE INITIAL
( 'MY REPLY IS NO $' );
DECLARE MSG11(41) BYTE INITIAL
( 'MY SOURCES SAY MAYBE $' );
DECLARE MSG12(41) BYTE INITIAL
( 'MY SOURCES SAY NO $' );
DECLARE MSG13(41) BYTE INITIAL
( 'OUTLOOK GOOD $' );
DECLARE MSG14(41) BYTE INITIAL
( 'OUTLOOK NOT SO GOOD $' );
DECLARE MSG15(41) BYTE INITIAL
( 'REPLY HAZY, TRY AGAIN $' );
DECLARE MSG16(41) BYTE INITIAL
( 'SIGNS POINT TO YES $' );
DECLARE MSG17(41) BYTE INITIAL
( 'VERY DOUBTFUL $' );
DECLARE MSG18(41) BYTE INITIAL
( 'WITHOUT A DOUBT $' );
DECLARE MSG19(41) BYTE INITIAL
( 'YES $' );
DECLARE MSG20(41) BYTE INITIAL
( 'YES, DEFINITELY $' );
DECLARE MSG21(41) BYTE INITIAL
( 'YES, PROBABLY NOT $' );
DECLARE MSG22(41) BYTE INITIAL
( 'YOU MAY RELY ON IT $' );
DECLARE MSG23(41) BYTE INITIAL
( 'YOUR QUESTION HAS ALREADY BEEN ANSWERED $' );
 
NUM = RND MOD 24;
 
CALL PRINT(.GREETING);
CALL PRINT(.CRLF);
 
DO WHILE CHR <> CR;
CHR = TTYIN;
END;
 
CALL PRINT(.CRLF);
 
DO CASE NUM;
CALL PRINT(.MSG01);
CALL PRINT(.MSG02);
CALL PRINT(.MSG03);
CALL PRINT(.MSG04);
CALL PRINT(.MSG05);
CALL PRINT(.MSG06);
CALL PRINT(.MSG07);
CALL PRINT(.MSG08);
CALL PRINT(.MSG09);
CALL PRINT(.MSG10);
CALL PRINT(.MSG11);
CALL PRINT(.MSG12);
CALL PRINT(.MSG13);
CALL PRINT(.MSG14);
CALL PRINT(.MSG15);
CALL PRINT(.MSG16);
CALL PRINT(.MSG17);
CALL PRINT(.MSG18);
CALL PRINT(.MSG19);
CALL PRINT(.MSG20);
CALL PRINT(.MSG21);
CALL PRINT(.MSG22);
CALL PRINT(.MSG23);
END;
CALL PRINT(.CRLF);
 
CALL EXIT;
 
END;
EOF
 
</syntaxhighlight>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Define.s Magic8="● It is certain."+#LF$+
"● It is decidedly so."+#LF$+
"● Without a doubt."+#LF$+
Line 1,440 ⟶ 2,438:
If Len(q$)=0 : End : EndIf
PrintN(StringField(Magic8,Random(CountString(Magic8,#LF$),1),#LF$))
ForEver</langsyntaxhighlight>
{{out}}
<pre>MAGIC8: What would you like To know? Do you want me to take over the world?
Line 1,452 ⟶ 2,450:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import random
 
s = ('It is certain', 'It is decidedly so', 'Without a doubt', 'Yes, definitely',
Line 1,466 ⟶ 2,464:
question = input('Ask your question:')
if len(question) == 0: break
answer = random.choice(s)
if question in q_and_a:
print('Your question has already been answered')
else:
answer = random.choice(s)
q_and_a[question] = answer
print(answer)</langsyntaxhighlight>
 
{{output}}
Line 1,489 ⟶ 2,486:
 
=={{header|Quackery}}==
<langsyntaxhighlight Quackerylang="quackery">[ 20 random
[ table
$ "Concentrate and ask again." $ "Yes."
Line 1,501 ⟶ 2,498:
$ "Don't count on it." $ "Yes - definitely."
$ "My sources say no." $ "As I see it, yes." ]
do echo$ cr ] is 8-ball ( --> )</langsyntaxhighlight>
 
'''Testing in Quackery shell:'''
Line 1,528 ⟶ 2,525:
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">eight_ball <- function()
{
responses <- c("It is certain", "It is decidedly so", "Without a doubt",
Line 1,559 ⟶ 2,556:
{
eight_ball()
}</langsyntaxhighlight>
Output:
<pre>Welcome to 8 ball!
Line 1,571 ⟶ 2,568:
 
=={{header|Racket}}==
<syntaxhighlight lang="text">(define eight-ball-responses
(list "It is certain" "It is decidedly so" "Without a doubt" "Yes definitely" "You may rely on it"
"As I see it, yes" "Most likely" "Outlook good" "Yes" "Signs point to yes"
Line 1,588 ⟶ 2,585:
(read-line)
(displayln (magic-eightball))
(loop)))</langsyntaxhighlight>
{{out}}
We'll see if it's right.
Line 1,596 ⟶ 2,593:
{{works with|Rakudo|2018.03}}
 
<syntaxhighlight lang="raku" perl6line>put 'Please enter your question or a blank line to quit.';
 
["It is certain", "It is decidedly so", "Without a doubt", "Yes, definitely",
Line 1,603 ⟶ 2,600:
"Better not tell you now", "Cannot predict now", "Concentrate and ask again",
"Don't bet on it", "My reply is no", "My sources say no", "Outlook not so good",
"Very doubtful"].roll.put while prompt('? : ').chars;</langsyntaxhighlight>
Output very similar to C, Kotlin and zkl examples.
 
=={{header|REXX}}==
===version 1===
<langsyntaxhighlight lang="rexx">
/* REXX */
Call mk_a "It is certain", "It is decidedly so", "Without a doubt",,
Line 1,633 ⟶ 2,630:
End
Return
</syntaxhighlight>
</lang>
{{out}}
<pre>your question:
Line 1,653 ⟶ 2,650:
 
Also, this REXX version appends a period to the phrase as per the (linked) documentation.
<langsyntaxhighlight lang="rexx">/*REXX program simulates the "shaking" of a "Magic 8-ball" and displaying an answer.*/
$="It is certain ÷It is decidedly so ÷Without a doubt÷Yes, definitely÷Signs point to yes",
"÷You may rely on it÷ As I see it, yes÷My reply is no÷Outlook good÷Outlook not so good",
Line 1,659 ⟶ 2,656:
"÷Concentrate and ask again÷Don't bet on it÷Most likely÷My sources say no÷Very doubtful"
 
say space(translate(word(translate(translate($, '┼', " "), , '÷'), random(1, 20)), ,"┼")).</langsyntaxhighlight>
{{out|possible output|text=&nbsp; when invoking the REXX program:}}
<pre>
Line 1,666 ⟶ 2,663:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Magic 8-Ball
 
Line 1,679 ⟶ 2,676:
index = random(len(answers)-1)+1
see answers[index] + nl
</syntaxhighlight>
</lang>
Output:
<pre>
It is certain
</pre>
=={{header|RPL}}==
{{trans|Quackery}}
{ <span style="color:red">"Concentrate and ask again." "Yes."
"Better not tell you now." "Most likely."
"Reply hazy, try again." "Outlook good."
"Outlook not so good." "It is certain."
"Cannot predict now." "Very doubtful."
"Signs point to yes." "My reply is no."
"It is decidedly so." "Ask again later."
"You may rely on it." "Without a doubt."
"Don't count on it." "Yes - definitely."
"My sources say no." "As I see it, yes."</span> }
RAND <span style="color:red">20</span> * CEIL GET
≫ '<span style="color:blue">'''MAG8B'''</span>' STO
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">#!/usr/bin/ruby
 
class EightBall
Line 1,724 ⟶ 2,737:
eight_ball = EightBall.new
eight_ball.run
</syntaxhighlight>
</lang>
Output:
<pre>Welcome to 8 ball! Ask your question below. Type 'quit' to exit the program.
Line 1,733 ⟶ 2,746:
=={{header|Rust}}==
{{libheader|rand}}
<langsyntaxhighlight lang="rust">extern crate rand;
 
use rand::prelude::*;
Line 1,775 ⟶ 2,788:
input_line.clear();
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,789 ⟶ 2,802:
 
=={{header|Sather}}==
<langsyntaxhighlight lang="sather">class MAIN is
const answers: ARRAY{STR} :=
|
Line 1,808 ⟶ 2,821:
end;
end;
end;</langsyntaxhighlight>
 
=={{Header|Scala}}==
<langsyntaxhighlight Scalalang="scala">import scala.util.Random
 
object Magic8Ball extends App {
Line 1,839 ⟶ 2,852:
}
}
}</langsyntaxhighlight>
 
=={{Header|Tcl}}==
<langsyntaxhighlight lang="tcl">namespace path {::tcl::mathop ::tcl::mathfunc}
 
set answers {
Line 1,875 ⟶ 2,888:
puts "⑧ says “$answer”"
puts -nonewline "Question: "; flush stdout
}</langsyntaxhighlight>
{{out}}
<pre>$ tclsh magic8.tcl
Line 1,885 ⟶ 2,898:
 
=={{Header|Tiny BASIC}}==
{{works with|TinyBasic}}
<lang>10 PRINT "Concentrate hard on your question, then tell me your favorite number."
<syntaxhighlight lang="basic">10 PRINT "Concentrate hard on your question, then tell me your favorite number."
20 INPUT N
30 REM in lieu of a random number generator
Line 1,930 ⟶ 2,944:
1180 PRINT "Outlook not so good."
1185 END
1190 PRINT "Very doubtful."</langsyntaxhighlight>
 
=={{header|Transd}}==
The description of Magic ball never says that the answer must be random. Hey, how can we give the user just random answers?
<syntaxhighlight lang="Scheme">#lang transd
 
MainModule : {
ans: ["It is certain.", "It is decidedly so.", "Without a doubt.",
"Yes - definitely.", "You may rely on it.", "As I see it, yes.",
"Most likely.", "Outlook good.", "Yes.", "Signs point to yes.",
"Reply hazy, try again.", "Ask again later.",
"Better not tell you now.", "Cannot predict now.",
"Concentrate and ask again.", "Don't count on it.",
"My reply is no.", "My sources say no.", "Outlook not so good.",
"Very doubtful."],
_start: (lambda locals: sense 0 quest ""
(while true
(lout "Please, ask your question:")
(getline quest)
(if (not (size quest)) break)
(if (neq (back quest) "?") (lout "\nEh?") continue)
(= sense 0)
(tql quest reduce: ["col1"] using: (λ c Char() (+= sense c)))
(lout (get ans (mod sense 20)))
)
)
}</syntaxhighlight>
 
=={{header|uBasic/4tH}}==
{{works with|version 3.64.0R4}}
<langsyntaxhighlight Basiclang="basic">Push Dup("It is certain"), Dup("It is decidedly so"), Dup("Without a doubt")
Push Dup("Yes, definitely"), Dup("You may rely on it")
Push Dup("As I see it, yes"), Dup("Most likely"), Dup("Outlook good")
Push Dup("Signs point to yes"), Dup("Yes"), Dup("Reply hazy, try again")
Push Dup("Ask again later"), Dup("Better not tell you now")
Push Dup("Cannot predict now"), Dup("Concentrate and ask again")
Push Dup("Don't bet on it"), Dup("My reply is no"), Dup("My sources say no")
Push Dup("Outlook not so good"), Dup("Very doubtful")
' read the replies
For x = 0 to Used() - 1 : @(x) = Pop(): Next
Line 1,951 ⟶ 2,990:
Until Len(r)=0 ' check for empty line
Print Show(@(Rnd(x))) : Print ' show the reply
Loop</langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
{{works with|Bourne Again Shell}}
<langsyntaxhighlight BASHlang="bash">#!/bin/bash
declare -ra RESPONSES=("It is certain" "It is decidedly so" "Without a doubt"
"Yes definitely" "You may rely on it" "As I see it yes"
"Most likely" "Outlook good" "Signs point to yes" "Yes"
Line 1,968 ⟶ 3,008:
find the answers you seek. Type 'quit' to exit.\n\n"
 
until
while true; do
read -p 'Enter Question: ' question
if [[ "$questionREPLY" == "quit" ]; then]
do printf "Response: %s\n\n" "${RESPONSES[RANDOM % ${#RESPONSES[@]}]}"
exit 0
done
fi
</syntaxhighlight>
 
{{out}}
response_index=$(($RANDOM % 20))
 
printf "Response: ${RESPONSES[response_index]}\n\n"
done</lang>
Output:
<pre>Welcome to 8 ball! Enter your questions using the prompt below to
find the answers you seek. Type 'quit' to exit.
Line 1,987 ⟶ 3,023:
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight ecmascriptlang="wren">import "random" for Random
import "io" for Stdin, Stdout
 
Line 2,009 ⟶ 3,045:
var answer = answers[rand.int(20)]
System.print("\n%(answer)")
}</langsyntaxhighlight>
 
{{out}}
Line 2,030 ⟶ 3,066:
? :
</pre>
 
=={{header|XBS}}==
Random Choice
<syntaxhighlight lang="xbs">set Responses = ["It is Certain.","It is decidedly so.","Without a doubt.","Yes definitely.","You may rely on it","As I see it, yes.","Most likely.","Outlook good.","Yes.","Sign points to yes.","Reply hazy, try again.","Ask again later.","Better not tell you now.","Cannot predict now.","Concentrate and ask again.","Don't count on it.","My reply is no.","My sources say no.","Outlook not so good.","Very doubtful."];
 
while(true){
window->prompt("Ask 8-Ball a question");
log(Responses[rnd(0,?Responses-1)]);
}</syntaxhighlight>
Non-Random Choice
<syntaxhighlight lang="xbs">set Responses = ["It is Certain.","It is decidedly so.","Without a doubt.","Yes definitely.","You may rely on it","As I see it, yes.","Most likely.","Outlook good.","Yes.","Sign points to yes.","Reply hazy, try again.","Ask again later.","Better not tell you now.","Cannot predict now.","Concentrate and ask again.","Don't count on it.","My reply is no.","My sources say no.","Outlook not so good.","Very doubtful."];
 
func Compile(String){
set n = 0;
foreach(k,v as String){
n+=string.char(v);
}
send n;
}
 
while(true){
let Response = window->prompt("Ask 8-Ball a question");
let N = Compile(Response);
log(Responses[N%(?Responses)]);
}</syntaxhighlight>
 
=={{header|XPL0}}==
Translation of Kotlin and C.
<langsyntaxhighlight XPL0lang="xpl0">int Answers, Answer, Question;
[Answers:= [
"It is certain", "It is decidedly so", "Without a doubt",
Line 2,053 ⟶ 3,114:
Text(0, Answer); CrLf(0);
]
]</langsyntaxhighlight>
 
{{out}}
Line 2,073 ⟶ 3,134:
=={{header|zkl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="zkl">answers:=T(
"It is certain", "It is decidedly so", "Without a doubt",
"Yes, definitely", "You may rely on it", "As I see it, yes",
Line 2,084 ⟶ 3,145:
);
println("Please enter your question or a blank line to quit.");
while(ask("? : ")){ println(answers[(0).random(answers.len())]) }</langsyntaxhighlight>
{{out}}
<pre>