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

ーーーーーーーーーーーーーーーーーーーー

  • Xで共有
  • Facebookで共有
  • はてなブックマークでブックマーク

作者を応援しよう!

ハートをクリックで、簡単に応援の気持ちを伝えられます。(ログインが必要です)

応援したユーザー

応援すると応援コメントも書けます

新規登録で充実の読書を

マイページ
読書の状況から作品を自動で分類して簡単に管理できる
小説の未読話数がひと目でわかり前回の続きから読める
フォローしたユーザーの活動を追える
通知
小説の更新や作者の新作の情報を受け取れる
閲覧履歴
以前読んだ小説が一覧で見つけやすい
新規ユーザー登録無料

アカウントをお持ちの方はログイン

カクヨムで可能な読書体験をくわしく知る