Jump to content

Magic 8-ball: Difference between revisions

m
syntax highlighting fixup automation
(Applesoft BASIC)
m (syntax highlighting fixup automation)
Line 14:
{{trans|Python}}
 
<langsyntaxhighlight 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’,
Line 34:
E
qs.add(question)
print(answer)</langsyntaxhighlight>
 
=={{header|8080 Assembly}}==
 
 
<langsyntaxhighlight lang="8080asm">bdos: equ 5 ; CP/M calls
puts: equ 9
gets: equ 10
Line 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.
<langsyntaxhighlight lang="asm">
.model small
.stack 1024
Line 204:
xchg al,ah
;now the word is loaded into ax, big-endian.
ret</langsyntaxhighlight>
 
{{out}}
Line 216:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC Main()
DEFINE PTR="CARD"
DEFINE COUNT="20"
Line 254:
PutE()
OD
RETURN</langsyntaxhighlight>
{{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">
<lang Ada>
 
with Ada.Text_IO; use Ada.Text_IO;
Line 332:
end Main;
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 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 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}}
<langsyntaxhighlight 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>
</lang>
=={{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 391:
input "Ask a question: "
print sample answers
]</langsyntaxhighlight>
 
{{out}}
Line 401:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">answers := [
(join
"It is certain", "It is decidedly so", "Without a doubt","Yes, definitely",
Line 417:
MsgBox % answers[rnd]
gosub, Ask
return</langsyntaxhighlight>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f MAGIC_8-BALL.AWK
BEGIN {
Line 456:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 471:
==={{header|BASIC256}}===
{{trans|QBasic}}
<langsyntaxhighlight BASIC256lang="basic256">dim answer$(20)
answer$[0] = "It is certain."
answer$[1] = "It is decidedly so."
Line 500:
print
end while
end</langsyntaxhighlight>
 
==={{header|QBasic}}===
{{works with|QBasic}}
{{works with|QuickBasic|4.5}}
<langsyntaxhighlight QBasiclang="qbasic">DIM answer$(19)
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."</langsyntaxhighlight>
 
==={{header|True BASIC}}===
{{trans|QBasic}}
<langsyntaxhighlight lang="qbasic">DIM answer$(20)
FOR i = 1 to ubound(answer$)
READ answer$(i)
Line 554:
PRINT
LOOP
END</langsyntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|QBasic}}
<langsyntaxhighlight lang="yabasic">dim answer$(19)
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."</langsyntaxhighlight>
 
 
=={{header|C}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Line 611:
if (question) free(question);
return 0;
}</langsyntaxhighlight>
 
{{output}}
Line 634:
 
=={{header|C++}}==
<langsyntaxhighlight Cpplang="cpp">#include <array>
#include <cstdlib>
#include <ctime>
Line 677:
std::cout << answers[std::rand() % answers.size()] << '\n';
}
}</langsyntaxhighlight>
 
=={{header|C#}}==
<langsyntaxhighlight lang="csharp">
using System;
 
Line 731:
 
 
</syntaxhighlight>
</lang>
 
=={{header|CFEngine}}==
 
<langsyntaxhighlight lang="cfengine3">
#!/var/cfengine/bin/cf-agent --no-lock
bundle agent __main__
Line 783:
depends_on => { "consider" };
}
</syntaxhighlight>
</lang>
 
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 803:
(read-line)
(println (rand-nth responses)))
</syntaxhighlight>
</lang>
 
=={{header|CMake}}==
<langsyntaxhighlight CMakelang="cmake">CMAKE_MINIMUM_REQUIRED(VERSION 3.6)
 
PROJECT(EightBall)
Line 844:
 
