Magic 8-ball: Difference between revisions
m
syntax highlighting fixup automation
(Applesoft BASIC) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 14:
{{trans|Python}}
<
‘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 34:
E
qs.add(question)
print(answer)</
=={{header|8080 Assembly}}==
<
puts: equ 9
gets: equ 10
Line 122:
xabcdat: equ buf + 60 ; Point RNG data into uninitialized memory,
; to make it more exciting.
</syntaxhighlight>
=={{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.
<
.model small
.stack 1024
Line 204:
xchg al,ah
;now the word is loaded into ax, big-endian.
ret</
{{out}}
Line 216:
=={{header|Action!}}==
<
DEFINE PTR="CARD"
DEFINE COUNT="20"
Line 254:
PutE()
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Magic_8-ball.png Screenshot from Atari 8-bit computer]
Line 276:
=={{header|Ada}}==
<syntaxhighlight lang="ada">
with Ada.Text_IO; use Ada.Text_IO;
Line 332:
end Main;
</syntaxhighlight>
{{out}}
<pre>
Line 348:
=={{header|ALGOL 68}}==
<
[]STRING answers = ("It is certain.", "It is decidedly so.",
"Without a doubt.", "Yes - definitely.", "You may rely on it.",
Line 362:
print((answers[ENTIER (random * UPB answers) + 1], new line))
OD
END</
=={{header|AppleScript}}==
<
display dialog some item of the answers</
=={{header|Applesoft BASIC}}==
{{trans|Commodore BASIC}}
<
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}}==
<
"It is certain" "It is decidedly so" "Without a doubt"
"Yes, definitely" "You may rely on it" "As I see it, yes"
Line 391:
input "Ask a question: "
print sample answers
]</
{{out}}
Line 401:
=={{header|AutoHotkey}}==
<
(join
"It is certain", "It is decidedly so", "Without a doubt","Yes, definitely",
Line 417:
MsgBox % answers[rnd]
gosub, Ask
return</
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f MAGIC_8-BALL.AWK
BEGIN {
Line 456:
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 471:
==={{header|BASIC256}}===
{{trans|QBasic}}
<
answer$[0] = "It is certain."
answer$[1] = "It is decidedly so."
Line 500:
print
end while
end</
==={{header|QBasic}}===
{{works with|QBasic}}
{{works with|QuickBasic|4.5}}
<
FOR i = 0 TO UBOUND(answer$): READ answer$(i): NEXT i
RANDOMIZE TIMER
Line 527:
DATA "Don't count on it.","My reply is no."
DATA "My sources say no.","Outlook not so good."
DATA "Very doubtful."</
==={{header|True BASIC}}===
{{trans|QBasic}}
<
FOR i = 1 to ubound(answer$)
READ answer$(i)
Line 554:
PRINT
LOOP
END</
==={{header|Yabasic}}===
{{trans|QBasic}}
<
for i = 0 to arraysize(answer$(),1): read answer$(i): next i
Line 578:
data "Don//t count on it.","My reply is no."
data "My sources say no.","Outlook not so good."
data "Very doubtful."</
=={{header|C}}==
{{trans|Kotlin}}
<
#include <stdlib.h>
#include <time.h>
Line 611:
if (question) free(question);
return 0;
}</
{{output}}
Line 634:
=={{header|C++}}==
<
#include <cstdlib>
#include <ctime>
Line 677:
std::cout << answers[std::rand() % answers.size()] << '\n';
}
}</
=={{header|C#}}==
<
using System;
Line 731:
</syntaxhighlight>
=={{header|CFEngine}}==
<
#!/var/cfengine/bin/cf-agent --no-lock
bundle agent __main__
Line 783:
depends_on => { "consider" };
}
</syntaxhighlight>
See https://docs.cfengine.com/docs/master/examples.html for a more complete example and introduction.
=={{header|Clojure}}==
<
(def responses
["It is certain" "It is decidedly so" "Without a doubt"
Line 803:
(read-line)
(println (rand-nth responses)))
</syntaxhighlight>
=={{header|CMake}}==
<
PROJECT(EightBall)
Line 844:
ADD_CUSTOM_TARGET(${PROJECT_NAME} ALL)
</syntaxhighlight>
No question specified:
<pre>$ cmake -H. -Bbuild
Line 874:
-- Generating done</pre>
=={{header|Commodore BASIC}}==
<syntaxhighlight lang="commodorebasicv2">
10 dim f$(19):for i=0 to 19:read f$(i):next
20 print chr$(147);chr$(14)
Line 897:
1080 data "My sources say no.","Outlook not so good."
1090 data "Very doubtful."
</syntaxhighlight>
=={{header|D}}==
<
const string[] responses = ["It is certain", "It is decidedly so",
Line 935:
}
}
</syntaxhighlight>
Output:
<pre>Welcome to 8 ball! Please enter your question to find the answers you seek.
Line 947:
{{libheader| windows}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
program Magic_8_ball;
Line 998:
ClearScreen;
end;
end.</
{{out}}
<pre>Magic 8 Ball! Ask question and hit ENTER key for the answer!</pre>
Line 1,008:
=={{header|Factor}}==
<
IN: rosetta-code.magic-8-ball
Line 1,025:
[ "? : " write flush readln empty? [ f ]
[ phrases random print t ] if ] loop</
{{out}}
<pre>
Line 1,039:
=={{header|FreeBASIC}}==
<
"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 1,054:
print answer(int(rnd*20))
print
loop</
=={{header|Forth}}==
Line 1,116:
=={{header|Fortran}}==
<
CHARACTER(LEN=100) :: RESPONSE
CHARACTER(LEN=100) :: QUESTION
Line 1,157:
PRINT*, "Response: ", TRIM(RESPONSES(FLOOR(R*20))), NEW_LINE('A')
ENDDO
END PROGRAM EIGHT_BALL</
Output:
<pre> Welcome to 8 Ball! Ask a question to find the answers
Line 1,167:
=={{header|Go}}==
{{trans|Kotlin}}
<
import (
Line 1,206:
log.Fatal(err)
}
}</
{{out}}
Line 1,227:
</pre>
=={{header|GW-BASIC}}==
<
20 DATA "Without a doubt.", "Yes - definitely."
30 DATA "You may rely on it.", "As I see it, yes."
Line 1,244:
160 INPUT "What would you like to know? ", Q$
170 PRINT M8BALL$(INT(RND*20))
180 END</
=={{header|Haskell}}==
<
import Control.Monad (forever)
Line 1,279:
getLine
n <- getStdRandom (randomR (0, pred $ length answers))
putStrLn $ answers !! n</
=={{header|J}}==
Any array should work as the possible answers for the left (x) argument to eight_ball .
<syntaxhighlight lang="j">
NB. translated from awk
Line 1,295:
end.
)
</syntaxhighlight>
<pre>
Line 1,309:
=={{header|Java}}==
<
import java.util.Random;
import java.util.Scanner;
Line 1,337:
}
}
</syntaxhighlight>
{{out}}
Line 1,354:
=={{header|JavaScript}}==
<syntaxhighlight lang="javascript">
//console
var answers = [ "It is certain", "It is decidedly so", "Without a doubt",
Line 1,372:
console.log(answers[Math.floor(Math.random()*answers.length)]);
}
</syntaxhighlight>
{{output}}
Line 1,384:
=={{header|Julia}}==
<
"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,398:
println(rand(responses))
end
</
<pre>
Ask a question (blank to exit):
Line 1,409:
=={{header|Kotlin}}==
<
import java.util.Random
Line 1,433:
println("\n$answer")
}
}</
{{output}}
Line 1,460:
=={{header|Liberty BASIC}}==
<
"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,478:
print "Answer: ";c$(d)
goto [loop]
</syntaxhighlight>
{{output}}
Sample session :
Line 1,491:
=={{header|Locomotive Basic}}==
<
20 input "Your question (hit Return to quit)";i$
30 if i$="" then print "Goodbye!":end
Line 1,516:
270 print "My sources say no":return
280 print "Outlook not so good":return
290 print "Very doubtful":return</
=={{header|Lua}}==
<
answers = {
"It is certain.", "It is decidedly so.", "Without a doubt.", "Yes, definitely.", "You may rely on it.",
Line 1,530:
question = io.read()
print("A: "..answers[math.random(#answers)])
end</
{{out}}
<pre>Q: Are you truly a magic 8-ball?
Line 1,538:
=={{header|M2000 Interpreter}}==
<syntaxhighlight 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,552:
}
Magic.8.Ball
</syntaxhighlight>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
"Without a doubt.", "Yes - definitely.", "You may rely on it.",
"As I see it, yes.", "Most likely.", "Outlook good.", "Yes.",
Line 1,565:
q = Input["Please enter your question"];
SeedRandom[Hash[q]];
Print[RandomChoice[answers]]</
{{out}}
<pre>[Are you awesome?]Yes - definitely.</pre>
Line 1,571:
=={{header|min}}==
{{works with|min|0.19.6}}
<
(
Line 1,586:
(phrases dup size pred random get puts!) :answer
(true) ("Ask a question" ask answer) while</
{{out}}
<pre>
Line 1,597:
=={{header|MiniScript}}==
<
"Yes, definitely", "You may rely on it", "As I see it, yes",
"Most likely", "Outlook good", "Signs point to yes", "Yes",
Line 1,607:
print "Ask your question and the Magic 8 Ball will give you the answer!"
input "What is your question?"
print answers[rnd * answers.len]</
{{out}}
<pre>
Line 1,616:
=={{header|Nim}}==
<
const Answers = ["It is certain", "It is decidedly so", "Without a doubt",
Line 1,641:
echo Answers[rand(19)], ".\n"
except EOFError:
exit()</
{{out}}
Line 1,654:
=={{header|ooRexx}}==
<syntaxhighlight lang="oorexx">
/* REXX */
a=.array~of("It is certain", "It is decidedly so", "Without a doubt",,
Line 1,670:
Say a[random(1,20)]
Say ''
End</
{{out}}
<pre>your question:
Line 1,683:
=={{header|Perl}}==
<
'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,694:
last unless <> =~ /\w/;
print @a[int rand @a], "\n";
}</
=={{header|Phix}}==
Line 1,700:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/magic8ball.htm here].
<!--<
<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>
Line 1,750:
<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>
<!--</
=={{header|PHP}}==
===CLI===
<
$fortunes = array(
Line 1,803:
echo 'A: ', $fortunes[ array_rand( $fortunes ) ], PHP_EOL;
</syntaxhighlight>
{{Out}}
Line 1,824:
=={{header|PureBasic}}==
<
"● It is decidedly so."+#LF$+
"● Without a doubt."+#LF$+
Line 1,851:
If Len(q$)=0 : End : EndIf
PrintN(StringField(Magic8,Random(CountString(Magic8,#LF$),1),#LF$))
ForEver</
{{out}}
<pre>MAGIC8: What would you like To know? Do you want me to take over the world?
Line 1,863:
=={{header|Python}}==
<
s = ('It is certain', 'It is decidedly so', 'Without a doubt', 'Yes, definitely',
Line 1,883:
answer = random.choice(s)
q_and_a[question] = answer
print(answer)</
{{output}}
Line 1,899:
=={{header|Quackery}}==
<
[ table
$ "Concentrate and ask again." $ "Yes."
Line 1,911:
$ "Don't count on it." $ "Yes - definitely."
$ "My sources say no." $ "As I see it, yes." ]
do echo$ cr ] is 8-ball ( --> )</
'''Testing in Quackery shell:'''
Line 1,938:
=={{header|R}}==
<
{
responses <- c("It is certain", "It is decidedly so", "Without a doubt",
Line 1,969:
{
eight_ball()
}</
Output:
<pre>Welcome to 8 ball!
Line 1,981:
=={{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,998:
(read-line)
(displayln (magic-eightball))
(loop)))</
{{out}}
We'll see if it's right.
Line 2,006:
{{works with|Rakudo|2018.03}}
<syntaxhighlight lang="raku"
["It is certain", "It is decidedly so", "Without a doubt", "Yes, definitely",
Line 2,013:
"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;</
Output very similar to C, Kotlin and zkl examples.
=={{header|REXX}}==
===version 1===
<
/* REXX */
Call mk_a "It is certain", "It is decidedly so", "Without a doubt",,
Line 2,043:
End
Return
</syntaxhighlight>
{{out}}
<pre>your question:
Line 2,063:
Also, this REXX version appends a period to the phrase as per the (linked) documentation.
<
$="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 2,069:
"÷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)), ,"┼")).</
{{out|possible output|text= when invoking the REXX program:}}
<pre>
Line 2,076:
=={{header|Ring}}==
<
# Project : Magic 8-Ball
Line 2,089:
index = random(len(answers)-1)+1
see answers[index] + nl
</syntaxhighlight>
Output:
<pre>
Line 2,095:
</pre>
=={{header|Ruby}}==
<
class EightBall
Line 2,134:
eight_ball = EightBall.new
eight_ball.run
</syntaxhighlight>
Output:
<pre>Welcome to 8 ball! Ask your question below. Type 'quit' to exit the program.
Line 2,143:
=={{header|Rust}}==
{{libheader|rand}}
<
use rand::prelude::*;
Line 2,185:
input_line.clear();
}
}</
{{out}}
<pre>
Line 2,199:
=={{header|Sather}}==
<
const answers: ARRAY{STR} :=
|
Line 2,218:
end;
end;
end;</
=={{Header|Scala}}==
<
object Magic8Ball extends App {
Line 2,249:
}
}
}</
=={{Header|Tcl}}==
<
set answers {
Line 2,285:
puts "⑧ says “$answer”"
puts -nonewline "Question: "; flush stdout
}</
{{out}}
<pre>$ tclsh magic8.tcl
Line 2,295:
=={{Header|Tiny BASIC}}==
<syntaxhighlight lang="text">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 2,340:
1180 PRINT "Outlook not so good."
1185 END
1190 PRINT "Very doubtful."</
=={{header|uBasic/4tH}}==
{{works with|version 3.64.0}}
<
Push Dup("Yes, definitely"), Dup("You may rely on it")
Push Dup("As I see it, yes"), Dup("Most likely"), Dup("Outlook good")
Line 2,361:
Until Len(r)=0 ' check for empty line
Print Show(@(Rnd(x))) : Print ' show the reply
Loop</
=={{header|UNIX Shell}}==
{{works with|Bourne Again Shell}}
<
RESPONSES=("It is certain" "It is decidedly so" "Without a doubt"
Line 2,387:
printf "Response: ${RESPONSES[response_index]}\n\n"
done</
Output:
<pre>Welcome to 8 ball! Enter your questions using the prompt below to
Line 2,397:
=={{header|Wren}}==
{{trans|Kotlin}}
<
import "io" for Stdin, Stdout
Line 2,419:
var answer = answers[rand.int(20)]
System.print("\n%(answer)")
}</
{{out}}
Line 2,443:
=={{header|XBS}}==
Random Choice
<
while(true){
window->prompt("Ask 8-Ball a question");
log(Responses[rnd(0,?Responses-1)]);
}</
Non-Random Choice
<
func Compile(String){
Line 2,464:
let N = Compile(Response);
log(Responses[N%(?Responses)]);
}</
=={{header|XPL0}}==
Translation of Kotlin and C.
<
[Answers:= [
"It is certain", "It is decidedly so", "Without a doubt",
Line 2,488:
Text(0, Answer); CrLf(0);
]
]</
{{out}}
Line 2,508:
=={{header|zkl}}==
{{trans|Raku}}
<
"It is certain", "It is decidedly so", "Without a doubt",
"Yes, definitely", "You may rely on it", "As I see it, yes",
Line 2,519:
);
println("Please enter your question or a blank line to quit.");
while(ask("? : ")){ println(answers[(0).random(answers.len())]) }</
{{out}}
<pre>
|