fork download
  1. import random
  2.  
  3. class Die:
  4. '''Represents a die
  5. sides: list of results the die can take
  6. value: current result showing on the die'''
  7.  
  8. def __init__(self, sides):
  9. '''Die(sides) -> Die
  10. Constructs a Die object with the faces in list sides. value
  11. attribute is set to sides[0].'''
  12. self.sides = sides # list of possible die results
  13. self.value = sides[0] # current result showing
  14.  
  15. def __str__(self):
  16. '''str(Die) -> str
  17. Returns a string like:
  18. 'A die with sides [1, 2, 3, 4, 5, 6] and value 1' '''
  19. output = 'A die with sides ' # start of string
  20. output += str( self.sides ) # add sides
  21. output += ' and value ' # more text
  22. output += str( self.value ) # add value
  23. return output # return, not print
  24.  
  25. def get_value(self):
  26. '''Die.get_value() -> value
  27. Returns the current value of the Die.'''
  28. return self.value
  29.  
  30. def roll(self):
  31. '''Die.roll() -> obj
  32. Sets value to a random side and returns it.'''
  33. # Pick a random index
  34. randomIndex = random.randrange( len( self.sides ) )
  35. # Get the side
  36. self.value = self.sides[ randomIndex ]
  37. return self.value # return new value
  38.  
  39. def add(self, otherDie):
  40. '''Die.add(Die) -> obj
  41. Returns the sum of value attributes of two Die objects.'''
  42. # Return sum
  43. return self.value + otherDie.value
  44.  
  45. class Pig(Die):
  46. '''Creates a Pig object for Pass the Pigs'''
  47.  
  48. def __init__(self):
  49. '''Pig() -> Pig
  50. Constructs a Pig die.'''
  51. # Approximate proportion of each result
  52. sides = ['left']*59 + ['right']*51 + ['back']*38 \
  53. + ['feet']*15 +['nose']*5+['ear']*1
  54. Die.__init__(self, sides) # use Die constructor
  55.  
  56. def __str__(self):
  57. '''str(Pig) -> str
  58. Returns the current value of the Pig.'''
  59. return self.value
  60.  
  61. def get_value(self):
  62. '''Pig.get_value() -> str
  63. Returns the current value of the Pig'''
  64. return self.value
  65.  
  66. def set_value(self, newValue):
  67. '''Pig.set_value(str)
  68. Sets value attribute to new value.'''
  69. self.value = newValue
  70.  
  71. def is_pig_out(self, other):
  72. '''Pig.is_pig_out(Pig) -> bool
  73. Returns whether or not this is a Pig Out.'''
  74. results = (self.value, other.value)
  75. return 'left' in results and 'right' in results
  76.  
  77. def add(self, other):
  78. '''Pig.add(Pig) -> int
  79. Returns the total value of a roll.'''
  80. if self.is_pig_out(other): # if a Pig Out
  81. return 0
  82.  
  83. # For converting values to a number
  84. valueDict = {'left':0, 'right':0, 'back':5,
  85. 'feet':5, 'nose':10, 'ear':15}
  86.  
  87. # Add the scores
  88. score = valueDict[self.value] + valueDict[other.value]
  89.  
  90. # If double was rolled
  91. if self.value == other.value:
  92. # If Siders
  93. if self.value in ('left', 'right'):
  94. return 1
  95. else: # other doubles
  96. score *= 2 # double score
  97.  
  98. return score # return score
  99.  
  100. def get_combo(self, other):
  101. '''Pig.get_combo(Pig) -> str
  102. Returns the name of the combo formed by self and other.'''
  103. # Names of different results
  104. nameDict = {'back':'Razorback', 'feet':'Trotter', \
  105. 'nose':'Snouter', 'ear':'Leaning Jowler'}
  106.  
  107. names = [] # will contain any non-side names
  108. for pig in (self,other): # for each Pig
  109. # If not a side
  110. if pig.value not in ('left','right'):
  111. # Add name to list
  112. names.append( nameDict[pig.value] )
  113.  
  114. if len(names) == 0: # if 2 sides
  115. # If doubles
  116. if self.value == other.value:
  117. return 'Siders' # Siders
  118. return 'Pig Out' # else: Pig Out
  119.  
  120. if len(names) == 1: # if 1 side
  121. return names[0] # return that name
  122.  
  123. # Only cases left have 2 non-sides
  124. # If doubles
  125. if self.value == other.value:
  126. # Double the name
  127. return 'Double ' + names[0]
  128. return 'Mixed Combo' # else: Mixed Combo
  129.  
  130. class Player(Die):
  131. '''Player in the Pass the Pigs game'''
  132.  
  133. def __init__(self, name, score=0):
  134. '''Player(str) -> Player
  135. Sets name for player; score defaults to 0.'''
  136. self.__name = name
  137. self.__score = score
  138.  
  139. def __str__(self):
  140. '''str(Player) -> str
  141. String for printing'''
  142. return self.__name + " has " + str(self.__score) + " points"
  143.  
  144. def get_name(self):
  145. '''Player.get_name() -> str
  146. Returns name attribute.'''
  147. return self.__name
  148.  
  149. def get_score(self):
  150. '''Player.get_score() -> int
  151. Returns score attribute.'''
  152. return self.__score
  153.  
  154. def add_to_score(self, points):
  155. '''Player.add_to_score(int)
  156. Updates score by adding points.'''
  157. self.__score += points
  158.  
  159. def has_won(self):
  160. '''Player.has_won() -> bool
  161. Determines if player has won (score 100 or more).'''
  162. return self.__score >= 100
  163.  
  164. def stop_turn(self, points):
  165. '''Player.stop_turn(int) -> bool
  166. Allows the player to decide if he/she is going to stop.
  167. Returns True if player wants to stop.'''
  168. choice = input('Enter STOP to bank ' + str(points) + ' points, or anything else to keep going: ')
  169. return choice == 'STOP'
  170.  
  171. def take_turn(self):
  172. '''Player.take_turn()
  173. Has player take one full turn.'''
  174. # Initialization
  175. pig1 = Pig() # create pigs
  176. pig2 = Pig()
  177. total = 0 # running total of points this turn
  178. stop = False # control while loop
  179.  
  180. # Warn player it's the start of their turn
  181. input(self.__name + ", press ENTER to start your turn")
  182.  
  183. while not stop: # while turn continues
  184. pig1.roll() # roll pigs
  185. top2 = pig2.roll() # save result
  186. # Print pigs
  187. print('You rolled ' + str(pig1) + ' and ' + top2)
  188. combo = pig1.get_combo(pig2) # name of combo
  189. value = pig1.add(pig2) # value of combo
  190. # Print result
  191. print('You scored ' + str(value) + ' with a ' + combo)
  192.  
  193. if combo == 'Pig Out': # if Pig Out
  194. stop = True # end the turn
  195. else:
  196. total += value # update running total
  197. if self.stop_turn(total): # ask if they want to STOP
  198. stop = True # stop the turn
  199. self.add_to_score(total) # bank these points
  200. # part A
  201.  
  202.  
  203. class PlayerSet(Pig):
  204.  
  205. def __init__(self, integer, name): # Contructor contains an integer attribute, and a list player attribute to store all players in a list.
  206.  
  207. self.integer = integer
  208.  
  209. self.name = input()
  210.  
  211. pList = []
  212.  
  213. for element in self.integer:
  214.  
  215. if element == 1: # If only one player
  216.  
  217. return "Player, please enter your name: " + self.name
  218.  
  219. pList.append(name) # Append the entered name to a list
  220.  
  221. if element > 1: # If more than one player
  222.  
  223. return "Player, please enter your name: " + self.name
  224.  
  225. pList.append(name) # Append the entered name(s) to a list
  226.  
  227.  
  228. def __str__(self):
  229. # Display the number of players playing as well as the list of those who are playing
  230. return 'The number of players playing is' + str(self.integer) + ' and here are the players who are playing: ' + str(pList) #
  231.  
  232. def get_curr_name(self):
  233.  
  234. return str(self._name) # Display current player
  235.  
  236. def go_to_next_player(self, pNum): # pNum is used to continue to next player
  237.  
  238. '''PlayerSet.go_to_next_player()
  239. Goes onto the next player.'''
  240.  
  241. self.pNum = pNum
  242.  
  243. self._name = (self.integer + 1) % self.pNum
  244.  
  245. return self._name
  246.  
  247.  
  248. def is_curr_winner(self):
  249.  
  250. '''PlayerSet.is_curr_winner()
  251. Determines if the current player is a winner.'''
  252.  
  253. if self._score >= 100:
  254.  
  255. return self._name + "You have won!"
  256.  
  257. elif self._score < 100:
  258.  
  259. return self._name + "You do not have 100 points."
  260.  
  261.  
  262.  
  263. def take_turn():
  264.  
  265. '''PlayerSet.take_turn()
  266. Has player take one full turn.'''
  267. # Initialization
  268. pig1 = Pig() # create pigs
  269. pig2 = Pig()
  270. total = 0 # running total of points this turn
  271. stop = False # control while loop
  272.  
  273. # Warn player it's the start of their turn
  274. input(self.__name + ", press ENTER to start your turn")
  275.  
  276. while not stop: # while turn continues
  277. pig1.roll() # roll pigs
  278. top2 = pig2.roll() # save result
  279. # Print pigs
  280. print('You rolled ' + str(pig1) + ' and ' + top2)
  281. combo = pig1.get_combo(pig2) # name of combo
  282. value = pig1.add(pig2) # value of combo
  283. # Print result
  284. print('You scored ' + str(value) + ' with a ' + combo)
  285.  
  286. if combo == 'Pig Out': # if Pig Out
  287. stop = True # end the turn
  288. else:
  289. total += value # update running total
  290. if self.stop_turn(total): # ask if they want to STOP
  291. stop = True # stop the turn
  292. self.add_to_score(total) # bank these points
  293.  
  294.  
  295. # part B
  296.  
  297. def play_pigs(self):
  298.  
  299. self.__init__() # Verify players
  300.  
  301. for player in self.integer:
  302.  
  303. self.get_curr_name() # Get the current player's name
  304.  
  305. self.take_turn() # Have current player take a turn
  306.  
  307. self.is_pig_out() # See if player has a pig-out
  308.  
  309. self.get_combo() # Calculate combo
  310.  
  311. self.add() # Add points to bank
  312.  
  313. self.is_curr_winner() # See if current player is a winner
  314.  
  315. self.go_to_next_player() # go to next player
  316.  
  317. self.play_pigs() # Repeat process
  318.  
Success #stdin #stdout 0.16s 12480KB
stdin
Standard input is empty
stdout
Standard output is empty