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

class CalculationGame
  def initialize
    @@choices = {"メニュー" => ["プレイ"]}
  end

  def question(choices,mold = "moji")
    kaisu = 0
    select = -1
    if mold == "hyozi"
      while kaisu < choices.length
        printf("%s ",choices[kaisu])
        print("\e[m")
        kaisu += 1
      end
    else
      while kaisu < choices.length
        printf("%s(%d) ",choices[kaisu],kaisu)
        print("\e[m")
        kaisu += 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 choices[select]
    elsif mold == "suji"
      @select = select
      return select
    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 game
    while true
      question(@@choices["メニュー"])
      if @select == "プレイ"
        play1
      end
    end
  end

  def play1
    turn = 0
    z = 0
    t = Time.new
    while true
      a = rand(1..2+turn)
      b = rand(1..2+turn)
      x = a+b
      point = 0
      while true
        printf("%d+%d=",a,b)
        kaitou = gets.to_f
        if kaitou == x && t < Time.new+z
          puts("正解!")
          z+=8
          turn += 1
          point += (1+(turn/10.floor))
          break
        elsif t >= Time.new+60+z
          return
        else
          puts("不正解!")
          z-=(5+(turn/10).floor(1))
        end
        p t-Time
      end
    end
  end
      
end

x = CalculationGame.new
x.game