From 9d3fe759062e1da96b17eebf7f712adc9320f6eb Mon Sep 17 00:00:00 2001 From: Rikuoh Date: Wed, 13 Mar 2024 20:37:35 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=9C=E3=82=A6=E3=83=AA=E3=83=B3=E3=82=B0?= =?UTF-8?q?=E3=81=AE=E3=82=B9=E3=82=B3=E3=82=A2=E8=A8=88=E7=AE=97=E3=83=97?= =?UTF-8?q?=E3=83=AD=E3=82=B0=E3=83=A9=E3=83=A0=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bowling.rb | 31 +++++++++++-------------------- rubocop.yml | 4 ++++ 2 files changed, 15 insertions(+), 20 deletions(-) create mode 100644 rubocop.yml diff --git a/bowling.rb b/bowling.rb index 59bc231..69f3dcf 100755 --- a/bowling.rb +++ b/bowling.rb @@ -1,4 +1,5 @@ #!/usr/bin/ruby +# frozen_string_literal: true score = ARGV[0] scores = score.split(',') @@ -19,37 +20,27 @@ end point = 0 -frames[0..7].each_with_index do |frame, index| +frames[0..9].each_with_index do |frame, index| # 条件分岐を要するフレームの加点処理 point += frame.sum - # ストライクかつ次もストライクの場合 + # 連続ストライクの場合、2つ先のフレームの1投目も得点となる 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 if frame.sum == 10 && frame != [10, 0] # スペアの場合 - point += frames[index + 1].first # 次のフレームの一投目が得点となる + point += frames[index + 1].first # 次のフレームの1投目が得点となる end - puts "#{index + 1}フレーム目の得点: #{point}" end -frames[8..11].each_with_index do |frame, index| # 例外処理のためにループを分割する +frames[10, 11].each do |frame| # 条件分岐を要しないフレームの加点処理 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}" end point -= frames[10].sum if frames[10] # 計算結果から余剰加点を除外 -point -= frames[11].first if frames[11] == [10, 0] # 計算結果から余剰加点を除外 -print frames -puts point - -# ストライクは次の「2投の合計値」が得点となる!!! -# つまりストライクを仮に1フレーム目で取り、2フレーム目もストライクだった場合、1フレーム目の得点には3フレーム目の初回分までが加算される!!!! -# 10フレーム目の3投目だけはスペアの加算対象としてはならない! +if frames[11].nil? # 3投目が存在しなかった場合は得点を表示して終了 + puts point +elsif frames[11] == [10, 0] || frames[9] == [10, 0] + point -= frames[11].first # 計算結果から余剰加点を除外して得点表示 + puts point +end diff --git a/rubocop.yml b/rubocop.yml new file mode 100644 index 0000000..cddb056 --- /dev/null +++ b/rubocop.yml @@ -0,0 +1,4 @@ +inherit_gem: + rubocop-fjord: + - "config/rubocop.yml" + # - "config/rails.yml"