Source

PythonDojo / 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