ADD_CUSTOM_TARGET(${PROJECT_NAME} ALL)
</syntaxhighlight>
</lang>
No question specified:
<pre>$ cmake -H. -Bbuild
Line 874:
-- Generating done</pre>
=={{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 897:
1080 data "My sources say no.","Outlook not so good."
1090 data "Very doubtful."
</syntaxhighlight>
</lang>
 
=={{header|D}}==
<langsyntaxhighlight Dlang="d">import std.random, std.stdio, std.string;
 
const string[] responses = ["It is certain", "It is decidedly so",
Line 935:
}
}
</syntaxhighlight>
</lang>
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">
<lang Delphi>
program Magic_8_ball;
 
Line 998:
ClearScreen;
end;
end.</langsyntaxhighlight>
{{out}}
<pre>Magic 8 Ball! Ask question and hit ENTER key for the answer!</pre>
Line 1,008:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: io kernel random sequences ;
IN: rosetta-code.magic-8-ball
 
Line 1,025:
 
[ "? : " write flush readln empty? [ f ]
[ phrases random print t ] if ] loop</langsyntaxhighlight>
{{out}}
<pre>
Line 1,039:
 
=={{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 1,054:
print answer(int(rnd*20))
print
loop</langsyntaxhighlight>
 
=={{header|Forth}}==
Line 1,116:
 
=={{header|Fortran}}==
<langsyntaxhighlight FORTRANlang="fortran">PROGRAM EIGHT_BALL
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</langsyntaxhighlight>
Output:
<pre> Welcome to 8 Ball! Ask a question to find the answers
Line 1,167:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,206:
log.Fatal(err)
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,227:
</pre>
=={{header|GW-BASIC}}==
<langsyntaxhighlight 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."
Line 1,244:
160 INPUT "What would you like to know? ", Q$
170 PRINT M8BALL$(INT(RND*20))
180 END</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import System.Random (getStdRandom, randomR)
import Control.Monad (forever)
 
Line 1,279:
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 1,295:
end.
)
</syntaxhighlight>
</lang>
 
<pre>
Line 1,309:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.util.Random;
import java.util.Scanner;
Line 1,337:
}
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,354:
 
=={{header|JavaScript}}==
<syntaxhighlight lang="javascript">
<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>
</lang>
 
{{output}}
Line 1,384:
 
=={{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,398:
println(rand(responses))
end
</langsyntaxhighlight>{{out}}
<pre>
Ask a question (blank to exit):
Line 1,409:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// Version 1.2.40
 
import java.util.Random
Line 1,433:
println("\n$answer")
}
}</langsyntaxhighlight>
 
{{output}}
Line 1,460:
 
=={{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,478:
print "Answer: ";c$(d)
goto [loop]
</syntaxhighlight>
</lang>
{{output}}
Sample session :
Line 1,491:
=={{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,516:
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,530:
question = io.read()
print("A: "..answers[math.random(#answers)])
end</langsyntaxhighlight>
{{out}}
<pre>Q: Are you truly a magic 8-ball?
Line 1,538:
 
=={{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,552:
}
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,565:
q = Input["Please enter your question"];
SeedRandom[Hash[q]];
Print[RandomChoice[answers]]</langsyntaxhighlight>
{{out}}
<pre>[Are you awesome?]Yes - definitely.</pre>
Line 1,571:
=={{header|min}}==
{{works with|min|0.19.6}}
<langsyntaxhighlight lang="min">randomize ; Seed the RNG with current timestamp.
 
(
Line 1,586:
(phrases dup size pred random get puts!) :answer
 
(true) ("Ask a question" ask answer) while</langsyntaxhighlight>
{{out}}
<pre>
Line 1,597:
 
=={{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,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]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,616:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import random, strutils
 
const Answers = ["It is certain", "It is decidedly so", "Without a doubt",
Line 1,641:
echo Answers[rand(19)], ".\n"
except EOFError:
exit()</langsyntaxhighlight>
 
{{out}}
Line 1,654:
 
=={{header|ooRexx}}==
<syntaxhighlight lang="oorexx">
<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</langsyntaxhighlight>
{{out}}
<pre>your question:
Line 1,683:
 
=={{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,694:
last unless <> =~ /\w/;
print @a[int rand @a], "\n";
}</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 1,700:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/magic8ball.htm here].
<!--<langsyntaxhighlight Phixlang="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>
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>
<!--</langsyntaxhighlight>-->
 
=={{header|PHP}}==
===CLI===
<langsyntaxhighlight lang="php"><?php
 
$fortunes = array(
Line 1,803:
 
echo 'A: ', $fortunes[ array_rand( $fortunes ) ], PHP_EOL;
</syntaxhighlight>
</lang>
 
{{Out}}
Line 1,824:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Define.s Magic8="● It is certain."+#LF$+
"● 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</langsyntaxhighlight>
{{out}}
<pre>MAGIC8: What would you like To know? Do you want me to take over the world?
Line 1,863:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import random
 
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)</langsyntaxhighlight>
 
{{output}}
Line 1,899:
 
=={{header|Quackery}}==
<langsyntaxhighlight Quackerylang="quackery">[ 20 random
[ 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 ( --> )</langsyntaxhighlight>
 
'''Testing in Quackery shell:'''
Line 1,938:
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">eight_ball <- function()
{
responses <- c("It is certain", "It is decidedly so", "Without a doubt",
Line 1,969:
{
eight_ball()
}</langsyntaxhighlight>
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)))</langsyntaxhighlight>
{{out}}
We'll see if it's right.
Line 2,006:
{{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 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;</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 2,043:
End
Return
</syntaxhighlight>
</lang>
{{out}}
<pre>your question:
Line 2,063:
 
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 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)), ,"┼")).</langsyntaxhighlight>
{{out|possible output|text=&nbsp; when invoking the REXX program:}}
<pre>
Line 2,076:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Magic 8-Ball
 
Line 2,089:
index = random(len(answers)-1)+1
see answers[index] + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,095:
</pre>
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">#!/usr/bin/ruby
 
class EightBall
Line 2,134:
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 2,143:
=={{header|Rust}}==
{{libheader|rand}}
<langsyntaxhighlight lang="rust">extern crate rand;
 
use rand::prelude::*;
Line 2,185:
input_line.clear();
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,199:
 
=={{header|Sather}}==
<langsyntaxhighlight lang="sather">class MAIN is
const answers: ARRAY{STR} :=
|
Line 2,218:
end;
end;
end;</langsyntaxhighlight>
 
=={{Header|Scala}}==
<langsyntaxhighlight Scalalang="scala">import scala.util.Random
 
object Magic8Ball extends App {
Line 2,249:
}
}
}</langsyntaxhighlight>
 
=={{Header|Tcl}}==
<langsyntaxhighlight lang="tcl">namespace path {::tcl::mathop ::tcl::mathfunc}
 
set answers {
Line 2,285:
puts "⑧ says “$answer”"
puts -nonewline "Question: "; flush stdout
}</langsyntaxhighlight>
{{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."</langsyntaxhighlight>
 
 
=={{header|uBasic/4tH}}==
{{works with|version 3.64.0}}
<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")
Line 2,361:
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
RESPONSES=("It is certain" "It is decidedly so" "Without a doubt"
Line 2,387:
 
printf "Response: ${RESPONSES[response_index]}\n\n"
done</langsyntaxhighlight>
Output:
<pre>Welcome to 8 ball! Enter your questions using the prompt below to
Line 2,397:
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="ecmascript">import "random" for Random
import "io" for Stdin, Stdout
 
Line 2,419:
var answer = answers[rand.int(20)]
System.print("\n%(answer)")
}</langsyntaxhighlight>
 
{{out}}
Line 2,443:
=={{header|XBS}}==
Random Choice
<langsyntaxhighlight XBSlang="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)]);
}</langsyntaxhighlight>
Non-Random Choice
<langsyntaxhighlight XBSlang="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){
Line 2,464:
let N = Compile(Response);
log(Responses[N%(?Responses)]);
}</langsyntaxhighlight>
 
=={{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,488:
Text(0, Answer); CrLf(0);
]
]</langsyntaxhighlight>
 
{{out}}
Line 2,508:
=={{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,519:
);
println("Please enter your question or a blank line to quit.");
while(ask("? : ")){ println(answers[(0).random(answers.len())]) }</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.