Class: BowlingScoreKeeper

Inherits:
Object
  • Object
show all
Defined in:
lib/bowling_score_keeper.rb

Constant Summary collapse

MAX_ROLL_POINTS =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBowlingScoreKeeper

Returns a new instance of BowlingScoreKeeper.



8
9
10
# File 'lib/bowling_score_keeper.rb', line 8

def initialize
  @rolls = []
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/bowling_score_keeper.rb', line 5

def name
  @name
end

Instance Method Details

#callObject



12
13
14
15
16
# File 'lib/bowling_score_keeper.rb', line 12

def call
  set_name

  track_score
end

#roll(pins) ⇒ Object



18
19
20
21
# File 'lib/bowling_score_keeper.rb', line 18

def roll(pins)
  puts "You knocked #{pins} pins."
  @rolls << pins.to_i
end

#scoreObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bowling_score_keeper.rb', line 23

def score
  begin
    score = 0
    first_in_frame = 0

    10.times do
      if(strike?(first_in_frame))
        score += MAX_ROLL_POINTS +
          @rolls[first_in_frame + 1] +
          @rolls[first_in_frame + 2]
        first_in_frame += 1
      elsif(spare?(first_in_frame))
        score += MAX_ROLL_POINTS + @rolls[first_in_frame + 2]
        first_in_frame += 2
      else
        score += @rolls[first_in_frame] + @rolls[first_in_frame + 1]
        first_in_frame += 2
      end
    end

    score
  rescue
    'incomplete'
  end
end