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.

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.

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.


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


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
CC-BY-NC-SA This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.