Magic 8-ball: Difference between revisions
Line 149: | Line 149: | ||
Yes |
Yes |
||
? : Are cats secretly alien spies? |
? : Are cats secretly alien spies? |
||
Signs point to yes |
Signs point to yes |
||
? : Am I a genius? |
? : Am I a genius? |
||
Line 154: | Line 155: | ||
? : |
? : |
||
</pre> |
</pre> |
||
=={{header|Forth}}== |
|||
====ANS/ISO FORTH, GNU FORTH==== |
|||
This example defines CASE: ;CASE that create a vector table. |
|||
VECTORS compiles a number execution vectors into memory from the stack. |
|||
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. |
|||
<LANG>\ magic eight ball Rosetta Code |
|||
INCLUDE RANDOM.FS |
|||
DECIMAL |
|||
: CASE: ( -- -7) CREATE ; |
|||
: ;CASE ( n -- ) DOES> SWAP CELLS + @ EXECUTE ; |
|||
: VECTORS 0 DO , LOOP ; |
|||
:NONAME ." It is certain" ; |
|||
:NONAME ." It is decidedly so" ; |
|||
:NONAME ." Without a doubt" ; |
|||
:NONAME ." Yes, definitely" ; |
|||
:NONAME ." You may rely on it" ; |
|||
:NONAME ." As I see it, ye." ; |
|||
:NONAME ." Most likely" ; |
|||
:NONAME ." Outlook good" ; |
|||
:NONAME ." Signs point to ye." ; |
|||
:NONAME ." Yes." ; |
|||
:NONAME ." Reply hazy, try again" ; |
|||
:NONAME ." Ask again later" ; |
|||
:NONAME ." Better not tell you now" ; |
|||
:NONAME ." Cannot predict now" ; |
|||
:NONAME ." Concentrate and ask again" ; |
|||
:NONAME ." Don't bet on it" ; |
|||
:NONAME ." My reply is no" ; |
|||
:NONAME ." My sources say no" ; |
|||
:NONAME ." Outlook not so good" ; |
|||
:NONAME ." Very doubtful" ; |
|||
CASE: MAGIC8BALL 20 VECTORS ;CASE |
|||
: GO |
|||
CR ." Please enter your question or a blank line to quit." |
|||
BEGIN CR ." ? :" PAD 80 ACCEPT 0> |
|||
WHILE CR 19 RANDOM MAGIC8BALL CR |
|||
REPEAT ;</LANG> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
Revision as of 19:47, 1 December 2018
Create Magic 8-Ball. See details: Magic 8-Ball
AWK
<lang AWK>
- syntax: GAWK -f MAGIC_8-BALL.AWK
BEGIN {
- Ten answers are affirmative, five are non-committal, and five are negative.
arr[++i] = "It is certain" arr[++i] = "It is decidedly so" arr[++i] = "Without a doubt" arr[++i] = "Yes, definitely" arr[++i] = "You may rely on it" arr[++i] = "As I see it, yes" arr[++i] = "Most likely" arr[++i] = "Outlook good" arr[++i] = "Signs point to yes" arr[++i] = "Yes" arr[++i] = "Reply hazy, try again" arr[++i] = "Ask again later" arr[++i] = "Better not tell you now" arr[++i] = "Cannot predict now" arr[++i] = "Concentrate and ask again" arr[++i] = "Don't bet on it" arr[++i] = "My reply is no" arr[++i] = "My sources say no" arr[++i] = "Outlook not so good" arr[++i] = "Very doubtful" srand() printf("Please enter your question or a blank line to quit.\n") while (1) { printf("\n? ") getline ans if (ans ~ /^ *$/) { break } printf("%s\n",arr[int(rand()*i)+1]) } exit(0)
} </lang>
- Output:
Please enter your question or a blank line to quit. ? will you still love me tomorrow Yes, definitely ?
C
<lang c>#include <stdio.h>
- include <stdlib.h>
- include <time.h>
int main() {
char *question = NULL; size_t len = 0; ssize_t read; const char* answers[20] = { "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" }; srand(time(NULL)); printf("Please enter your question or a blank line to quit.\n"); while (1) { printf("\n? : "); read = getline(&question, &len, stdin); if (read < 2) break; printf("\n%s\n", answers[rand() % 20]); } if (question) free(question); return 0;
}</lang>
- Output:
Sample session :
Please enter your question or a blank line to quit. ? : Will May win the next UK election? It is certain ? : Do flying saucers really exist? Signs point to yes ? : Will humans ever colonize Mars? It is decidedly so ? :
Clojure
<lang clojure> (def 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" "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"])
(do
(println "Ask a question. ") (read-line) (println (rand-nth responses)))
</lang>
Factor
<lang factor>USING: io kernel random sequences ; IN: rosetta-code.magic-8-ball
CONSTANT: phrases {
"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"
}
"Please enter your question or a blank line to quit." print
[ "? : " write flush readln empty? [ f ] [ phrases random print t ] if ] loop</lang>
- Output:
Please enter your question or a blank line to quit. ? : Are there more than 10^57 grains of sand in the universe? Yes ? : Are cats secretly alien spies? Signs point to yes ? : Am I a genius? Outlook not so good ? :
Forth
ANS/ISO FORTH, GNU FORTH
This example defines CASE: ;CASE that create a vector table. VECTORS compiles a number execution vectors into memory from the stack. 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. <LANG>\ magic eight ball Rosetta Code INCLUDE RANDOM.FS DECIMAL
- CASE: ( -- -7) CREATE ;
- ;CASE ( n -- ) DOES> SWAP CELLS + @ EXECUTE ;
- VECTORS 0 DO , LOOP ;
- NONAME ." It is certain" ;
- NONAME ." It is decidedly so" ;
- NONAME ." Without a doubt" ;
- NONAME ." Yes, definitely" ;
- NONAME ." You may rely on it" ;
- NONAME ." As I see it, ye." ;
- NONAME ." Most likely" ;
- NONAME ." Outlook good" ;
- NONAME ." Signs point to ye." ;
- NONAME ." Yes." ;
- NONAME ." Reply hazy, try again" ;
- NONAME ." Ask again later" ;
- NONAME ." Better not tell you now" ;
- NONAME ." Cannot predict now" ;
- NONAME ." Concentrate and ask again" ;
- NONAME ." Don't bet on it" ;
- NONAME ." My reply is no" ;
- NONAME ." My sources say no" ;
- NONAME ." Outlook not so good" ;
- NONAME ." Very doubtful" ;
CASE: MAGIC8BALL 20 VECTORS ;CASE
- GO
CR ." Please enter your question or a blank line to quit." BEGIN CR ." ? :" PAD 80 ACCEPT 0> WHILE CR 19 RANDOM MAGIC8BALL CR REPEAT ;</LANG>
Go
<lang go>package main
import ( "bufio" "bytes" "fmt" "log" "math/rand" "os" "time" )
func main() { rand.Seed(time.Now().UnixNano()) answers := [...]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", "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", } const prompt = "\n? : " fmt.Print("Please enter your question or a blank line to quit.\n" + prompt) sc := bufio.NewScanner(os.Stdin) for sc.Scan() { question := sc.Bytes() question = bytes.TrimSpace(question) if len(question) == 0 { break } answer := answers[rand.Intn(len(answers))] fmt.Printf("\n%s\n"+prompt, answer) } if err := sc.Err(); err != nil { log.Fatal(err) } }</lang>
- Output:
Please enter your question or a blank line to quit. ? : Will Trump win the next US election? As I see it, yes ? : Is it true that we live in a multiverse? Yes ? : Will the singularity occur before 2030? My reply is no ? :
JavaScript
<lang JavaScript> //console var 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"])
console.log("ASK ANY QUESTION TO THE MAGIC 8-BALL AND YOU SHALL RECEIVE AN ANSWER!")
for(;;){
var answer = prompt("question:") console.log(answer)
console.log(answers[Math.floor(Math.random()*answers.length)]); } </lang>
- Output:
"ASK ANY QUESTION TO THE MAGIC 8-BALL AND YOU SHALL RECEIVE AN ANSWER!" "Is this code going to work?" "Most likely"
Kotlin
<lang scala>// Version 1.2.40
import java.util.Random
fun main(args: Array<String>) {
val answers = listOf( "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" ) val rand = Random() println("Please enter your question or a blank line to quit.") while (true) { print("\n? : ") val question = readLine()!! if (question.trim() == "") return val answer = answers[rand.nextInt(20)] println("\n$answer") }
}</lang>
- Output:
Sample session :
Please enter your question or a blank line to quit. ? : Will Trump win the next US election? Outlook not so good ? : Is it true that we live in a multiverse? It is certain ? : Will the singularity occur before 2030? Reply hazy, try again ? : Will the singularity occur before 2030? Signs point to yes ? :
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") Print "Please enter your question or a blank line to quit." { Line Input A$ Print A$=trim$(A$) If A$="" Then Exit Print Array$(answers, Random(0, 19)) Loop }
} Magic.8.Ball </lang>
ooRexx
<lang ooRexx> /* REXX */ a=.array~of("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")
Do Forever
Say 'your question:' Parse Pull q If q= Then Leave Say a[random(1,20)] Say End</lang>
- Output:
your question: will it rain tonight Concentrate and ask again your question: will it rain tonight You may rely on it your question:
Perl
<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', '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');
while () {
print 'Enter your question:'; last unless <> =~ /\w/; print @a[int rand @a], "\n";
}</lang>
Perl 6
<lang perl6>put 'Please enter your question or a blank line to quit.';
["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"].roll.put while prompt('? : ').chars;</lang>
Output very similar to C, Kotlin and zkl examples.
PHP
CLI
<lang php><?php
$fortunes = array( "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" );
/*
* Prompt the user at the CLI for the command */
function cli_prompt( $prompt='> ', $default=false ) {
// keep asking until a non-empty response is given do { // display the prompt echo $prompt;
// read input and remove CRLF $cmd = chop( fgets( STDIN ) );
} while ( empty( $default ) and empty( $cmd ) );
return $cmd ?: $default;
}
$question = cli_prompt( 'What is your question? ' );
echo 'Q: ', $question, PHP_EOL;
echo 'A: ', $fortunes[ array_rand( $fortunes ) ], PHP_EOL; </lang>
- Output:
$ php 8ball.php What is your question? Are we together forever and never to part? Q: Are we together forever and never to part? A: Without a doubt $ php 8ball.php What is your question? Are we together forever we two? Q: Are we together forever we two? A: Don't bet on it $ php 8ball.php What is your question? Will I move heaven and earth to be together forever with you? Q: Will I move heaven and earth to be together forever with you? A: It is certain
Python
<lang python>import random
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')
q_and_a = {}
while True:
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: q_and_a[question] = answer print(answer)</lang>
- Output:
Ask your question:Is python the best language ever? Better not tell you now Ask your question:You are really not helping your case here. Very doubtful Ask your question:So, no? Signs point to yes Ask your question:So, no? Your question has already been answered Ask your question:
Racket
<lang>(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" "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"))
(define ((answer-picker answers)) (sequence-ref answers (random (sequence-length answers))))
(define magic-eightball (answer-picker eight-ball-responses))
(module+ main
(let loop () (display "What do you want to know\n?") (read-line) (displayln (magic-eightball)) (loop)))</lang>
- Output:
We'll see if it's right.
REXX
version 1
<lang rexx> /* REXX */ Call mk_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",, "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"
Do Forever
Say 'your question:' Parse Pull q If q= Then Leave z=random(1,a.0) Say a.z Say End
Exit mk_a: a.0=arg() Do i=1 To a.0
a.i=arg(i) End
Return </lang>
- Output:
your question: will it rain tonight My reply is no your question: will it rain tonight Signs point to yes your question:
version 2
This REXX version is modeled after the Ring entry.
The method used is to translate all blanks to psuedo-blanks, then extract and show a random phrase (after translating the pseudo-blanks back to blanks).
Also, this REXX version appends a period to the phrase as per the (linked) documentation. <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",
"÷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"
@=translate(translate($, '┼', " "),, '÷') /*trans: blanks─►psuedos, seps─►blanks.*/ say space(translate(word(@,random(1,20)),,"┼")). /*stick a fork in it, we're all done. */</lang>
- output when invoking the REXX program:
Reply hazy, try again.
Ring
<lang ring>
- Project : 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"]
index = random(len(answers)-1)+1 see answers[index] + nl </lang> Output:
It is certain
zkl
<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", "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"
); println("Please enter your question or a blank line to quit."); while(ask("? : ")){ println(answers[(0).random(answers.len())]) }</lang>
- Output:
lease enter your question or a blank line to quit. ? : will it rain today It is certain ? : where is Turkey Yes ? : what is the price of milk It is decidedly so ? : who is Elton John Most likely ? :