Created
November 15, 2010 22:16
-
-
Save bjartek/701059 to your computer and use it in GitHub Desktop.
CLI ui for quiz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package scalaexamples.quiz.console | |
import scalaexamples.quiz.{Answer, Question, Quiz} | |
import annotation.tailrec | |
class ConsoleGame(quiz: Quiz) { | |
println("Welcome to our faboulus quiz called '" + quiz.title + "'") | |
val answers = quiz.questions.map { | |
question => | |
println("Question: " + question.text); | |
question.answers.zipWithIndex.foreach {case (answer, index) => println((index + 1) + ". " + answer.text)} | |
println("What is your answer?:") | |
askQuestion(question) | |
} | |
val correct = answers.filter(_.correct == true).size | |
println("Du svarte rett på " + correct + " av " + quiz.questions.size + " spørsmål") | |
@tailrec | |
private def askQuestion(question: Question): Answer = { | |
def parseInput = try readLine.toInt - 1 catch {case _ => -1} | |
val index = parseInput | |
//lazy val input = try readLine.toInt - 1 catch {case _ => -1} | |
if (!question.answers.isDefinedAt(index)) { | |
println("Not valid input, try again!:") | |
askQuestion(question) | |
} else { | |
question.answers(index) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment