Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby
- class Question
- def initialize(question)
- @question = question
- @answers = Array.new
- @correct = -1
- end
- def addAnswer(text, isCorrect)
- @answers.push(text)
- if isCorrect
- @correct = @answers.length - 1
- end
- end
- def getCorrect()
- return @correct
- end
- def getQuestion()
- return @question
- end
- def getAnswers()
- return @answers
- end
- end
- def emoji(n)
- return [n].pack('U*');
- end
- if ARGV.count != 1
- puts "usage: vwm-tester [filename]"
- abort
- end
- questions = Array.new
- File.open(ARGV[0]) do |f|
- inQuestion = false
- f.each_line do |line|
- if line.slice(0,2) == "//" || line.length < 2
- inQuestion = false
- if questions.last.getCorrect == -1
- questions.pop
- end
- next
- end
- if inQuestion
- if line.slice(-3, 2) == "//"
- questions.last.addAnswer(line.slice(0, line.length - 3), true)
- else
- questions.last.addAnswer(line, false)
- end
- else
- if line.slice(-2,1) == ":" || line.slice(-2,1) == "?"
- inQuestion = true
- questions.push(Question.new(line))
- next
- end
- end
- end
- end
- option = "-"
- answered = 0
- correctly = 0
- random = Random.new
- puts "Parsed #{questions.length} questions."
- puts "Test started, enter \"q\" to quit."
- while option.strip != "q"
- answered += 1
- questionid = random.rand(questions.length - 1)
- puts "\n[Question #{answered}]"
- puts questions[questionid].getQuestion
- answers = questions[questionid].getAnswers
- answers.each_index do |i|
- puts " - #{(65 + i).chr}) #{answers[i]}"
- end
- option = $stdin.readline
- if option.length == 0
- next
- end
- correct = questions[questionid].getCorrect
- print "\n"
- if (option.upcase.strip.ord - 65) == correct || (option.strip.to_f - 1) == correct
- correctly += 1
- print "#{emoji(9989)} CORRECT!"
- puts "\t\t#{correctly}/#{answered}, #{((correctly.to_f/answered) * 100).round(2)} %"
- else
- print "#{emoji(128308)} WRONG!"
- puts "\t\t#{correctly}/#{answered}, #{((correctly.to_f/answered) * 100).round(2)} %"
- puts "#{(65 + correct).chr}) #{answers[correct]}"
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement