Advertisement
Guest User

Untitled

a guest
May 30th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. #!/usr/bin/ruby
  2.  
  3. class Question
  4. def initialize(question)
  5. @question = question
  6. @answers = Array.new
  7. @correct = -1
  8. end
  9.  
  10. def addAnswer(text, isCorrect)
  11. @answers.push(text)
  12. if isCorrect
  13. @correct = @answers.length - 1
  14. end
  15. end
  16.  
  17. def getCorrect()
  18. return @correct
  19. end
  20.  
  21. def getQuestion()
  22. return @question
  23. end
  24.  
  25. def getAnswers()
  26. return @answers
  27. end
  28. end
  29.  
  30. def emoji(n)
  31. return [n].pack('U*');
  32. end
  33.  
  34. if ARGV.count != 1
  35. puts "usage: vwm-tester [filename]"
  36. abort
  37. end
  38.  
  39. questions = Array.new
  40.  
  41. File.open(ARGV[0]) do |f|
  42. inQuestion = false
  43. f.each_line do |line|
  44.  
  45. if line.slice(0,2) == "//" || line.length < 2
  46. inQuestion = false
  47. if questions.last.getCorrect == -1
  48. questions.pop
  49. end
  50. next
  51. end
  52.  
  53. if inQuestion
  54. if line.slice(-3, 2) == "//"
  55. questions.last.addAnswer(line.slice(0, line.length - 3), true)
  56. else
  57. questions.last.addAnswer(line, false)
  58. end
  59. else
  60. if line.slice(-2,1) == ":" || line.slice(-2,1) == "?"
  61. inQuestion = true
  62. questions.push(Question.new(line))
  63. next
  64. end
  65. end
  66.  
  67. end
  68. end
  69.  
  70. option = "-"
  71. answered = 0
  72. correctly = 0
  73. random = Random.new
  74.  
  75. puts "Parsed #{questions.length} questions."
  76. puts "Test started, enter \"q\" to quit."
  77.  
  78. while option.strip != "q"
  79. answered += 1
  80. questionid = random.rand(questions.length - 1)
  81.  
  82. puts "\n[Question #{answered}]"
  83. puts questions[questionid].getQuestion
  84.  
  85. answers = questions[questionid].getAnswers
  86. answers.each_index do |i|
  87. puts " - #{(65 + i).chr}) #{answers[i]}"
  88. end
  89.  
  90. option = $stdin.readline
  91.  
  92. if option.length == 0
  93. next
  94. end
  95.  
  96. correct = questions[questionid].getCorrect
  97. print "\n"
  98.  
  99. if (option.upcase.strip.ord - 65) == correct || (option.strip.to_f - 1) == correct
  100. correctly += 1
  101. print "#{emoji(9989)} CORRECT!"
  102. puts "\t\t#{correctly}/#{answered}, #{((correctly.to_f/answered) * 100).round(2)} %"
  103. else
  104. print "#{emoji(128308)} WRONG!"
  105. puts "\t\t#{correctly}/#{answered}, #{((correctly.to_f/answered) * 100).round(2)} %"
  106. puts "#{(65 + correct).chr}) #{answers[correct]}"
  107. end
  108. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement