Class: QuizGame
- Inherits:
-
Object
- Object
- QuizGame
- Defined in:
- lib/quizgame.rb,
lib/quizgame.rb
Overview
クイズゲームを管理・実行するクラス。
Defined Under Namespace
Classes: NoQuizError, Problem
Constant Summary collapse
- QUIT_STR =
"q"
- NUM_EMPTY_LINE =
NEXT_STR = “”
1
Instance Method Summary collapse
-
#initialize(requirement, problems, picker_file) ⇒ QuizGame
constructor
クイズデータを読み込む。.
-
#run(norma) ⇒ Object
条件を満たすまでクイズを実行。 norma は終了条件の数.
Constructor Details
#initialize(requirement, problems, picker_file) ⇒ QuizGame
クイズデータを読み込む。
27 28 29 30 31 |
# File 'lib/quizgame.rb', line 27 def initialize(requirement, problems, picker_file) @requirement = requirement @problems = problems @weight_file = picker_file end |
Instance Method Details
#run(norma) ⇒ Object
条件を満たすまでクイズを実行。 norma は終了条件の数
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/quizgame.rb', line 35 def run(norma) begin picker = WeightedPicker.load_file @weight_file picker.merge @problems.keys rescue WeightedPicker::InvalidWeightError puts "Error!" puts "#{@weight_file} contains float weight. Use integers as weights." exit rescue Errno::ENOENT #puts "TEST" picker = WeightedPicker.new({}) picker.merge @problems.keys end history = Array.new(norma) problem_count = 0 show_statement(norma) puts "Requirement: ", @requirement start_time = Time.new while (problem_count < norma ) id = picker.pick problem = @problems[id] puts "-"*60 print "[Q.#{problem_count+1}, #{id}] " problem.exhibit_question input_str = user_input (puts "-- Interrupted --"; break) if (input_str == QUIT_STR) #(puts "-- Next --"; next) if (input_str == NEXT_STR) problem_count += 1 result = problem.correct?(input_str) history[problem_count -1] = result if result picker.lighten(id) else picker.weigh(id) end puts show_result(problem, input_str) puts problem.show_supplement print "History: " show_history(history) end File.open(@weight_file, "w"){|io| picker.dump(io) } puts "="*60 sleep(0.35) #Visual effect. A little stop before showing results. if ((history.count(true) == norma) && norma > 0) show_great end printf("result: [%d correct / %d problems]", history.count(true), problem_count) begin printf(" %2d\%\n", ( (history.count(true).to_f)/ problem_count*100).to_i) rescue FloatDomainError puts " No problem is answered." end print "Time: ", Time.now - start_time, "\n" puts picker.dump_histgram($stdout) end |