Commits

Christoph Schindler committed 919d465 Merge

9. Coding-Dojo: Bowling. Again.

  • Participants
  • Parent commits 06f25c5, 20aeae9

Comments (0)

Files changed (2)

BowlingWithAVengeance/bowling.py

+STRIKE = 10
+SPARE = '/'
+
+
+def parse_roll(roll):
+    if roll == '/':
+        return SPARE
+    return STRIKE
+    
+def frame_score(frame):
+    return sum(frame)
+    
+def is_strike(frame):
+    if frame[0]==10:
+        return True
+    return False
+
+def is_spare(frame):
+    return frame_score(frame) == 10 and not is_strike(frame)
+    
+def get_score(frames):
+    was_spare = False
+    was_strike = False
+    score = sum(frame_score(frame) for frame in frames[:10])
+    for index, frame in enumerate(frames):
+
+        if index < len(frames) - 1 and index < 10:
+            # if this is not the last frame, we might want to
+            # look at future frames for additional scores
+            next_frame = frames[index+1]
+            if is_strike(frame):
+                score += frame_score(next_frame)
+                if is_strike(next_frame):
+                    next_next_frame = frames[index+2]
+                    score += next_next_frame[0]
+            elif is_spare(frame):
+                score += next_frame[0]
+    return score

BowlingWithAVengeance/test.py

+from nose.tools import assert_equal
+
+import bowling
+
+def test_parse_strike():
+    assert_equal(bowling.parse_roll("X"),bowling.STRIKE)
+    
+def test_parse_spare():
+    assert_equal(bowling.parse_roll("/"),bowling.SPARE)
+    
+def test_simple_frame():
+    assert_equal(bowling.frame_score((3,2)), 5)
+    
+def test_simple_frame_2():
+    assert_equal(bowling.frame_score((9,0)), 9)
+    
+def test_strike():
+    assert_equal(bowling.frame_score((10,)), 10)
+    
+def test_get_score():
+    assert_equal(bowling.get_score(((3,2), (4,5))), 14)
+
+def test_get_score_2():
+    assert_equal(bowling.get_score(((3,5), (4,5))), 17)
+    
+def test_get_score_3():
+    assert_equal(bowling.get_score(((10,), (3,5))), 26)
+    
+def test_get_score_4():
+    assert_equal(bowling.get_score(((10,), (10,), (5,4))), 25+19+9)
+    
+def test_get_score_5():
+    assert_equal(bowling.get_score(((10,), (10,), (2,4))), 22+16+6)
+    
+def test_get_score_three_strikes():
+    assert_equal(bowling.get_score(((10,), (10,), (10,), (2,4))), 30+22+16+6)
+    
+def test_spare():
+    assert_equal(bowling.get_score(((1, 9), (2, 4))), 18)
+        
+def test_spare_2():
+    assert_equal(bowling.get_score(((1, 9), (3, 4))), 20)
+    
+def test_perfect_game():
+    assert_equal(bowling.get_score([(10,)] * 12), 300) 
+    
+def test_only_nines():
+    assert_equal(bowling.get_score([(9,0)] * 10), 90)
+        
+def test_game_with_spares():
+    assert_equal(bowling.get_score([(5,5)] * 10 + [(5,)]), 150)
+    
+def test_game_almostperfetc():
+    assert_equal(bowling.get_score([(10,)] * 10 + [(3,7)]), 283)