Ruby版も書いてみた
Rubyでも書いてみました。ほぼベタ移植ですが……。
ーーーーーーーーーーーーーーーーーーーー
def score(frames)
rest = frames
total = 0
frame_count = 1
while rest != []
current_frame = rest[0]
a, b = current_frame
if rest.length == 1
c = current_frame[2]
total += a + b + c
else
if a == 10
total += strike(rest, frame_count)
elsif a + b == 10
total += spare(rest, frame_count)
else
total += a + b
end
frame_count += 1
end
rest = rest[1...]
end
total
end
def spare(frames, frame_count)
10 + frames[1][0]
end
def strike(frames, frame_count)
score = 10
if frame_count == 9
score += frames[1][0] + frames[1][1]
## frame_count should be <= 9
elsif frames[1][0] == 10
score += 10 + frames[2][0]
else
score += frames[1][0] + frames[1][1]
end
score
end
frames = [
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[135, 0],
[10, 0],
[10, 0, 0]
]
puts score(frames) # 165
frames = [
[10, 0],
[10, 0],
[10, 0],
[10, 0],
[10, 0],
[10, 0],
[10, 0],
[10, 0],
[10, 0],
[10, 10, 10]
]
puts score(frames) # 300
ーーーーーーーーーーーーーーーーーーーー
新規登録で充実の読書を
- マイページ
- 読書の状況から作品を自動で分類して簡単に管理できる
- 小説の未読話数がひと目でわかり前回の続きから読める
- フォローしたユーザーの活動を追える
- 通知
- 小説の更新や作者の新作の情報を受け取れる
- 閲覧履歴
- 以前読んだ小説が一覧で見つけやすい
アカウントをお持ちの方はログイン
ビューワー設定
文字サイズ
背景色
フォント
組み方向
機能をオンにすると、画面の下部をタップする度に自動的にスクロールして読み進められます。
応援すると応援コメントも書けます