








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This document contains instructions regarding coursework for the python class.
Typology: Lecture notes
1 / 14
This page cannot be seen from the preview
Don't miss anything!









In this assignment, we will be implementing a dice game called pig. The game will have two modes, a two-player mode and a single player mode that plays against the computer. We will be applying the data analysis help create a good, but simple, strategy for the computer to use. We will also be doing a number of smaller exercises to build up to the final implementation. Todd Neller came up with this assignment and has done a crazy amount of anaylsis, so I’ll use his words to explain the rules below, as well as much of the sample outputs.
Pig is a folk jeopardy dice game with simple rules: Two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 (”pig”) is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player’s turn, the player is faced with two decisions:
roll If the player rolls a
1: the player scores nothing and it becomes the opponent’s turn. 2 - 6: the number is added to the player’s turn total and the player’s turn continues.
hold The turn total is added to the player’s score and it becomes the op- ponent’s turn.
1 One Automated Turn of Pig
Our first exercise is to simulate a single turn of Pig where a player rolls until a 1 (“pig”) is rolled, or the turn total is greater than or equal to 20. We will call this strategy Hold-at-20. The user doesn’t need to make any choices, the computer will roll automagically, following the hold-at-20 strategy.
The different runs of the program are separated by dots.
Roll: 4 Roll: 5 Roll: 6 Roll: 5 Turn total: 20 ... Roll: 3 Roll: 1 Turn total: 0 ... Roll: 5 Roll: 2 Roll: 3 Roll: 6 Roll: 5 Turn total: 21
3 Hold-at-X Outcomes
As the previous exercise, but allow the user to specify the hold value. What is the probability of reaching 100 in a single turn if you allow the cpu to hold at 100?
4 Hold-at-20-or-Goal Turn
Given a player’s score, simulate a single turn of Pig where a player rolls until a 1 (“pig”) is rolled, or the turn total is greater than or equal to 20, or the score plus the turn total is greater than or equal to 100.
Input
The input is a single integer representing a Pig player score.
Output
Score? 90 Roll: 2 Roll: 3 Roll: 4 Roll: 4 Turn total: 13 New score: 103
6 Average Pig Turns
What is the expected number of turns per solitaire game with a hold-at-20- or-goal play policy? Simulate a given number of solitaire Pig games where a player rolls until a 1 (“pig”) is rolled, or the turn total is greater than or equal to 20, or the score plus the turn total is greater than or equal to 100. Report the average number of turns per game.
Input
Enter a single positive integer indicating the number of games simulated. (Larger numbers will tend to yield better estimations.)
Output
Games? 1000000 Average turns: 12.
8 Pig Game
Implement a game of Pig where the user plays against a “hold at 20 or goal” computer player that rolls until a 1 (“pig”) is rolled, or the turn total is greater than or equal to 20, or the score plus the turn total is greater than or equal to 100. The first player is chosen randomly.
Input
An empty input (i.e., Enter) indicates that the user wishes to roll. Any entered line of non-zero length indicates that the user wishes to hold.
Output
You will be player 2. Enter nothing to roll; enter anything to hold. Player 1 score: 0 Player 2 score: 0 It is player 1’s turn. Roll: 5
Turn total: 20 Roll/Hold? h Turn total: 20 New score: 41 Player 1 score: 0 Player 2 score: 41 It is player 1’s turn. Roll: 3 Roll: 3 Roll: 6 Roll: 4 Roll: 4 Turn total: 20 New score: 20 Player 1 score: 20 Player 2 score: 41 It is player 2’s turn. Roll: 3 Turn total: 3 Roll/Hold? (Enter) Roll: 3 Turn total: 6 Roll/Hold? (Enter) Roll: 2 Turn total: 8 Roll/Hold? (Enter) Roll: 2 Turn total: 10 Roll/Hold? (Enter) Roll: 4 Turn total: 14 Roll/Hold? (Enter) Roll: 2 Turn total: 16 Roll/Hold? (Enter) Roll: 4 Turn total: 20 Roll/Hold? h Turn total: 20 New score: 61 Player 1 score: 20 Player 2 score: 61 It is player 1’s turn. Roll: 5 Roll: 1 Turn total: 0 New score: 20 Player 1 score: 20
Player 2 score: 61 It is player 2’s turn. Roll: 3 Turn total: 3 Roll/Hold? (Enter) Roll: 3 Turn total: 6 Roll/Hold? (Enter) Roll: 5 Turn total: 11 Roll/Hold? (Enter) Roll: 2 Turn total: 13 Roll/Hold? (Enter) Roll: 6 Turn total: 19 Roll/Hold? h Turn total: 19 New score: 80 Player 1 score: 20 Player 2 score: 80 It is player 1’s turn. Roll: 3 Roll: 1 Turn total: 0 New score: 20 Player 1 score: 20 Player 2 score: 80 It is player 2’s turn. Roll: 2 Turn total: 2 Roll/Hold? (Enter) Roll: 2 Turn total: 4 Roll/Hold? (Enter) Roll: 4 Turn total: 8 Roll/Hold? (Enter) Roll: 2 Turn total: 10 Roll/Hold? (Enter) Roll: 3 Turn total: 13 Roll/Hold? (Enter) Roll: 6 Turn total: 19 Roll/Hold? (Enter) Roll: 5 Turn total: 24 Roll/Hold? h Turn total: 24 New score: 104