fjord/bowling.rb

56 lines
1.7 KiB
Ruby
Raw Normal View History

2024-03-10 22:39:11 +09:00
#!/usr/bin/ruby
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) do |s|
frames << s
end
point = 0
2024-03-13 15:10:54 +09:00
frames[0..7].each_with_index do |frame, index|
point += frame.sum
2024-03-13 15:10:54 +09:00
# ストライクかつ次もストライクの場合
point += frames[index + 2].first if frame == [10, 0] && frames[index + 1].first == 10
if frame == [10, 0] # ストライク1回のみの場合
point += frames[index + 1].sum # 次のフレームの合計値が得点となる
end
2024-03-13 15:10:54 +09:00
if frame.sum == 10 && frame != [10, 0] # スペアの場合
point += frames[index + 1].first # 次のフレームの一投目が得点となる
end
puts "#{index + 1}フレーム目の得点: #{point}"
2024-03-10 22:39:11 +09:00
end
2024-03-13 15:10:54 +09:00
frames[8..11].each_with_index do |frame, index| # 例外処理のためにループを分割する
point += frame.sum
if frame == [10, 0]
point += frames[index + 9].sum
print frames[index + 8]
end
if frame.sum == 10 && frame != [10, 0]
point += frames[index + 9].first
end
puts "#{index + 9}フレーム目の得点: #{point}"
2024-03-12 22:51:17 +09:00
end
2024-03-13 15:10:54 +09:00
point -= frames[10].sum if frames[10] # 計算結果から余剰加点を除外
point -= frames[11].first if frames[11] == [10, 0] # 計算結果から余剰加点を除外
2024-03-12 22:51:17 +09:00
print frames
puts point
2024-03-13 15:10:54 +09:00
# ストライクは次の「2投の合計値」が得点となる
# つまりストライクを仮に1フレーム目で取り、2フレーム目もストライクだった場合、1フレーム目の得点には3フレーム目の初回分までが加算される
# 10フレーム目の3投目だけはスペアの加算対象としてはならない