Created
July 26, 2008 07:57
-
-
Save iseebi/2620 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MODE_EASY = 1 | |
MODE_DIFFICULT = 2 | |
OP_ADD = 0 | |
OP_SUB = 1 | |
OP_MULTI = 2 | |
OP_DIV = 3 | |
QUESTIONS = 100 | |
class Question | |
attr_reader :left, :right, :operator | |
def self.create(mode) | |
if mode == MODE_EASY | |
Question.new(1, 3) | |
elsif mode == MODE_DIFFICULT | |
Question.new(2, 4) | |
else | |
raise ArgumentError | |
end | |
end | |
def initialize(keta, operator) | |
@left = rand(10 ** keta) | |
@right = rand(10 ** keta) | |
if (@right != 0) && (@left % @right == 0) | |
@operator = rand(operator) | |
else | |
@operator = rand(3) | |
end | |
end | |
def answer | |
case @operator | |
when OP_ADD | |
@left + @right | |
when OP_SUB | |
@left - @right | |
when OP_MULTI | |
@left * @right | |
when OP_DIV | |
@left / @right | |
end | |
end | |
def to_s | |
case @operator | |
when OP_ADD | |
sprintf('%2d + %2d = ?', @left, @right) | |
when OP_SUB | |
sprintf('%2d - %2d = ?', @left, @right) | |
when OP_MULTI | |
sprintf('%2d * %2d = ?', @left, @right) | |
when OP_DIV | |
sprintf('%2d / %2d = ?', @left, @right) | |
end | |
end | |
end | |
puts sprintf('計算 %d 開始', QUESTIONS) | |
print '難易度を選択してください。 1:かんたん 2:むずかしい > ' | |
level = gets.chomp.to_i | |
questions = Array.new | |
if (level == MODE_EASY) || (level == MODE_DIFFICULT) | |
QUESTIONS.times do | |
questions << Question.create(level) | |
end | |
else | |
raise "Invalid Level" | |
end | |
try_count = 0 | |
collect_count = 0 | |
start_time = Time.now | |
questions.each do |e| | |
try_count += 1 | |
print e.to_s | |
print ' > ' | |
answer = gets.chomp.to_i | |
if answer == e.answer | |
puts '正解!' | |
collect_count += 1 | |
else | |
puts '不正解!' | |
end | |
if try_count % 10 == 0 | |
puts sprintf('%d 問突破', try_count) | |
end | |
end | |
puts sprintf('%d 問終了しました', try_count) | |
finish_time = Time.now | |
time = finish_time - start_time | |
puts sprintf('正解 :%d', collect_count) | |
puts sprintf('不正解:%d', try_count - collect_count) | |
puts sprintf('タイム:%d分%d秒', time / 60, time % 60) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment