Skip to content

Instantly share code, notes, and snippets.

@bjartek
Created November 15, 2010 22:16
Show Gist options
  • Save bjartek/701059 to your computer and use it in GitHub Desktop.
Save bjartek/701059 to your computer and use it in GitHub Desktop.
CLI ui for quiz
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