Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Game:
- def __init__(self):
- self.total = 0
- self.num_rolls = 0
- self.score_of_previous_frames = 0
- self.should_add_next_roll_as_bonus = False
- self.bonus_points = 0
- self.error = -1
- def put_into_error_state(self):
- self.total = self.error
- def roll(self, pins):
- self.num_rolls = self.num_rolls + 1
- if self.total == self.error:
- return
- if pins > 10:
- self.put_into_error_state()
- else:
- self.total = self.total + pins
- if self.should_add_next_roll_as_bonus and not self.total == self.error:
- self.bonus_points = self.bonus_points + pins
- self.should_add_next_roll_as_bonus = False
- is_end_of_frame = self.num_rolls % 2 == 0 # TODO: make this work with strikes
- if is_end_of_frame:
- this_frame_score = self.total - self.score_of_previous_frames
- if this_frame_score > 10:
- self.put_into_error_state()
- elif this_frame_score == 10:
- self.should_add_next_roll_as_bonus = True
- self.score_of_previous_frames = self.total
- def score(self):
- if self.num_rolls != 20:
- return self.error
- self.total = self.total + self.bonus_points
- return self.total
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement