daily pastebin goal
25%
SHARE
TWEET

Bowling Game module

rajaramanathan Nov 8th, 2012 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class GameNotStarted(Exception):
  2.         pass
  3.        
  4. class GameNotFinished(Exception):
  5.         pass
  6.  
  7. class GameOver(Exception):
  8.         pass
  9.  
  10. class BowlingGameSetting(object):
  11.         MAX_ROLLS = 21
  12.         ROLL_PER_FRAME = 2
  13.         STRIKE_POINTS = 10
  14.         STRIKE_BOUNS_ROLLS = 2
  15.         SPARE_POINTS = 10
  16.         SPARE_BONUS_ROLLS = 1
  17.        
  18. class BowlingGame(object):
  19.         """ Bowling game """
  20.  
  21.         def __init__(self):
  22.                 """ Constructor """
  23.                 self.__rolls = []
  24.        
  25.         def play(self,pinsDown):
  26.                 """ update the scores based on the pins down during the roll """
  27.  
  28.                 if len(self.__rolls) >= BowlingGameSetting.MAX_ROLLS:
  29.                         raise GameOver
  30.                 self.__rolls.append(pinsDown)
  31.  
  32.         def __canScore(self):
  33.                 """ checks to see if we can score """
  34.                 if len(self.__rolls) == 0:
  35.                         raise GameNotStarted
  36.                 if len(self.__rolls) < BowlingGameSetting.MAX_ROLLS - 1:
  37.                         raise GameNotFinished
  38.  
  39.         def __getBonusRollScores(self,startingAt,nItems):
  40.                 """ total the scores of bonus rolls """
  41.                 itemCount = 0
  42.                 bonus = 0
  43.                 for item in self.__rolls[startingAt + 1:]:
  44.                         if itemCount >= nItems:
  45.                                 break
  46.                         bonus += item
  47.                         itemCount += 1
  48.                 return bonus
  49.                
  50.         def __getBonus(self,frameIndex,frame):
  51.                 """ calculates the bonus for the provided frame """
  52.                 #bonus for spare frame
  53.                 if sum(frame) == BowlingGameSetting.SPARE_POINTS:
  54.                         return self.__getBonusRollScores((frameIndex * BowlingGameSetting.ROLL_PER_FRAME) + 1,BowlingGameSetting.SPARE_BONUS_ROLLS)
  55.                 #bonus for every strike in the frame
  56.                 bonus = 0
  57.                 for index,roll in enumerate(frame):
  58.                         if roll == BowlingGameSetting.STRIKE_POINTS:
  59.                                 bonus += self.__getBonusRollScores((frameIndex * BowlingGameSetting.ROLL_PER_FRAME) + index,BowlingGameSetting.STRIKE_BOUNS_ROLLS)
  60.                 return bonus
  61.        
  62.         def score(self):
  63.                 """ calculates the  score """
  64.                 self.__canScore()
  65.                 #calculate the score
  66.                 score = 0
  67.                 for frameIndex,frame in enumerate(zip(*[iter(self.__rolls)]* BowlingGameSetting.ROLL_PER_FRAME)):  #split the rolls into frame.
  68.                         score += sum(frame) + self.__getBonus(frameIndex,frame)
  69.                 return score
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top