-
Notifications
You must be signed in to change notification settings - Fork 0
bowling code #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?
bowling code #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,46 @@ | ||
| require 'date' | ||
| require 'optparse' | ||
|
|
||
| # コマンドラインでparseする | ||
| options = {} | ||
| OptionParser.new do |opts| | ||
| opts.banner = "Usage: ruby calendar.rb [options]" | ||
|
|
||
| opts.on("-m", "--month MONTH", Integer, "Specify the month (1-12)") do |month| | ||
| options[:month] = month | ||
| end | ||
|
|
||
| opts.on("-y", "--year YEAR", Integer, "Specify the year") do |year| | ||
| options[:year] = year | ||
| end | ||
| end.parse! | ||
|
|
||
| # 今の日付を取る | ||
| current_date = Date.today | ||
| year = options[:year] || current_date.year | ||
| month = options[:month] || current_date.month | ||
|
|
||
| # date enumeratorを作る | ||
| date_range = Date.new(year, month, 1)..Date.new(year, month, -1) | ||
| dates_enum = date_range.to_enum | ||
|
|
||
| # カレンダーを表示する | ||
| puts "\n#{Date::MONTHNAMES[month]} #{year}".center(20) | ||
| puts "日 月 火 水 木 金 土" | ||
|
|
||
| # 最初の日に計算する | ||
| initial_offset = dates_enum.peek.wday | ||
|
|
||
| # 見やすいのため | ||
| print " " * initial_offset | ||
|
|
||
| # 1ヶ月分表示する | ||
| dates_enum.each do |date| | ||
| print date.day.to_s.rjust(2) + " " | ||
|
|
||
| # 土曜日になったら新しいラインを作る | ||
| puts if date.saturday? | ||
| end | ||
|
|
||
| puts "\n" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| require 'rubocop' | ||
| require 'debug' | ||
|
|
||
| score = ARGV[0] | ||
| scores = score.split(',') | ||
| shots = [] | ||
|
|
||
| scores.each do |s| | ||
| if s == 'X' | ||
| shots << 10 | ||
| shots << 0 | ||
| else | ||
| shots << s.to_i | ||
| end | ||
| end | ||
|
|
||
| frames = [] | ||
| shots.each_slice(2) do |s| | ||
|
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. ここは以下のように書けますね。 frames = shots.each_slice(2).to_a |
||
| frames << s | ||
| end | ||
| p frames | ||
|
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. デバッグ用だと思うので、削除しておきましょう。 |
||
| point = 0 | ||
|
|
||
| frames.each_with_index do |frame, index| | ||
|
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. ここは以下のように Enumerable#sum を使うと、自分で point = frames.each_with_index.sum do |frame, index|
# 戻り値を各フレームのスコアにする
end |
||
| # binding.break | ||
| if index >=9 | ||
| point += frame.sum | ||
| # elsif frame_index == 11 | ||
|
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. 不要であれば削除しておきましょう。3行↑にある |
||
|
|
||
| else | ||
| if frame[0] == 10 | ||
| if frames[index + 1][0] == 10 | ||
| if frames[index + 2][0] == 10 | ||
| point += 30 | ||
| else | ||
| point += 20 + frames[index + 2][0] | ||
| end | ||
|
|
||
| else | ||
| point += 10 + frames[index + 1].sum | ||
| end | ||
|
|
||
|
|
||
| elsif frame.sum == 10 | ||
| point += 10 + frames[index + 1][0] | ||
| else | ||
| point += frame.sum | ||
| end | ||
| end | ||
| 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.
カレンダーのプログラムがPRに混じってしまっていますね。新しいブランチを作成する際は、今どのブランチにいるのかに注意して作成するようにしましょう。今回はこのままでOKです。