Class: QuizGame

Inherits:
Object
  • Object
show all
Defined in:
lib/quizgame/quizgame.rb

Overview

クイズゲームを管理・実行するクラス。

Defined Under Namespace

Classes: NoQuizError

Constant Summary collapse

QUIT_STR =
"q"
NUM_EMPTY_LINE =

NEXT_STR = “”

8

Instance Method Summary collapse

Constructor Details

#initialize(requirement, problems, picker_file) ⇒ QuizGame

クイズデータを読み込む。



27
28
29
30
31
# File 'lib/quizgame/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
# File 'lib/quizgame/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

  correct_count = 0
  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
    if problem.correct?(input_str)
      correct_count += 1
      picker.lighten(id)
    else
      picker.weigh(id)
    end

    puts show_result(problem, input_str)
    puts problem.show_supplement
    #pp correct_count
    #pp problem_count
    #pp @norma
    printf( "[%d correct / %d problems / %d norma]\n",
      correct_count, problem_count, norma)
  end

  File.open(@weight_file, "w"){|io| picker.dump(io) }

  puts "="*60
  sleep(0.35) #Visual effect. A little stop before showing results.
  if (correct_count == norma)
    show_great
  end

  printf("result: [%d correct / %d problems]",
      correct_count, problem_count)

  begin
    printf("  %2d\%\n", (correct_count.to_f/ problem_count*100).to_i)
  rescue FloatDomainError
    puts "  No problem is answered."
  end

  print "Time: ", Time.now - start_time, "\n"
end