class QuizGame

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

Constants

NUM_EMPTY_LINE

NEXT_STR = “”

QUIT_STR

Public Class Methods

new(requirement, problems, picker_file) click to toggle source

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

# File lib/quizgame.rb, line 27
def initialize(requirement, problems, picker_file)
  @requirement = requirement
  @problems    = problems
  @weight_file = picker_file
end

Public Instance Methods

run(norma) click to toggle source

条件を満たすまでクイズを実行。 norma は終了条件の数

# 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

Private Instance Methods

show_great() click to toggle source
# File lib/quizgame.rb, line 146
def show_great
  print "\n"
  great = Array.new
  puts "■■■■■□■■■■■□■■■■■□■■■■■□■■■■■□■□■".color(:red   )
  puts "■□□□□□■□□□■□■□□□□□■□□□■□□□■□□□■□■".color(:yellow)
  puts "■□■■■□■■■■■□■■■■■□■■■■■□□□■□□□■□■".color(:green )
  puts "■□□□■□■□□■□□■□□□□□■□□□■□□□■□□□□□□".color(:cyan  )
  puts "■■■■■□■□□□■□■■■■■□■□□□■□□□■□□□■□■".color(:blue  )
  puts
  puts "All answers are correct!"
  puts
end
show_history(results, io = $stdout) click to toggle source
# File lib/quizgame.rb, line 108
def show_history(results, io = $stdout)
  str = results.map do |result|
    if result == true
      "o"
    elsif result == false
      "x"
    elsif result == nil
      "-"
    else
      raise "Must not happen!"
    end
  end . join("")
  io.puts str
end
show_result(problem, str, io = $stdout) click to toggle source
# File lib/quizgame.rb, line 139
def show_result(problem, str, io = $stdout)
  if (problem.correct?(str) == false)
    io.puts "×: ".color(:red) + problem.emphasize_wrong(str)
  end
  io.puts "○: #{problem.answer}"
end
show_statement(norma, io = $stdout) click to toggle source

ゲーム開始前に情報を提示。

# File lib/quizgame.rb, line 124
def show_statement(norma, io = $stdout)
  io.puts "#{QUIT_STR}[Enter] for immediately quit."
  io.puts
  io.puts "This program has #{@problems.size} problems."
  io.print "Norma: " + norma.to_s.color(:green) + " "
  io.puts "problems."
end
user_input(input = STDIN, output = $stdout) click to toggle source
# File lib/quizgame.rb, line 132
def user_input(input = STDIN, output = $stdout)
  output.print "\n" * NUM_EMPTY_LINE
  output.print "input: "
  return input.gets.chomp!
end