Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ボウリングスコア計算プログラムの追加 #3

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

yuu-cc
Copy link
Owner

@yuu-cc yuu-cc commented Nov 3, 2023

No description provided.

end

frames = []
(1..10).each do |i|
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここは each よりも map を使うとコードがスッキリしますね。

frames = (1..10).map do |i|
  # frameに挿入したい値を戻り値にする
end

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

引数 i だと何を指しているのか分かりづらいですし、今回の場合 i は未使用となっていますので、引数を省略しましょう。

# 10フレーム目
if i == 9
# 単に足すだけ
total_point += frame.sum
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この加算式は全ての分岐に存在するので、分岐の前後に移動させることでコードの重複をなくせるはずです

elsif i == 8
total_point += frame.sum
# ストライク
if frame[0] == 10
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

フレームを判定してからストライクorスペアを判定していることで、ストライク・スペアの計算処理が2回記述されてしまっています。
先にストライクorスペアを判定して、それからフレームを判定すれば、フレームによる処理の違いのみを分岐させられるので、コードの重複を減らせるはずです。

total_point += frame.sum
# ストライク
if frame[0] == 10
total_point += frames[i + 1][0]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この2行は以下のように書くと1行にまとめられますね。
※他の指摘の修正によりまとめられなくなるかもしれませんが、参考まで。

total_point += frames[i + 1][0..1].sum

@@ -0,0 +1,9 @@
./bowling.rb 6,3,9,0,0,3,8,2,7,3,X,9,1,8,0,X,6,4,5 #-> 139
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

テストを自動化する試みはとても良いですね 👍 効率的に開発が進められますよね。

end

total_point = 0
frames.each_with_index do |frame, i|
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可能であればここでも sum を使うと、total_pointを自分で加算する必要がなくなりコードがスッキリするのでおすすめです。

total_point = frames.each_with_index.sum do |frame, i|
  # 各フレームのスコアをブロックの戻り値にする
end

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらは今回対応しない認識で大丈夫でしょうか?

end
# スペア
elsif frame.sum == 10
total_point += frames[i + 1][0]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最終フレームがスペアの場合に存在しないフレームを参照してしまう、という懸念はないでしょうか?

end

total_point = 0
frames.each_with_index do |frame, i|
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらは今回対応しない認識で大丈夫でしょうか?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants