Magic 8-ball: Difference between revisions

no edit summary
No edit summary
No edit summary
 
(17 intermediate revisions by 12 users not shown)
Line 501:
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}}===
Line 523 ⟶ 546:
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}}===
Line 550 ⟶ 595:
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}}===
Line 895 ⟶ 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">
Line 928 ⟶ 1,071:
 
bgcolor 0, 0, 255
cls graphics
 
formid 1
Line 1,194 ⟶ 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}}==
Line 1,243 ⟶ 1,398:
print
loop</syntaxhighlight>
 
=={{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 1,596 ⟶ 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}}==
Line 1,757 ⟶ 1,968:
{{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}}==
Line 1,870 ⟶ 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}}==
Line 2,011 ⟶ 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}}==
Line 2,283 ⟶ 2,681:
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}}==
<syntaxhighlight lang="ruby">#!/usr/bin/ruby
Line 2,559 ⟶ 2,973:
 
=={{header|uBasic/4tH}}==
{{works with|version 3.64.0R4}}
<syntaxhighlight lang="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 2,577 ⟶ 2,991:
Print Show(@(Rnd(x))) : Print ' show the reply
Loop</syntaxhighlight>
 
=={{header|UNIX Shell}}==
{{works with|Bourne Again Shell}}
<syntaxhighlight lang="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 2,593 ⟶ 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</syntaxhighlight>
Output:
<pre>Welcome to 8 ball! Enter your questions using the prompt below to
find the answers you seek. Type 'quit' to exit.
Line 2,612 ⟶ 3,023:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "io" for Stdin, Stdout