LoginSignup
1
0

More than 5 years have passed since last update.

「オフラインリアルタイムどう書く第三回の参考問題」を解いてみた

Last updated at Posted at 2012-08-29

http://qiita.com/items/ebd8a56b41711ba459f9

こんな感じになった。

baseball_count.rb

class BsoCount
  def initialize
    @ball = @strike = @out = 0
  end
  def ball
    @ball += 1
    @ball = @strike = 0 if @ball == 4
  end
  def strike
    @strike += 1
    self.out if @strike == 3
  end
  def out
    @out += 1
    @out = 0 if @out == 3
    @ball = @strike = 0
  end
  def hit
    @ball = @strike = 0
  end
  def foulball
    @strike += 1 unless @strike == 2
  end
  def result
    [@out, @strike, @ball]
  end
end

ansArr = []
bso = BsoCount.new
STDIN.gets.each_char do |actionChar|
  case actionChar
  when 's' then bso.strike
  when 'b' then bso.ball
  when 'f' then bso.foulball
  when 'h' then bso.hit
  when 'p' then bso.out
  end
  ansArr.push bso.result.to_s
end
print ansArr.join ','
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0