Newer
Older
Ruby / quiz.rb
#!usr/bin/env ruby
# -*-coding: utf-8 -*-

class QUIZ
  
  def initialize
    @@problem = CSV.read("problem.csv")
    for i in @@problem
      i.gsub!("'=='","==")
    end
    for i in @@problem
      i.gsub!("'true'","true")
    end
    for i in @@problem
      i.gsub!("'false'","false")
    end
    x = 0
    while x < @@problem.length
      @@problem[x].compact!            #nilってるのを消してる
      x += 1
    end
    @@story = CSV.read("story.csv",:headers => true)
    @@choices = {"スタート" => ["スタート","あとがき","スタッフロール","プログラムを終了させる"]}
  end
  
  def question(choices,mold = "moji")
    x = 0
    @select = -1
    if mold == "hyozi"
      while x < choices.length
        printf("%s ",choices[x])
        print("\e[m")
        x += 1
      end
    else
      while x < choices.length
        printf("%s(%d) ",choices[x],x)
        print("\e[m")
        x += 1
      end
    end
    print("\n")
    while @select < 0 || @select >= choices.length   #回答の仕分け
      print("選択:")
      @select = gets.chomp
      if @select == "" || check(@select) == false
        redo
      end
      @select = @select.to_i
    end
    if mold == "moji"
      @select = choices[@select]
      return @select
    elsif mold == "suji"
      return @select
    else
      puts("Error")
    end
  end

  def check(h)
    number = ["1","2","3","4","5","6","6","7","8","9","0"]
    x = h.split("")
    n = 0
    for i in x
      if  false == number.include?(x[n])
        return false
      end
      n += 1
    end
    return true
  end

  def talk(story,mold = "置き換え")
    print("\n")
    if not story == "なし"
      if mold == "置き換え"
        for i in @@story[story]
          puts(i.gsub("_me_",@status["name"])
                 .gsub("_jibun_",@status["一人称"])
                 .gsub("_seibetu_",@status["性別"])
              )
          print("\e[m")
          gets
        end
      elsif mold == "読み上げ"
        for i in @@story[story]
          puts i
          print("\e[m")
          gets
        end
      end
    end
  end
  
  def maine
    while true
      puts("Rubyクイズ!!")
      puts()
      question(@@choices["スタート"])
      if @select == "あとがき"

      elsif @select == "スタッフロール"
        talk(,"読み上げ")
      elsif @select == "プログラムを終了させる"
        break
      end
    end
  end
  
end