Skip navigation
Homework 8: The Game of Pig
- Assigned
- Wednesday October 5, 2016
- Due
- 11 p.m., Wednesday October 12, 2016
- Summary
- The
goal of this assignment is to gain experience writing a complete
program with multiple functions. You will practice reading
documentation and translating algorithms described in pseudo-code into
Python code. You will use conditionals, Boolean operators,
while
loops, accumulators, and random numbers.
- Collaboration
- Do this assignment on your own or with a partner of your choice.
- Submitting
- Submit a Python program using the online turnin form. See below.
- Scoring
- 16 points
Assignment: The Game of Pig
In the Game of Pig, two opposing players attempt to win by being the
player with the most total points after at least one player
exceeds 100 total points.
At the beginning of the game, both players have 0 total points.
In each round, player 1 takes a turn, and player 2 takes a turn.
The game ends if at the end of a round, at least one of the players
has 100 or more points. The winner is the player with the most total
points.
The player begins a turn with 0 turn points. The player
rolls a
six sided die. If the die roll produces a 1, then the player’s
turn points are removed, and their turn is over. If any other
number is rolled (2-6), the roll is added to the player’s turn
points.
The player now chooses whether to quit the turn, or
continue the turn. If the player quits the turn, their
turn points are added to their total points. If the
player
continues, then the player rolls the die again, and follows
the above outline based on the value rolled.
In the rest of this assignment, you will create a computer program
that
will allow two users to play the Game of Pig against each
other.
We will break the assignment into two parts. Each part will
involve the creation of one or two functions. When working
together, the functions will create the full behavior of the
game. Breaking a problem into smaller pieces to solve one part
at a time is called decomposition.
Preliminaries
Create a file named pig.py in which you will write all your code.
Part 1: Rolling the die and taking a turn
In this part, you will implement two functions, rollDie
and takeTurn
.
Rolling the die
Create a function named rollDie
.
It should receive one parameter, an integer that specifies the number
of sides on the
die. It should generate a random integer that is at least 1 and no more
than the number
of sides. (For example, a six-sided die can only produce the
integers 1 through 6, inclusive.) Then it should return the
randomly generated number.
Hint: Look at the
online python
documentation for the random module to help you decide which random
number function to call in your rollDie
function. There is one that is much more suitable than random.random()
!
At the bottom of pig.py,
add the following test code:
def test():
for i in range(10):
print(rollDie(4))
if __name__=='__main__':
test()
Write any additional test code you like to feel confident your function is working correctly.
Taking a turn
Create a function named takeTurn
to let
a single player to take a single turn. The function
should receive one parameter, an integer that
identifies the player number. It should return the
number of turn points acquired by the player.
Below is a pseudo-code description of the algorithm
you should use to implement the takeTurn
function.
- set turn points to 0
- set keep rolling flag to true
- print beginning of turn message for whichever player's turn it is
- wait for the enter key to be pressed
- while the user can and wants to keep rolling:
- roll the six-side die
- tell the user the value of the roll
- if the roll was a 1
- set turn points to 0
- set keep rolling flag to false
- otherwise (the roll was not a 1)
- add the value of the roll to turn points
- tell the user the current number of turn points
- ask the user if they want to continue
- if yes
- set keep rolling flag to true
- otherwise (no)
- set keep rolling flag to false
- print end of turn message
- return the number of turn points
As you create your takeTurn
function, you should test it
to be sure it behaves correctly. Track the turn points
manually to be sure that the function adds up points
correctly.
Below is an example output from the takeTurn
function
for player 1.
==================================================
Player 1 press enter to begin your turn.
You rolled a 4
Your turn points are now 4 .
Continue rolling (0=no,1=yes)? 1
You rolled a 3
Your turn points are now 7 .
Continue rolling (0=no,1=yes)? 1
You rolled a 1
Turn over
==================================================
This is an example for player 2.
==================================================
Player 2 press enter to begin your turn.
You rolled a 3
Your turn points are now 3 .
Continue rolling (0=no,1=yes)? 1
You rolled a 3
Your turn points are now 6 .
Continue rolling (0=no,1=yes)? 1
You rolled a 5
Your turn points are now 11 .
Continue rolling (0=no,1=yes)? 0
Turn over
==================================================
Update the test code as follows to test the takeTurn
function for both players:
def test():
for i in range(10):
print(rollDie(4))
print()
takeTurn(1)
takeTurn(2)
if __name__=='__main__':
test()
Run this program to make sure it behaves as expected. Fix any
problems.
Part 2: Completing the game
You will
now complete
the game by adding instructions and implementing the main game loop.
Giving instructions
Create a function called showInstructions
that prints out
a description of how to play the game for the users. This
function does not receive any parameters. It does not return
any values.
This is an example of the instructions that may be printed
by your function:
Welcome to the Game of Pig. To win, be
the
player with the most points at the end of the
game. The game ends at the end of a round where
at least one player has 100 or more points.
On each turn, you may roll the die as many times
as you like to obtain more points. However, if
you roll a 1, your turn is over, and you do not
obtain any points that turn.
The main game loop
Create a function called playGame
that lets two players play one game of Pig. This function receives no
parameters and returns
no values.
Below is a pseudo-code description of the algorithm
you should use to implement the playGame
function.
- set player 1’s total points to 0
- set player 2’s total points to 0
- while player 1’s total points are less than 100 and player 2’s
total points are less than 100
- show the total points for each player
- have player 1 take a turn
- add player 1’s turn points to their total points
- show the total points for each player
- have player 2 take a turn
- add player 2’s turn points to their total points
- display the end of game message
- show the total points for each player
- if player 1 has more total points
- show that player 1 is the winner
- if player 2 has more total points
- show that player 2 is the winner
- otherwise
- show that the game is a tie
See the sample output for a full game at the end of this document.
Add a main function that calls showInstructions
and playGame
:
def main():
showInstructions()
playGame()
if __name__=='__main__':
main()
Play your game!
Above & Beyond
For this assignment, you must have at least a small Above &
Beyond component. Here are some ideas for going above & beyond with
the Game of
Pig.
- Refactor the code specified above by identifying more
opportunities for
functional decomposition. For example, repeated code often suggests an
opportunity
to create a function.
- Improve on the user experience specified above. For
example, your program could allow players to type "y", "yes", "n", or
"no", rather than "0" or "1". Or ask the players for their names, and use them when announcing whose turn it is.
- Let players choose what kind of dice to play with (how many sides). This should affect the difficulty of the game.
- Let players play multiple games. Keep a running tally of wins.
- Make the game more visually appealing by drawing ASCII art to show the dice rolls and/or whose turn it is.
- Implement a variation
of Pig. If you do this, please put it in a separate file named, for
example, big_pig.py.
Submitting your work
Please document your functions and organize your code per section 3.4 of the textbook.
Submit one file, pig.py, through the online turnin form. If you submitted a
separate Above & Beyond program, make sure it is clearly
identifiable.
Point distribution
- Style and following instructions, including the provision of
test
and main
functions - 2 pt
rollDie
- 2 pt
takeTurn
- 5 pt
showInstructions
- 1 pt
playGame
- 5 pt
- Above & Beyond - 1+ pt
Sample output of a full Game of Pig
Welcome to the Game of Pig. To win, be
the
player with the most points at the end of the
game. The game ends at the end of a round where
at least one player has 100 or more points.
On each turn, you may roll the die as many times
as you like to obtain more points. However, if
you roll a 1, your turn is over, and you do not
obtain any points that turn.
Player 1 has 0 points.
Player 2 has 0 points.
==================================================
Player 1 press enter to begin your turn.
You rolled a 5
Your turn points are now 5 .
Continue rolling (0=no,1=yes)? 1
You rolled a 5
Your turn points are now 10 .
Continue rolling (0=no,1=yes)? 1
You rolled a 6
Your turn points are now 16 .
Continue rolling (0=no,1=yes)? 1
You rolled a 3
Your turn points are now 19 .
Continue rolling (0=no,1=yes)? 2
Turn over
==================================================
Player 1 has 19 points.
Player 2 has 0 points.
==================================================
Player 2 press enter to begin your turn.
You rolled a 3
Your turn points are now 3 .
Continue rolling (0=no,1=yes)? 1
You rolled a 1
Turn over
==================================================
Player 1 has 19 points.
Player 2 has 0 points.
==================================================
Player 1 press enter to begin your turn.
You rolled a 6
Your turn points are now 6 .
Continue rolling (0=no,1=yes)? 1
You rolled a 4
Your turn points are now 10 .
Continue rolling (0=no,1=yes)? 1
You rolled a 4
Your turn points are now 14 .
Continue rolling (0=no,1=yes)? 1
You rolled a 5
Your turn points are now 19 .
Continue rolling (0=no,1=yes)? 2
Turn over
==================================================
Player 1 has 38 points.
Player 2 has 0 points.
==================================================
Player 2 press enter to begin your turn.
You rolled a 2
Your turn points are now 2 .
Continue rolling (0=no,1=yes)? 1
You rolled a 3
Your turn points are now 5 .
Continue rolling (0=no,1=yes)? 1
You rolled a 2
Your turn points are now 7 .
Continue rolling (0=no,1=yes)? 1
You rolled a 6
Your turn points are now 13 .
Continue rolling (0=no,1=yes)? 1
You rolled a 4
Your turn points are now 17 .
Continue rolling (0=no,1=yes)? 1
You rolled a 1
Turn over
==================================================
Player 1 has 38 points.
Player 2 has 0 points.
==================================================
Player 1 press enter to begin your turn.
You rolled a 3
Your turn points are now 3 .
Continue rolling (0=no,1=yes)? 1
You rolled a 4
Your turn points are now 7 .
Continue rolling (0=no,1=yes)? 1
You rolled a 1
Turn over
==================================================
Player 1 has 38 points.
Player 2 has 0 points.
==================================================
Player 2 press enter to begin your turn.
You rolled a 3
Your turn points are now 3 .
Continue rolling (0=no,1=yes)? 1
You rolled a 6
Your turn points are now 9 .
Continue rolling (0=no,1=yes)? 1
You rolled a 4
Your turn points are now 13 .
Continue rolling (0=no,1=yes)? 1
You rolled a 2
Your turn points are now 15 .
Continue rolling (0=no,1=yes)? 1
You rolled a 1
Turn over
==================================================
Player 1 has 38 points.
Player 2 has 0 points.
==================================================
Player 1 press enter to begin your turn.
You rolled a 3
Your turn points are now 3 .
Continue rolling (0=no,1=yes)? 1
You rolled a 5
Your turn points are now 8 .
Continue rolling (0=no,1=yes)? 1
You rolled a 6
Your turn points are now 14 .
Continue rolling (0=no,1=yes)? 1
You rolled a 6
Your turn points are now 20 .
Continue rolling (0=no,1=yes)? 1
You rolled a 2
Your turn points are now 22 .
Continue rolling (0=no,1=yes)? 1
You rolled a 5
Your turn points are now 27 .
Continue rolling (0=no,1=yes)? 2
Turn over
==================================================
Player 1 has 65 points.
Player 2 has 0 points.
==================================================
Player 2 press enter to begin your turn.
You rolled a 6
Your turn points are now 6 .
Continue rolling (0=no,1=yes)? 1
You rolled a 3
Your turn points are now 9 .
Continue rolling (0=no,1=yes)? 1
You rolled a 1
Turn over
==================================================
Player 1 has 65 points.
Player 2 has 0 points.
==================================================
Player 1 press enter to begin your turn.
1
You rolled a 3
Your turn points are now 3 .
Continue rolling (0=no,1=yes)? 1
You rolled a 2
Your turn points are now 5 .
Continue rolling (0=no,1=yes)? 1
You rolled a 3
Your turn points are now 8 .
Continue rolling (0=no,1=yes)? 1
You rolled a 4
Your turn points are now 12 .
Continue rolling (0=no,1=yes)? 1
You rolled a 4
Your turn points are now 16 .
Continue rolling (0=no,1=yes)? 2
Turn over
==================================================
Player 1 has 81 points.
Player 2 has 0 points.
==================================================
Player 2 press enter to begin your turn.
You rolled a 1
Turn over
==================================================
Player 1 has 81 points.
Player 2 has 0 points.
==================================================
Player 1 press enter to begin your turn.
1
You rolled a 3
Your turn points are now 3 .
Continue rolling (0=no,1=yes)? 1
You rolled a 6
Your turn points are now 9 .
Continue rolling (0=no,1=yes)? 1
You rolled a 3
Your turn points are now 12 .
Continue rolling (0=no,1=yes)? 1
You rolled a 3
Your turn points are now 15 .
Continue rolling (0=no,1=yes)? 1
You rolled a 5
Your turn points are now 20 .
Continue rolling (0=no,1=yes)? 1
You rolled a 4
Your turn points are now 24 .
Continue rolling (0=no,1=yes)? 1
You rolled a 4
Your turn points are now 28 .
Continue rolling (0=no,1=yes)? 2
Turn over
==================================================
Player 1 has 109 points.
Player 2 has 0 points.
==================================================
Player 2 press enter to begin your turn.
1
You rolled a 3
Your turn points are now 3 .
Continue rolling (0=no,1=yes)? 1
You rolled a 3
Your turn points are now 6 .
Continue rolling (0=no,1=yes)? 1
You rolled a 4
Your turn points are now 10 .
Continue rolling (0=no,1=yes)? 1
You rolled a 2
Your turn points are now 12 .
Continue rolling (0=no,1=yes)? 1
You rolled a 6
Your turn points are now 18 .
Continue rolling (0=no,1=yes)? 1
You rolled a 1
Turn over
==================================================
The game is over.
Player 1 has 109 points.
Player 2 has 0 points.
Player 1 wins.
Janet
Davis (davisj@whitman.edu).
This assignment is adapted from
one written by Craig Wastlund and shared on EngageCSEdu.
Created October 5, 2016
Last revisedOctober 05, 2016, 10:58:11 AM PDT
This work is licensed under a Creative
Commons Attribution-Noncommercial-Share Alike 3.0 United States License.