From 2aef970d38a1441d20a05df273018a0ab9e702d1 Mon Sep 17 00:00:00 2001 From: shoma Date: Thu, 31 Jul 2025 18:12:21 +0900 Subject: [PATCH 1/4] first commit --- 03.bowling/bowling.rb | 6 ++++ 03.bowling/bowling_method.rb | 62 ++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100755 03.bowling/bowling.rb create mode 100644 03.bowling/bowling_method.rb diff --git a/03.bowling/bowling.rb b/03.bowling/bowling.rb new file mode 100755 index 0000000000..f93373f4e2 --- /dev/null +++ b/03.bowling/bowling.rb @@ -0,0 +1,6 @@ +#! /usr/bin/env ruby +require_relative './bowling_method' + +frames = format_score(ARGV[0]) +puts sum_score(frames) + diff --git a/03.bowling/bowling_method.rb b/03.bowling/bowling_method.rb new file mode 100644 index 0000000000..d9cc84209d --- /dev/null +++ b/03.bowling/bowling_method.rb @@ -0,0 +1,62 @@ +def strings_to_numbers(scores) + converted_array = [] + scores.each do |score| + if score == 'X' + converted_array << 10 + converted_array << 0 + else + converted_array.push(score.to_i) + end + end + converted_array +end + +def format_score(raw_scores) + scores = raw_scores.split(',') + integer_scores = strings_to_numbers(scores) + # 10フレーム目でストライクかスペアを出すとframesの要素数が10以上になる。こうすることで10フレーム目のストライク、スペアの得点加算も通常と同じ処理で済むのでこの形にしてある + formatted_scores = integer_scores.each_slice(2).to_a +end + +def strike?(frame) + frame[0] == 10 ? true : false +end + +def spare?(frame) + frame[0] + frame[1] == 10 ? true : false +end + +def add_strike_bonus (index, frames) + strike_bonus = 0 + next_index = index + 1 + next_frame = frames[next_index] + if strike? next_frame + after_next_frame = frames[next_index + 1] + strike_bonus += next_frame[0] + after_next_frame[0] + else + strike_bonus += next_frame[0] + next_frame[1] + end +end + +def add_spare_bonus (index, frames) + spare_bonus = 0 + next_index = index + 1 + next_frame = frames[next_index] + spare_bonus += next_frame[0] +end + +def sum_score(frames) + total_score = 0 + total_frames = 10 + total_frames.times do |index| + frame = frames[index] + total_score += frame[0] + frame[1] + if strike?(frame) + total_score += add_strike_bonus(index, frames) + elsif spare?(frame) + total_score += add_spare_bonus(index, frames) + end + end + total_score +end + From 519664bf62953e0c7b312dade4bf55bafb3c4cbf Mon Sep 17 00:00:00 2001 From: shoma Date: Fri, 1 Aug 2025 09:54:19 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BD=99=E8=A8=88=E3=81=AA=E7=A9=BA?= =?UTF-8?q?=E7=99=BD=E3=81=A8=E5=86=97=E9=95=B7=E3=81=AA=E6=A7=8B=E6=96=87?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 03.bowling/bowling_method.rb | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/03.bowling/bowling_method.rb b/03.bowling/bowling_method.rb index d9cc84209d..bc7e86196f 100644 --- a/03.bowling/bowling_method.rb +++ b/03.bowling/bowling_method.rb @@ -1,6 +1,6 @@ def strings_to_numbers(scores) converted_array = [] - scores.each do |score| + scores.each do |score| if score == 'X' converted_array << 10 converted_array << 0 @@ -13,36 +13,34 @@ def strings_to_numbers(scores) def format_score(raw_scores) scores = raw_scores.split(',') - integer_scores = strings_to_numbers(scores) + integer_scores = strings_to_numbers(scores) # 10フレーム目でストライクかスペアを出すとframesの要素数が10以上になる。こうすることで10フレーム目のストライク、スペアの得点加算も通常と同じ処理で済むのでこの形にしてある - formatted_scores = integer_scores.each_slice(2).to_a + integer_scores.each_slice(2).to_a end def strike?(frame) - frame[0] == 10 ? true : false + frame[0] == 10 end def spare?(frame) - frame[0] + frame[1] == 10 ? true : false + frame[0] + frame[1] == 10 end -def add_strike_bonus (index, frames) - strike_bonus = 0 +def calc_strike_bonus(index, frames) next_index = index + 1 next_frame = frames[next_index] if strike? next_frame after_next_frame = frames[next_index + 1] - strike_bonus += next_frame[0] + after_next_frame[0] - else - strike_bonus += next_frame[0] + next_frame[1] + next_frame[0] + after_next_frame[0] + else + next_frame[0] + next_frame[1] end end - -def add_spare_bonus (index, frames) - spare_bonus = 0 + +def calc_spare_bonus(index, frames) next_index = index + 1 next_frame = frames[next_index] - spare_bonus += next_frame[0] + next_frame[0] end def sum_score(frames) @@ -52,9 +50,9 @@ def sum_score(frames) frame = frames[index] total_score += frame[0] + frame[1] if strike?(frame) - total_score += add_strike_bonus(index, frames) + total_score += calc_strike_bonus(index, frames) elsif spare?(frame) - total_score += add_spare_bonus(index, frames) + total_score += calc_spare_bonus(index, frames) end end total_score From 649d3c9a15f247d2761091ec07a36948f22724ac Mon Sep 17 00:00:00 2001 From: shoma Date: Fri, 1 Aug 2025 10:53:55 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=E3=83=9E=E3=82=B8=E3=83=83=E3=82=AF?= =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 03.bowling/bowling.rb | 3 ++- 03.bowling/bowling_method.rb | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/03.bowling/bowling.rb b/03.bowling/bowling.rb index f93373f4e2..c591dbf98c 100755 --- a/03.bowling/bowling.rb +++ b/03.bowling/bowling.rb @@ -1,6 +1,7 @@ #! /usr/bin/env ruby +# frozen_string_literal: true + require_relative './bowling_method' frames = format_score(ARGV[0]) puts sum_score(frames) - diff --git a/03.bowling/bowling_method.rb b/03.bowling/bowling_method.rb index bc7e86196f..1c489eccb1 100644 --- a/03.bowling/bowling_method.rb +++ b/03.bowling/bowling_method.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + def strings_to_numbers(scores) converted_array = [] scores.each do |score| @@ -57,4 +59,3 @@ def sum_score(frames) end total_score end - From 6c5769af58c93fa118d75d7d19211d00b89be907 Mon Sep 17 00:00:00 2001 From: shoma Date: Thu, 7 Aug 2025 14:23:07 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=E8=AA=A4=E8=A7=A3=E3=82=92=E7=94=9F?= =?UTF-8?q?=E3=82=80=E5=8F=AF=E8=83=BD=E6=80=A7=E3=81=8C=E3=81=82=E3=82=8B?= =?UTF-8?q?=E5=91=BD=E5=90=8D=E3=81=A7=E3=81=82=E3=81=A3=E3=81=9F=E3=81=9F?= =?UTF-8?q?=E3=82=81=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 03.bowling/bowling_method.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/03.bowling/bowling_method.rb b/03.bowling/bowling_method.rb index 1c489eccb1..f4dba1fa39 100644 --- a/03.bowling/bowling_method.rb +++ b/03.bowling/bowling_method.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -def strings_to_numbers(scores) +def to_int_scores(strings_scores) converted_array = [] - scores.each do |score| + strings_scores.each do |score| if score == 'X' converted_array << 10 converted_array << 0 @@ -13,11 +13,11 @@ def strings_to_numbers(scores) converted_array end -def format_score(raw_scores) +def parse_scores(raw_scores) scores = raw_scores.split(',') - integer_scores = strings_to_numbers(scores) + int_scores = to_int_scores(scores) # 10フレーム目でストライクかスペアを出すとframesの要素数が10以上になる。こうすることで10フレーム目のストライク、スペアの得点加算も通常と同じ処理で済むのでこの形にしてある - integer_scores.each_slice(2).to_a + int_scores.each_slice(2).to_a end def strike?(frame)