Advertisement
Atyeo

Owzthat v0.5

Dec 31st, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.99 KB | None | 0 0
  1. '''
  2. Owzthat v0.5
  3. Simulates the old fasioned cricket based dice game known as Owzthat!
  4. http://en.wikipedia.org/wiki/Owzthat
  5.  
  6. Future developments
  7. Make it more realistic and have two players in at once alternating strike as in real cricket
  8. Add more user interaction
  9. Add gui
  10. Add weighting based on skill of batter
  11. '''
  12.  
  13. import random
  14.  
  15. class Player(object):
  16.     '''The team members!'''
  17.     def __init__(self, name):
  18.         self.name = name
  19.         self.runs = 0
  20.         self.balls = 0
  21.         self.fours = 0
  22.         self.sixes = 0
  23.         self.dismissal = ''
  24.        
  25.     def add_runs(self, runs):
  26.         self.runs += runs
  27.  
  28.     def add_balls(self, balls):
  29.         self.balls += balls
  30.  
  31.     def add_fours(self, fours):
  32.         self.fours += fours
  33.  
  34.     def add_sixes(self, sixes):
  35.         self.sixes += sixes
  36.        
  37.     def add_dismissal(self, dismissal):
  38.         self.dismissal = dismissal
  39.  
  40. players = ['Cook', 'Prior', 'Pietersen', 'Bell', 'Trott', 'Compton', 'Swann', 'Patel', 'Broad', 'Anderson', 'Panesar']
  41.  
  42. Batters = [Player(name) for name in players]
  43.  
  44. def bowl():
  45.     '''Simulates a single bowl
  46.    Either returns the runs scored or the result of the appeal'''
  47.     first_dice = (1, 2, 3, 4, 6, 'Owzthat!')
  48.     ball = first_dice[random.randint(0,5)]
  49.     if  ball in [1, 2, 3, 4, 6]:
  50.         print 'Nice hit! Thats %s runs.' % ball
  51.         return ball
  52.     else:
  53.         print 'Owzthat! It\'s going to appeal!'
  54.         second_dice = ('Bowled!', 'Caught!', 'Not Out!', 'Stumped!', 'L.B.W!', 'No Ball!')
  55.         appeal = second_dice[random.randint(0,5)]
  56.         print 'The decision is...%s' % appeal
  57.         return appeal
  58.  
  59. def scorecard(extras, total_team_score, total_balls):
  60.     '''Produces a standardised scorecard'''
  61.     print '%-20s %-7s %-7s %-7s %-7s' % (' ', 'Runs', 'Balls', 'Fours', 'Sixes')
  62.     for player in Batters:
  63.         print '%-10s %-10s %-7d %-7d %-7d %-7d' % (player.name, player.dismissal,
  64.                                                player.runs, player.balls,
  65.                                                player.fours, player.sixes)
  66.     print '%-21s %-7d' % ('Extras', extras)
  67.     print '%-21s %-7d %-7d' % ('Total', total_team_score, total_balls)
  68.    
  69. def main():
  70.     player_number = 0 # initializes player index
  71.     total_team_score = 0 # stores total runs scored
  72.     total_balls = 0 # stores total number of balls bowled
  73.     extras = 0 # stores total extras scored as a result of 'no balls'
  74.     while player_number < 10:
  75.         ball = bowl()
  76.         total_balls += 1 # adds one to to the number of balls bowled in match
  77.         Batters[player_number].add_balls(1) # adds one to the number of balls bowled to batter
  78.         if ball in ['Bowled!', 'Caught!', 'Stumped!', 'L.B.W!']: # if batter is out
  79.             player_number += 1 # moves to next batter
  80.             print Batters[player_number].name + ' scores %s' %Batters[player_number].runs
  81.             print 'England are now %s for %s' %(total_team_score, player_number)
  82.             Batters[player_number].add_dismissal(ball) # adds the type of dismissal to the batter
  83.         elif ball == 'No Ball!': # if a no ball is bowled
  84.             extras += 1 # add one to the extras
  85.             total_team_score += 1 # add one to the total runs score
  86.         elif ball == 'Not Out!': # if it's not out
  87.             pass # don't do anything
  88.         else: # if runs are scored
  89.             Batters[player_number].add_runs(ball) # add runs to batters total score
  90.             total_team_score += ball # add runs to total runs scored
  91.             if ball == 4: # if a four is scored
  92.                 Batters[player_number].add_fours(1) # add one to the number of fours scored by batter
  93.             elif ball == 6: # if a six is scored
  94.                 Batters[player_number].add_sixes(1) # add one to the number of sixes scored by batter
  95.         print Batters[player_number].name + ' now has %s' %Batters[player_number].runs
  96.     scorecard(extras, total_team_score, total_balls) # print out scorecard at end of game
  97.    
  98. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement