Magic 8-ball

From Rosetta Code
Revision as of 09:08, 24 April 2018 by PureFox (talk | contribs) (Added Kotlin)
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


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

? : 

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)) if index != 0

  see answers[index] + nl

ok </lang> Output:

It is certain