import random
class Die:
'''Represents a die
sides: list of results the die can take
value: current result showing on the die'''
def __init__(self, sides):
'''Die(sides) -> Die
Constructs a Die object with the faces in list sides. value
attribute is set to sides[0].'''
self.sides = sides # list of possible die results
self.value = sides[0] # current result showing
def __str__(self):
'''str(Die) -> str
Returns a string like:
'A die with sides [1, 2, 3, 4, 5, 6] and value 1' '''
output = 'A die with sides ' # start of string
output += str( self.sides ) # add sides
output += ' and value ' # more text
output += str( self.value ) # add value
return output # return, not print
def get_value(self):
'''Die.get_value() -> value
Returns the current value of the Die.'''
return self.value
def roll(self):
'''Die.roll() -> obj
Sets value to a random side and returns it.'''
# Pick a random index
randomIndex = random.randrange( len( self.sides ) )
# Get the side
self.value = self.sides[ randomIndex ]
return self.value # return new value
def add(self, otherDie):
'''Die.add(Die) -> obj
Returns the sum of value attributes of two Die objects.'''
# Return sum
return self.value + otherDie.value
class Pig(Die):
'''Creates a Pig object for Pass the Pigs'''
def __init__(self):
'''Pig() -> Pig
Constructs a Pig die.'''
# Approximate proportion of each result
sides = ['left']*59 + ['right']*51 + ['back']*38 \
+ ['feet']*15 +['nose']*5+['ear']*1
Die.__init__(self, sides) # use Die constructor
def __str__(self):
'''str(Pig) -> str
Returns the current value of the Pig.'''
return self.value
def set_value(self, newValue):
'''Pig.set_value(str)
Sets value attribute to new value.'''
self.value = newValue
def is_pig_out(self, other):
'''Pig.is_pig_out(Pig) -> bool
Returns whether or not this is a Pig Out.'''
results = (self.value, other.value)
return 'left' in results and 'right' in results
def add(self, other):
'''Pig.add(Pig) -> int
Returns the total value of a roll.'''
if self.is_pig_out(other): # if a Pig Out
return 0
# For converting values to a number
valueDict = {'left':0, 'right':0, 'back':5,
'feet':5, 'nose':10, 'ear':15}
# Add the scores
score = valueDict[self.value] + valueDict[other.value]
# If double was rolled
if self.value == other.value:
# If Siders
if self.value in ('left', 'right'):
return 1
else: # other doubles
score *= 2 # double score
return score # return score
def get_combo(self, other):
'''Pig.get_combo(Pig) -> str
Returns the name of the combo formed by self and other.'''
# Names of different results
nameDict = {'back':'Razorback', 'feet':'Trotter', \
'nose':'Snouter', 'ear':'Leaning Jowler'}
names = [] # will contain any non-side names
for pig in (self,other): # for each Pig
# If not a side
if pig.value not in ('left','right'):
# Add name to list
names.append( nameDict[pig.value] )
if len(names) == 0: # if 2 sides
# If doubles
if self.value == other.value:
return 'Siders' # Siders
return 'Pig Out' # else: Pig Out
if len(names) == 1: # if 1 side
return names[0] # return that name
# Only cases left have 2 non-sides
# If doubles
if self.value == other.value:
# Double the name
return 'Double ' + names[0]
return 'Mixed Combo' # else: Mixed Combo
class Player:
'''Player in the Pass the Pigs game'''
def __init__(self, name):
'''Player(str) -> Player
Creates a Player with given name and score 0.'''
self.name = name
self.score = 0
def __str__(self):
'''str(Player) -> str
String for printing'''
return self.name + " has " + str(self.score) + " points"
def get_name(self):
'''Player.get_name() -> int
Returns name attribute.'''
return self.name
def get_score(self):
'''Player.get_score() -> int
Returns score attribute.'''
return self.score
def get_roll_choice(self):
'''Player.get_roll_choice() -> bool
Returns True if player wants to keep rolling. False otherwise.'''
response = input('Enter STOP to bank points or anything else to keep going: ')
return response != 'STOP'
def play_turn(self):
'''Player.play_turn()
Has player play one full turn.'''
pig1 = Pig() # create Pigs
pig2 = Pig()
pig1.roll() # roll Pigs
pig2.roll()
rollPoints = pig1.add(pig2) # value of roll
combo = pig1.get_combo(pig2) # roll combo
pot = rollPoints # set pot to value of first roll
print('You rolled ' + pig1.get_value() + ' and ' + pig2.get_value())
print('You scored ' + str(rollPoints) + ' points with a ' + combo)
print('You have ' + str(pot) + ' points in the pot')
while not pig1.is_pig_out(pig2) and self.get_roll_choice():
pig1.roll() # roll Pigs
pig2.roll()
rollPoints = pig1.add(pig2) # value of roll
combo = pig1.get_combo(pig2) # roll combo
pot += rollPoints # add roll
print('You rolled ' + pig1.get_value() + ' and ' + pig2.get_value())
print('You scored ' + str(rollPoints) + ' points with a ' + combo)
print('You have ' + str(pot) + ' points in the pot')
if pig1.is_pig_out(pig2): # if player pigged out
print('PIG OUT!')
else:
print('You bank ' + str(pot) + ' points')
self.score += pot # bank the pot
class PlayerSet:
'''A group of players to play
the Pass The Pigs game.'''
def __init__(self,playerCount):
'''PlayerSet(int) -> PlayerSet
Contstructs a group of players,
given how many there are and asks the
name of each player.'''
self.playerCount = playerCount # initialize count of players attribute
self.playersList = [] # initialize list of players attrbute
for i in range(1,self.playerCount+1): # for every i between 1 and plyerCount
name = input('Player ' + str(i) + ', please enter your name:') # ask player i's name
player = Player(name) # create a player with that name
self.playersList.append(player) # add the player to the list of players
self.currentPlayer = self.playersList[0] # initialize current player attribute
def __str__(self):
'''print(PlayerSet) -> str
Returns all of the player's
name and score.'''
string='' # will contain output
for player in self.playersList: # for every player in the list of players
string+= player.get_name() + ' has ' + str(player.get_score()) + ' points \n' # add a string saying the player's name and current score
return string # return the string
def get_curr_name(self):
'''PlayerSet.get_curr_name() -> str
Returns the name of the current player.'''
return self.currentPlayer.get_name() # return the name of the player
def go_to_next_player(self):
'''PlayerSet.go_to_next_player()
Moves the current player to the next
player.'''
index = 0 # will contain index of the current player
for i in range(len(self.playersList)): # for every index in list of players
if self.playersList[i] == self.currentPlayer: # if that index is correct
index = i # assign index to the desired index
index += 1 # add 1 to the desired index, we want to move to next player
index %= self.playerCount # if the number of the player goes over, take the residue mod playerCount
self.currentPlayer=self.playersList[index] # make the current player equal to the new index of the list of players
def is_curr_winner(self):
'''PlayerSet.is_curr_winner() -> bool
Returns if the current player has won.'''
if self.currentPlayer.get_score() >= 100: # if the current player has won
return True
else: # otherwise
return False
def take_turn(self):
'''PlayerSet.take_turn()
Takes the player's turn.'''
self.currentPlayer.play_turn() # use player's method - play_turn() which plays a turn
def play_game(self):
'''PlayerSet.play_game()
Plays the real Pass The Pigs game! Woohoo!'''
while self.is_curr_winner() == False: # until a player has won
print(self) # print the status of the players
print(self.get_curr_name() + ", it's your turn") # print whose turn it is
self.take_turn() # take the player's turn
if self.is_curr_winner() == True: # if the current player has won
print(self) # print the status of the players
return self.get_curr_name() + ' has won!' # return which player has won
self.go_to_next_player() # go to the next player