-
Notifications
You must be signed in to change notification settings - Fork 0
ボーリングプログラム作成 #3
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
base: main
Are you sure you want to change the base?
ボーリングプログラム作成 #3
Conversation
JunichiIto
left a comment
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.
全体的に修正が必要な箇所が多数あるので、提出物のページにコメントします。
また、bug_cafeへのシンボリックリンク?がdiffに含まれています。
これも不要なので削除してください。
03.bowling/y-bowling.rb
Outdated
| end | ||
|
|
||
|
|
||
| p point No newline at end of file |
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.
ファイルの末尾に赤丸が出ないようにしてください。
https://docs.komagata.org/5676#crlf
あと、pメソッドはデバッグ目的で使うことが多いメソッドです。
通常のターミナル出力はputsまたはprintを使ってください。
JunichiIto
left a comment
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/y-bowling.rb
Outdated
| #!/usr/bin/env ruby | ||
|
|
||
| score = ARGV[0] | ||
| score = score.split(',') |
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.
再代入を避け、別の変数名を付けましょう
https://bootcamp.fjord.jp/pages/avoid-re-assign
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/y-bowling.rb
Outdated
| frames[9] += frames[10] | ||
| frames.pop |
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.
| frames[9] += frames[10] | |
| frames.pop | |
| frames[9] += frames.pop |
でよいですね
03.bowling/y-bowling.rb
Outdated
| end | ||
|
|
||
| point = 0 | ||
| count = 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.
each_with_indexメソッドを使えばcount変数をなくせるはずですー
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/y-bowling.rb
Outdated
| count = 0 | ||
| frames.each do |frame| | ||
| point += if frame.length == 2 && frame[0] == 10 | ||
| if frames[count + 1].length == 3 |
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.
next_frame = frames[count + 1]のようにして変数に入れるとコードが読みやすくなります。
説明変数、説明用変数、みたいなキーワードでネットを検索してみてください。
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/y-bowling.rb
Outdated
| frames.each do |frame| | ||
| point += if frame.length == 2 && frame[0] == 10 | ||
| if frames[count + 1].length == 3 | ||
| 10 + frames[count + 1][0] + frames[count + 1][1] |
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.
| 10 + frames[count + 1][0] + frames[count + 1][1] | |
| 10 + frames[count + 1][0..1].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/y-bowling.rb
Outdated
| elsif frames[count + 1][0] == 10 | ||
| 10 + 10 + frames[count + 2][0] | ||
| else | ||
| 10 + frames[count + 1][0] + frames[count + 1][1] |
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.
| 10 + frames[count + 1][0] + frames[count + 1][1] | |
| 10 + frames[count + 1].sum |
elseに入ってるならlengthが2であることは決定しているので、こんなふうに書けますね
JunichiIto
left a comment
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/y-bowling.rb
Outdated
| # frozen_string_literal: true | ||
|
|
||
| score = ARGV[0] | ||
| split_score = score.split(',') |
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.
配列なので複数形にした方がわかりやすいです。
あと、splitは動詞なので、split_scoreだと「スコアを分割する」というメソッドに見えます。
https://qiita.com/jnchito/items/459d58ba652bf4763820
splitした、という意味なら splited_scores みたいな名前になりますが、そもそもここはシンプルに
| split_score = score.split(',') | |
| scores = ARGV[0].split(',') |
で良さそうです
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.
なるほどです。
こういう単純そうに感じることでも言われてようやく気が付きます。。🥲
思考の柔軟性がたりないのでしょうか、、
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.
思考の柔軟性がたりないのでしょうか、、
最初から完璧に書ける人はいないので大丈夫です!
僕なんか20年プログラマやってますからね。単純に経験値だけ見てもきっと雲泥の差があると思います。
(同じコードを見ても、jepeさんには見えない何かが僕にはたくさん見えてるはず)
過去に同じようなテーマでブログを書いたので参考にしてみてください。
https://blog.jnito.com/entry/2020/08/24/083030
リーダブルコードやCODE COMPLETEみたいに「読みやすくてわかりやすいコードの書き方」に特化した技術書もあるので、そうした本を読んでみるのもお勧めです!
03.bowling/y-bowling.rb
Outdated
| shots << if s == 'X' | ||
| 10 | ||
| else | ||
| s.to_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.
| shots << if s == 'X' | |
| 10 | |
| else | |
| s.to_i | |
| end | |
| shots << (s == 'X' ? 10 : s.to_i) |
三項演算子の方がすっきりしそうです
さらにいうと、ロジック的にはここまで短くできるかも?
split_score.each do |s|
shots << (s == 'X' ? 10 : s.to_i)
shots << 0 if shots.length < 18 && s == 'X'
endThere 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.
3項演算子は理解できていたつもりになっていましたが、このように書けるのは正直思いもしませんでした。
こういったテクニックを教えていただけるのはとても良い経験となります😁
03.bowling/y-bowling.rb
Outdated
| frames = [] | ||
| shots.each_slice(2) do |s| | ||
| frames << s | ||
| 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.
| frames = [] | |
| shots.each_slice(2) do |s| | |
| frames << s | |
| end | |
| frames = shots.each_slice(2).to_a |
でOKです
03.bowling/y-bowling.rb
Outdated
| 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.
| point = 0 | |
| frames.each_with_index do |frame, i| | |
| point = frames.each_with_index.sum do |frame, i| |
sumメソッドを使ってpoint +=をなくしてみましょう
03.bowling/y-bowling.rb
Outdated
| point = 0 | ||
| frames.each_with_index do |frame, i| | ||
| next_frame = frames[i + 1] | ||
| not_last_frame = frame.length == 2 |
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.
| not_last_frame = frame.length == 2 | |
| not_last_frame = i != 9 |
とした方がコードの意図が明確になりそうです
JunichiIto
left a comment
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.
いくつかコメントしましたが大きな問題ではないのでこれでOKです!🙆♂️
| frames = shots.each_slice(2).to_a | ||
|
|
||
| frames[9] += frames.pop if frames.length == 11 | ||
| point = frames.each_with_index.sum 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.
| frames = shots.each_slice(2).to_a | |
| frames[9] += frames.pop if frames.length == 11 | |
| point = frames.each_with_index.sum do |frame, i| | |
| frames = shots.each_slice(2).to_a | |
| frames[9] += frames.pop if frames.length == 11 | |
| point = frames.each_with_index.sum do |frame, i| |
こんなふうに改行した方が処理のグルーピングとしてわかりやすいように思いました
| next_frame = frames[i + 1] | ||
| not_last_frame = i != 9 | ||
| strilke = frame[0] == 10 | ||
| spare = frame.sum == 10 | ||
|
|
||
| if not_last_frame && strilke | ||
| if next_frame.length == 3 | ||
| 10 + next_frame[0..1].sum | ||
| elsif next_frame[0] == 10 | ||
| 10 + 10 + frames[i + 2][0] | ||
| else | ||
| 10 + next_frame.sum | ||
| end | ||
| elsif not_last_frame && spare | ||
| frame.sum + next_frame[0] | ||
| else | ||
| frame.sum | ||
| 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.
あくまで参考情報ですが、こんなふうに書くこともできます。
| next_frame = frames[i + 1] | |
| not_last_frame = i != 9 | |
| strilke = frame[0] == 10 | |
| spare = frame.sum == 10 | |
| if not_last_frame && strilke | |
| if next_frame.length == 3 | |
| 10 + next_frame[0..1].sum | |
| elsif next_frame[0] == 10 | |
| 10 + 10 + frames[i + 2][0] | |
| else | |
| 10 + next_frame.sum | |
| end | |
| elsif not_last_frame && spare | |
| frame.sum + next_frame[0] | |
| else | |
| frame.sum | |
| end | |
| next frame.sum if i == 9 | |
| next_frame = frames[i + 1] | |
| strilke = frame[0] == 10 | |
| spare = frame.sum == 10 | |
| if strilke | |
| if i == 8 | |
| 10 + next_frame[0..1].sum | |
| elsif next_frame[0] == 10 | |
| 10 + 10 + frames[i + 2][0] | |
| else | |
| 10 + next_frame.sum | |
| end | |
| elsif spare | |
| frame.sum + next_frame[0] | |
| else | |
| frame.sum | |
| end |
ポイントは以下の通りです。
- 最終フレームだけ特別扱いして(
next frame.sum if i == 9)、21行目、29行目の条件分岐をシンプルにする if next_frame.length == 3が真になるのは9フレーム目しかないので、if i == 8としてしまう(これは好みの問題かも)
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.