Advertisement
Archon

TDD demo: code

Aug 25th, 2021
1,285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. class Game:
  2.     def __init__(self):
  3.         self.total = 0
  4.         self.num_rolls = 0
  5.         self.score_of_previous_frames = 0
  6.         self.should_add_next_roll_as_bonus = False
  7.         self.bonus_points = 0
  8.        
  9.         self.error = -1
  10.        
  11.     def put_into_error_state(self):
  12.         self.total = self.error
  13.    
  14.     def roll(self, pins):
  15.         self.num_rolls = self.num_rolls + 1
  16.        
  17.         if self.total == self.error:
  18.             return
  19.         if pins > 10:
  20.             self.put_into_error_state()
  21.         else:
  22.             self.total = self.total + pins
  23.            
  24.         if self.should_add_next_roll_as_bonus and not self.total == self.error:
  25.             self.bonus_points = self.bonus_points + pins
  26.             self.should_add_next_roll_as_bonus = False
  27.            
  28.         is_end_of_frame = self.num_rolls % 2 == 0 # TODO: make this work with strikes
  29.         if is_end_of_frame:
  30.             this_frame_score = self.total - self.score_of_previous_frames
  31.             if this_frame_score > 10:
  32.                 self.put_into_error_state()
  33.             elif this_frame_score == 10:
  34.                 self.should_add_next_roll_as_bonus = True
  35.             self.score_of_previous_frames = self.total
  36.    
  37.     def score(self):
  38.         if self.num_rolls != 20:
  39.             return self.error
  40.        
  41.         self.total = self.total + self.bonus_points
  42.         return self.total
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement