-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Conversation
03.bowling/bowling.rb
Outdated
end | ||
|
||
frames = [] | ||
(1..10).each do |i| |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
引数 i
だと何を指しているのか分かりづらいですし、今回の場合 i
は未使用となっていますので、引数を省略しましょう。
03.bowling/bowling.rb
Outdated
# 10フレーム目 | ||
if i == 9 | ||
# 単に足すだけ | ||
total_point += frame.sum |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この加算式は全ての分岐に存在するので、分岐の前後に移動させることでコードの重複をなくせるはずです
03.bowling/bowling.rb
Outdated
elsif i == 8 | ||
total_point += frame.sum | ||
# ストライク | ||
if frame[0] == 10 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
フレームを判定してからストライクorスペアを判定していることで、ストライク・スペアの計算処理が2回記述されてしまっています。
先にストライクorスペアを判定して、それからフレームを判定すれば、フレームによる処理の違いのみを分岐させられるので、コードの重複を減らせるはずです。
03.bowling/bowling.rb
Outdated
total_point += frame.sum | ||
# ストライク | ||
if frame[0] == 10 | ||
total_point += frames[i + 1][0] |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
テストを自動化する試みはとても良いですね 👍 効率的に開発が進められますよね。
03.bowling/bowling.rb
Outdated
end | ||
|
||
total_point = 0 | ||
frames.each_with_index do |frame, i| |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらは今回対応しない認識で大丈夫でしょうか?
03.bowling/bowling.rb
Outdated
end | ||
# スペア | ||
elsif frame.sum == 10 | ||
total_point += frames[i + 1][0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
最終フレームがスペアの場合に存在しないフレームを参照してしまう、という懸念はないでしょうか?
03.bowling/bowling.rb
Outdated
end | ||
|
||
total_point = 0 | ||
frames.each_with_index do |frame, i| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらは今回対応しない認識で大丈夫でしょうか?
No description provided.