fork(3) 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 set_value(self, newValue):
  62. '''Pig.set_value(str)
  63. Sets value attribute to new value.'''
  64. self.value = newValue
  65.  
  66. def is_pig_out(self, other):
  67. '''Pig.is_pig_out(Pig) -> bool
  68. Returns whether or not this is a Pig Out.'''
  69. results = (self.value, other.value)
  70. return 'left' in results and 'right' in results
  71.  
  72. def add(self, other):
  73. '''Pig.add(Pig) -> int
  74. Returns the total value of a roll.'''
  75. if self.is_pig_out(other): # if a Pig Out
  76. return 0
  77.  
  78. # For converting values to a number
  79. valueDict = {'left':0, 'right':0, 'back':5,
  80. 'feet':5, 'nose':10, 'ear':15}
  81.  
  82. # Add the scores
  83. score = valueDict[self.value] + valueDict[other.value]
  84.  
  85. # If double was rolled
  86. if self.value == other.value:
  87. # If Siders
  88. if self.value in ('left', 'right'):
  89. return 1
  90. else: # other doubles
  91. score *= 2 # double score
  92.  
  93. return score # return score
  94.  
  95. def get_combo(self, other):
  96. '''Pig.get_combo(Pig) -> str
  97. Returns the name of the combo formed by self and other.'''
  98. # Names of different results
  99. nameDict = {'back':'Razorback', 'feet':'Trotter', \
  100. 'nose':'Snouter', 'ear':'Leaning Jowler'}
  101.  
  102. names = [] # will contain any non-side names
  103. for pig in (self,other): # for each Pig
  104. # If not a side
  105. if pig.value not in ('left','right'):
  106. # Add name to list
  107. names.append( nameDict[pig.value] )
  108.  
  109. if len(names) == 0: # if 2 sides
  110. # If doubles
  111. if self.value == other.value:
  112. return 'Siders' # Siders
  113. return 'Pig Out' # else: Pig Out
  114.  
  115. if len(names) == 1: # if 1 side
  116. return names[0] # return that name
  117.  
  118. # Only cases left have 2 non-sides
  119. # If doubles
  120. if self.value == other.value:
  121. # Double the name
  122. return 'Double ' + names[0]
  123. return 'Mixed Combo' # else: Mixed Combo
  124.  
  125. class Player:
  126. '''Player in the Pass the Pigs game'''
  127.  
  128. def __init__(self, name):
  129. '''Player(str) -> Player
  130. Creates a Player with given name and score 0.'''
  131. self.name = name
  132. self.score = 0
  133.  
  134. def __str__(self):
  135. '''str(Player) -> str
  136. String for printing'''
  137. return self.name + " has " + str(self.score) + " points"
  138.  
  139. def get_name(self):
  140. '''Player.get_name() -> int
  141. Returns name attribute.'''
  142. return self.name
  143.  
  144. def get_score(self):
  145. '''Player.get_score() -> int
  146. Returns score attribute.'''
  147. return self.score
  148.  
  149. def get_roll_choice(self):
  150. '''Player.get_roll_choice() -> bool
  151. Returns True if player wants to keep rolling. False otherwise.'''
  152. response = input('Enter STOP to bank points or anything else to keep going: ')
  153. return response != 'STOP'
  154.  
  155. def play_turn(self):
  156. '''Player.play_turn()
  157. Has player play one full turn.'''
  158. pig1 = Pig() # create Pigs
  159. pig2 = Pig()
  160.  
  161. pig1.roll() # roll Pigs
  162. pig2.roll()
  163. rollPoints = pig1.add(pig2) # value of roll
  164. combo = pig1.get_combo(pig2) # roll combo
  165. pot = rollPoints # set pot to value of first roll
  166.  
  167. print('You rolled ' + pig1.get_value() + ' and ' + pig2.get_value())
  168. print('You scored ' + str(rollPoints) + ' points with a ' + combo)
  169. print('You have ' + str(pot) + ' points in the pot')
  170.  
  171. while not pig1.is_pig_out(pig2) and self.get_roll_choice():
  172. pig1.roll() # roll Pigs
  173. pig2.roll()
  174. rollPoints = pig1.add(pig2) # value of roll
  175. combo = pig1.get_combo(pig2) # roll combo
  176. pot += rollPoints # add roll
  177.  
  178. print('You rolled ' + pig1.get_value() + ' and ' + pig2.get_value())
  179. print('You scored ' + str(rollPoints) + ' points with a ' + combo)
  180. print('You have ' + str(pot) + ' points in the pot')
  181.  
  182. if pig1.is_pig_out(pig2): # if player pigged out
  183. print('PIG OUT!')
  184. else:
  185. print('You bank ' + str(pot) + ' points')
  186. self.score += pot # bank the pot
  187. class PlayerSet:
  188. '''A group of players to play
  189. the Pass The Pigs game.'''
  190.  
  191. def __init__(self,playerCount):
  192. '''PlayerSet(int) -> PlayerSet
  193. Contstructs a group of players,
  194. given how many there are and asks the
  195. name of each player.'''
  196. self.playerCount = playerCount # initialize count of players attribute
  197. self.playersList = [] # initialize list of players attrbute
  198. for i in range(1,self.playerCount+1): # for every i between 1 and plyerCount
  199. name = input('Player ' + str(i) + ', please enter your name:') # ask player i's name
  200. player = Player(name) # create a player with that name
  201. self.playersList.append(player) # add the player to the list of players
  202. self.currentPlayer = self.playersList[0] # initialize current player attribute
  203.  
  204. def __str__(self):
  205. '''print(PlayerSet) -> str
  206. Returns all of the player's
  207. name and score.'''
  208. string='' # will contain output
  209. for player in self.playersList: # for every player in the list of players
  210. string+= player.get_name() + ' has ' + str(player.get_score()) + ' points \n' # add a string saying the player's name and current score
  211. return string # return the string
  212.  
  213. def get_curr_name(self):
  214. '''PlayerSet.get_curr_name() -> str
  215. Returns the name of the current player.'''
  216. return self.currentPlayer.get_name() # return the name of the player
  217.  
  218. def go_to_next_player(self):
  219. '''PlayerSet.go_to_next_player()
  220. Moves the current player to the next
  221. player.'''
  222. index = 0 # will contain index of the current player
  223. for i in range(len(self.playersList)): # for every index in list of players
  224. if self.playersList[i] == self.currentPlayer: # if that index is correct
  225. index = i # assign index to the desired index
  226.  
  227. index += 1 # add 1 to the desired index, we want to move to next player
  228. index %= self.playerCount # if the number of the player goes over, take the residue mod playerCount
  229. self.currentPlayer=self.playersList[index] # make the current player equal to the new index of the list of players
  230.  
  231. def is_curr_winner(self):
  232. '''PlayerSet.is_curr_winner() -> bool
  233. Returns if the current player has won.'''
  234. if self.currentPlayer.get_score() >= 100: # if the current player has won
  235. return True
  236. else: # otherwise
  237. return False
  238.  
  239. def take_turn(self):
  240. '''PlayerSet.take_turn()
  241. Takes the player's turn.'''
  242. self.currentPlayer.play_turn() # use player's method - play_turn() which plays a turn
  243.  
  244. def play_game(self):
  245. '''PlayerSet.play_game()
  246. Plays the real Pass The Pigs game! Woohoo!'''
  247. while self.is_curr_winner() == False: # until a player has won
  248. print(self) # print the status of the players
  249. print(self.get_curr_name() + ", it's your turn") # print whose turn it is
  250. self.take_turn() # take the player's turn
  251. if self.is_curr_winner() == True: # if the current player has won
  252. print(self) # print the status of the players
  253. return self.get_curr_name() + ' has won!' # return which player has won
  254. self.go_to_next_player() # go to the next player
  255.  
  256.  
Success #stdin #stdout 0.07s 12336KB
stdin
Standard input is empty
stdout
Standard output is empty