Advertisement
Guest User

ship_captain_crew.py

a guest
Mar 21st, 2014
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | None | 0 0
  1. from random import randint
  2.  
  3. class Player(object):
  4.     def __init__(self, name, hold):
  5.         self.name = name
  6.         self.hold_limit = hold
  7.         self.total_score = 0
  8.         self.score = 0
  9.        
  10.     def take_turn(self, printing):
  11.         self.score = 0
  12.         ship = 0
  13.         captain = 0
  14.         crew = 0
  15.         if printing:
  16.             print "\n" + self.name
  17.         for i in range(3):
  18.             num_dice = 5 - sum((ship, captain, crew))
  19.             dice = [randint(1, 6) for x in range(num_dice)]
  20.             if printing:
  21.                 print dice
  22.             if not ship and 6 in dice:
  23.                 ship = 1
  24.                 dice.remove(6)
  25.             if ship and not captain:
  26.                 if 5 in dice:
  27.                     captain = 1
  28.                     dice.remove(5)
  29.             if captain and not crew:
  30.                 if 4 in dice:
  31.                     crew = 1
  32.                     dice.remove(4)
  33.             if crew:
  34.                 cargo = sum(dice)
  35.                 if cargo >= self.hold_limit:
  36.                     self.score = cargo
  37.                     if printing:
  38.                         print "{} held with {}".format(self.name, self.score)
  39.                     return
  40.         if crew:
  41.             self.score = cargo
  42.             if printing:
  43.                 print "{} scored {} points".format(self.name, self.score)            
  44.  
  45.                
  46. def ship():
  47.     players = [Player("Re-Roller", 12), Player("Aggressive", 10),
  48.                     Player("Average", 9), Player("Conservative", 8),
  49.                     Player("Timid", 7), Player("Never-Roller", 2)]
  50.            
  51.     num_rounds = int(raw_input("How many rounds?"))
  52.     printing = True
  53.     if num_rounds > 50:
  54.         printing = False
  55.     for i in range(num_rounds):
  56.         for player in players:
  57.             player.take_turn(printing)
  58.             player.total_score += player.score
  59.         players = players[1:] + players[:1]
  60.    
  61.     print "\n"
  62.     for final in sorted(players, key=lambda x: x.total_score)[::-1]:
  63.         print "{:15} {}".format(final.name, final.total_score)
  64.        
  65.  
  66. if __name__ == "__main__":
  67.     ship()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement