class Bowling::Frame

Attributes

balls[RW]
frame_number[RW]
game[RW]

Public Class Methods

new( frame_number, game, data = nil ) click to toggle source

Construction ===

# File lib/bowling/frame.rb, line 8
def initialize( frame_number, game, data = nil )
  @frame_number = frame_number
  @game         = game
  @balls        = Array.new( total_balls ){ |i| Bowling::Ball.new i, self }

  load data if data
end

Public Instance Methods

base_score() click to toggle source

The base score ignores the 3rd ball of the final frame

# File lib/bowling/frame.rb, line 73
def base_score
  [ balls[0].score + balls[1].score, 10 ].min # No frame should have more than 10
end
final?() click to toggle source

Returns true if this is the last frame of the game

# File lib/bowling/frame.rb, line 116
def final?
  frame_number == 9 # Frame with index 9 is 10th frame
end
first_ball() click to toggle source
# File lib/bowling/frame.rb, line 28
def first_ball
  balls[0]
end
load( data ) click to toggle source

Load an array of scores (integers) representing each ball

# File lib/bowling/frame.rb, line 22
def load( data )
  raise ArgumentError.new '#load needs array' unless data.instance_of? Array
  data.each_with_index{ |pins, i| @balls[i].knocked_pins = pins }
end
next_ball() click to toggle source
# File lib/bowling/frame.rb, line 43
def next_ball
  if final?
    balls[1]
  else
    next_frame.first_ball
  end
end
next_frame() click to toggle source
# File lib/bowling/frame.rb, line 33
def next_frame
  get_frame frame_number+1
end
next_next_ball() click to toggle source
# File lib/bowling/frame.rb, line 52
def next_next_ball
  if final?
    next_frame.balls[2]
  elsif next_frame.strike? and not next_frame.final?
    next_next_frame.first_ball
  else
    next_frame.balls[1]
  end
end
next_next_frame() click to toggle source
# File lib/bowling/frame.rb, line 38
def next_next_frame
  get_frame frame_number+2
end
score() click to toggle source

Calculations ===

# File lib/bowling/frame.rb, line 65
def score
  base_score + strike_score + spare_score
end
spare?() click to toggle source
# File lib/bowling/frame.rb, line 126
def spare?
  not strike? and ( balls[0].score + balls[1].score >= 10 )
end
spare_score() click to toggle source

Spare score represents the additional score added to the base when a spare occurs It is equal to the knocked_pins of the following ball

# File lib/bowling/frame.rb, line 97
def spare_score
  return 0 unless spare?

  if final?
    balls[2].score
  else
    next_ball.score
  end
end
strike?() click to toggle source
# File lib/bowling/frame.rb, line 121
def strike?
  first_ball.perfect?
end
strike_score() click to toggle source

Strike score represents the additional score added to the base when a strike occurs It is equal to the knocked_pins of the following 2 balls

# File lib/bowling/frame.rb, line 82
def strike_score
  return 0 unless strike?
  
  if final?
    balls[1].score + balls[2].score
  else
    next_ball.score + next_next_ball.score
  end
end
total_balls() click to toggle source
# File lib/bowling/frame.rb, line 108
def total_balls
  final? ? 3 : 2
end

Protected Instance Methods

get_frame( number ) click to toggle source
# File lib/bowling/frame.rb, line 136
def get_frame( number )
  if game.frames[number]
    game.frames[number]
  else
    Bowling::NullFrame
  end
end