fjord/bowling.rb

32 lines
739 B
Ruby
Raw Normal View History

2024-03-10 22:39:11 +09:00
#!/usr/bin/ruby
# frozen_string_literal: true
2024-03-10 22:39:11 +09:00
score = ARGV[0]
scores = score.split(',')
shots = []
scores.each do |s|
if s == 'X'
shots << 10
shots << 0
else
shots << s.to_i
end
end
frames = shots.each_slice(2).to_a
2024-03-10 22:39:11 +09:00
point = 0
strike = [10, 0]
2024-03-10 22:39:11 +09:00
frames[0..8].each_with_index do |frame, index|
point += frame.sum
# 連続ストライクの場合、2つ先のフレームの1投目も得点となる
point += frames[index + 2].first if frame == strike && frames[index + 1] == strike
point += frames[index + 1].sum if frame == strike
point += frames[index + 1].first if frame.sum == 10 && frame != strike
2024-03-10 22:39:11 +09:00
end
frames[9..11].map { |frame| point += frame.sum } # 条件分岐を要しない加点処理
2024-03-12 22:51:17 +09:00
puts point