Magic 8-ball: Difference between revisions

From Rosetta Code
Content added Content deleted
(add ooRexx)
Line 173: Line 173:


? :
? :
</pre>

=={{header|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>
{{out}}
<pre>your question:
will it rain tonight
Concentrate and ask again

your question:
will it rain tonight
You may rely on it
</pre>
</pre>



Revision as of 18:30, 20 May 2018

Magic 8-ball is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.


Create Magic 8-Ball. See details: Magic 8-Ball


AWK

<lang AWK>

  1. syntax: GAWK -f MAGIC_8-BALL.AWK

BEGIN {

  1. 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

Translation of: Kotlin

<lang c>#include <stdio.h>

  1. include <stdlib.h>
  2. 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>

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

? : 

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

Perl 6

Works with: Rakudo version 2018.03

<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.

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.

Ring

<lang ring>

  1. Project : Magic 8-Ball
  2. Date  : 2018/04/24
  3. Author : Gal Zsolt (~ CalmoSoft ~)
  4. Email  : <calmosoft@gmail.com>

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

Translation of: Perl 6

<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
? :