-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,35 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #!/usr/bin/env ruby | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # frozen_string_literal: true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scores = ARGV[0].split(',') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| shots = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scores.each do |s| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| shots << (s == 'X' ? 10 : s.to_i) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| shots << 0 if shots.length < 18 && s == 'X' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+16
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. あくまで参考情報ですが、こんなふうに書くこともできます。
Suggested change
ポイントは以下の通りです。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. なるほど、確かに最終フレームを特別扱いするだけで、 特別な処理とそうでない処理を切り分けるというのはいろんなところで使えそうなので |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| puts point | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
こんなふうに改行した方が処理のグルーピングとしてわかりやすいように思いました