Write a program to play the game Pass the Pigs (pigs.py) In python . Pass the Pi
ID: 3825930 • Letter: W
Question
Write a program to play the game Pass the Pigs (pigs.py) In python. Pass the Pigs is a popular games of chance for two or more players based on a dice game called Pig.
The rules of the game are straightforward. A player tosses two toy pigs, each of which has a dot on one side (see image below). Each pig can land in one of six positions:
On its side
Side with dot down
Side with dot up
Razorback (on its back)
Trotter (on all four feet)
Snouter (balanced on front two feet and its snout)
Leaning jowler (balanced on one foot, snout, and ear)
Note: This is extremely rare! See the table of position frequencies below.
Here is an example of a Trotter (left pig) and a Snouter (right pig):
(image from https://upload.wikimedia.org/wikipedia/commons/thumb/b/b8/Pass_the_pigs_dice.jpg/600px-Pass_the_pigs_dice.jpg)
After the pigs have come to rest, the player gains or loses points based on the way the pigs land:
Double (both pigs in the same position)
Sider (either both with the spot facing upward or both with the spot facing downward): 1 point
Double Razorback: 20 points
Double Trotter: 20 points
Double Snouter: 40 points
Double Leaning Jowler: 60 points
Pig Out: 0 Points!! If both pigs are lying on their sides, one with the spot facing upwards and one with the spot facing downwards the score for that turn is reset to 0 and the turn changes to the next player
Mixed Combo: A combination not mentioned above is the sum of the single pigs' scores. Single pigs' scores:
The pig is lying on its side: 0 points
Razorback: 5 points
Trotter: 5 points
Snouter: 10 points
Leaning Jowler: 15 points
As an example, in the image above, the pig on left is in the Trotter position and the pig on the right is in the Snouter position. This throw of the pigs is worth 5 + 10 = 15 points.
Note: While there are a few more scoring combinations in the official Pass the Pigs game, we will only use the above combinations for simplicity.
Each player's turn continues until the player decides to stop their turn (accumulating points), or rolls a Pig Out. The player's score is updated and the turn is passed to the next player. The game ends when a player reaches a predetermined number of points (usually 100).
Program Details
Write a program that implements Pass the Pigs for two players according to the above rules. Your program will prompt the user for the total number of points representing a winning score. The winning score should be an integer greater than 0 and less than or equal to 100. Your program should validate the entered winning score is valid.
At a minimum, your solution must contain the functions below. These functions will help you get started!
(5 pts) display_game_rules(): Prints out the rules of Pass the Pigs.
(10 pts) get_valid_winning_score(): Prompts the players (users) for the integer number of points to play until. The validated integer number of points is returned. Calls is_valid_winning_score(score) to determine if the user-entered number of points is valid.
(5 pts) is_valid_winning_score(score): Checks to see if the entered winning score is an integer > 0 and <= 100. This function is a predicate function:
If the wager is in the range [1, 100] inclusive, then True is returned; otherwise False is returned.
(10 pts) roll_pig(): Rolls one pig. This function should randomly generate a pig position using the frequencies listed in the table below. Returns a string representing the position of the rolled pig, one of:
"SIDE" (for side without dot)
"SIDE-D" (for side with dot)
"RZR" (for razorback)
"TROT" (for trotter)
"SNOUT" (for snouter)
"LEAN" (for leaning jowler)
(10 pts) determine_roll_result(pig1, pig2): Determines the roll result based on the two pigs' positions and returns a string representing the roll result, one of:
"SIDER" (for both pigs on their side with dots facing the same direction)
"DBL-RAZR" (for a double razorback)
"DBL-TROT" (for a double trotter)
"DBL-SNOUT" (for a double snouter)
"DBL-LEAN" (for a double leaning jowler)
"PIGOUT" (for both pigs on their side, one with dot up one with dot down)
"MIXED" (for a mixed combo)
(10 pts) calculate_total_roll_points(pig1, pig2, roll_result): Determines the total points rolled based on the both pigs' positions (roll_result e.g. "SIDER", "DBL-RAZR", etc.) and returns the total points.
(30 pts) main() and the definition of additional functions beyond than the ones listed above: Call the above functions in order to play the game. Break the problem into tasks and build your solution accordingly. You will be graded on your approach. Ask the instructor or your TA for help with this!
Note: Pass the Pigs should be continually played until the players want to quit the game.
Relative Pig Position Frequencies
Use random integer generation (from the random module) and the "Integer percentage" column in the table below to "roll" a pig according to a pig's relative frequencies. Data in the table below are from a Journal of Statistics Education paper by Kern, JC (2006), "Pig Data and Bayesian Inference on Multinomial Probabilities."
Chatter Messages
As the game progresses, print various "chatter" messages based on the number of rolls taken so far by the player, the current score, and whether or not the player just won his/her roll such as, "Sorry, you rolled a Pig Out!", or "Oh, you're going for broke, huh?", or "Aw c'mon, take a chance!", or "You're up big, now is the time to cash in your points!"
Use function(s) to do this!
Position Percentage Integer Percentage Side (no dot) 34.9% 35% Side (dot) 30.2% 30% Razorback 22.4% 22% Trotter 8.8% 9% Snouter 3.0% 3% Leaning jowler 0.61% 1%Explanation / Answer
import random
#pig class
class Pig:
#to roll the ping with given probabilities
def roll_pig(self):
position=['SIDE'] * 35 + ['SIDE-D'] * 30 + ['RZR'] * 22 + ['TROT'] * 9 + ['SNOUT'] * 3 + ['LEAN']* 1
symbol=random.choice(position)
return symbol
#player class
class Player:
#variables
score=0;
pig1=None;
pig2=None;
pig1_pos=None;
pig2_pos=None;
single_pig_dic_score={"SIDE":0,"SIDE-D":0,"RZR":5,"TROT":5,"SNOUT":10,"LEAN":15}
double_pig_dic_score={"SIDER":1,"DBL-RAZR":20,"DBL-TROT":20,"DBL-SNOUT":40,"DBL-LEAN":60,"PIGOUT":0}
#constructer for pig
def __init__(self):
self.pig1=Pig()
self.pig2=Pig()
self.score=0
#to get the score
def get_score(self):
return self.score;
#to add the score
def addScore(self,points):
self.score=self.score+points
#to check player win or not
def is_win(self):
return self.score==100
#to check wether given score is valid or not
def is_valid_winning_score(self,points):
return (self.score+points>0 and self.score+points<=100)
#to roll both pigs
def roll_pig(self):
self.pig1_pos=self.pig1.roll_pig()
self.pig2_pos=self.pig2.roll_pig()
print "Pig1 : ",self.pig1_pos
print "Pig2 : ",self.pig2_pos
#to get both pig rolling result
def roll_result(self):
if((self.pig1_pos=='SIDE' and self.pig2_pos=='SIDE') or (self.pig1_pos=='SIDE-D' and self.pig2_pos=='SIDE-D')):
print 'SIDER'
return 'SIDER'
elif(self.pig1_pos=='RZR' and self.pig2_pos=='RZR'):
print 'DBL-RAZR'
return 'DBL-RAZR'
elif(self.pig1_pos=='TROT' and self.pig2_pos=='TROT'):
print 'DBL-TROT'
return 'DBL-TROT'
elif(self.pig1_pos=='SNOUT' and self.pig2_pos=='SNOUT'):
print 'DBL-SNOUT'
return 'DBL-SNOUT'
elif(self.pig1_pos=='LEAN' and self.pig2_pos=='LEAN'):
print 'DBL-LEAN'
return 'DBL-LEAN'
elif((self.pig1_pos=='SIDE' and self.pig2_pos=='SIDE-D') or (self.pig1_pos=='SIDE-D' and self.pig2_pos=='SIDE')):
print 'PIGOUT'
return 'PIGOUT'
else:
print 'MIXED'
return 'MIXED'
#to determine score given by both results
def determine_roll_result(self):
roll_result=self.roll_result()
if(roll_result=='MIXED'):
return self.single_pig_dic_score[self.pig1_pos]+self.single_pig_dic_score[self.pig2_pos]
else:
return self.double_pig_dic_score[roll_result]
#game class
class PassThePig:
#game rules
def game_rules(self):
print "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"
print "Ruels:"
print
print "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"
print '''
Scoring:
Sigle Pig
------------------------------------------------------------
Symbol | Meaning | Score
------------------------------------------------------------
SIDE | side without dot | 0
SIDE-D | side with dot | 0
RZR | razorback | 5
TROT | trotter | 5
SNOUT | snouter | 10
LEAN | leaning jowler | 15
'''
print '''
Double Pig
--------------------------------------------------------------
Symbol | Meaning | Score
--------------------------------------------------------------
SIDER | Sider | 1
DBL-RZR | Double Razorback | 20
DBL-TROT | Double Trotter | 20
DBL-SNOUT | Double Snouter | 40
DBL-LEAN | Double Leaning Jowler | 60
PigOut | Pig Out | 0
Mixed | Mixed Combo | sum of pig positions
'''
print
print
print
print '''
Both pigs
------------------------------------------------------------------------
Sider - The pigs are on their sides, either both with
the spot facing upward or both with the
spotfacing downward
- 1 Point
Double Razorback - The pigs are both lying on their backs
- 20 Points
Double Trotter - The pigs are both standing upright
- 20 Points
Double Snouter - The pigs are both leaning on their snouts
- 40 Points
Double Leaning Jowler - The pigs are both resting between snouts
and ears.
- 60 Points
Mixed Combo - A combination not mentioned above is the sum of
the single pigs' scores
Pig Out - If both pigs are lying on their sides, one with
the spot facing upwards and one with the spot
facing downwards the score for that turn is
reset to 0 and the turn changes to the next
player.
-------------------------------------------------------------------------
'''
print "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"
#playing game
def play_game(self):
#varibles
done=False;
turn=0
p1=Player()
p2=Player()
while(done!=True):
print ("Enter q to quite or anything to continue");
x=raw_input()
if(x=='q'):
return
print "Scores"
#to display score
print "##################################"
print "Player1 Score: ",p1.get_score()
print "Player2 Score: ",p2.get_score()
print "##################################"
print
print "--------------------------------"
if(turn==0):
print "Player 1 Turn :"
p1.roll_pig()
print "Result : ",
score=p1.determine_roll_result()
print "GOT : ",score,"Points"
#if we got pigout the we have to give turn to other player
if(score==0):
turn=1
if(p1.is_valid_winning_score(score)):
p1.addScore(score);
elif(turn==1):
print "Player 2 Turn :"
p2.roll_pig()
print "Result :",
score=p2.determine_roll_result()
print "GOT : ",score,"Points"
#if we got pigout the we have to give turn to other player
if(score==0):
turn=0
if(p2.is_valid_winning_score(score)):
p2.addScore(score)
print "--------------------------------"
print
if(p1.is_win()):
done=True
print "Player1 Wins"
elif(p2.is_win()):
print "Player2 Wins"
done=True
#lets start game
game=PassThePig()
game.game_rules()
game.play_game()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.