**Pig** is a folk jeopardy dice game described by John Scarne in 1945, and
was an ancestor of the modern game **Pass the Pigs®** (originally called **
PigMania®**).

The rules are simple: Two players race to reach 100 points. Each turn, a
player repeatedly rolls a die until either a 1 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 opponent's turn.

To familiarize yourself with play, you can play an optimal Pig opponent online. The key decision facing a player is how large a turn total should be risked to possibly get an even larger total. To learn more about the game of Pig, visit The Game of Pig web page.

Summary | The Game of Pig - Milestone-based approach to implementation of the dice game Pig with side opportunities for simple, interesting analysis driven by curiosity to better understand game play. Also includes object-oriented and GUI programming exercises. |

Topics | Bottom-up, milestone-based development, dynamic programming and Monte Carlo analysis techniques. |

Audience | Assignment suite below is appropriate
for CS1. Upper-level Pig-related assignments for networking,
artificial intelligence, etc. are described in
Pedagogical
Possibilities for the Dice Game Pig. Journal of Computing Sciences
in Colleges, vol. 21, no. 6, pp. 149-161, June 2006. |

Difficulty | These assignments range from beginner to intermediate difficulty. CS1 students complete all but the object-oriented and GUI programming assignments in less than 3 weeks. |

Strengths | Pig offers one of the best fun-to-SLOC (source lines of code) ratios of any game. One would be hard pressed to find another game with such simple rules that engages and leaves one on the knife-edge of indecision. As such, it is a teaching treasure. Mathematics instructors have long used this game to teach probabilistic concepts. We here offer a rich collection of CS1 Pig exercises that illustrate bottom-up development. Alternatively, one can approach the final "Pig Game" exercise as a good top-down, stepwise refinement exercise. Opportunities for Monte Carlo simulation abound. |

Weaknesses | Except for GUI assignment, text-based and non-graphical. |

Dependencies | Most assignments require only understanding of random number generation, text I/O, and simple control structures. GUI assignment requires additional understanding of basic GUI concepts and components including labels, buttons, panel image display, and popup window dialogs. O-O assignment requires understand of basic O-O concepts, including interfaces. |

Variants | One can easily create variants by choosing different play policies and/or rule variations. Both are described in detail below. |

Each exercise below may be used stand-alone or in a logical sequence of milestones.

Problem Description |
Previous Milestone |

Hold-at-20 Turn - simulate a single turn of Pig where a player holds at 20 points | |

Hold-at-20 Outcomes (Estimation) - statistics on Monte Carlo simulations of hold-at-20 turn outcomes | Hold-at-20 Turn |

Hold-at-20 Outcomes (Computation) - dynamic programming computation of hold-at-20 turn outcomes | Hold-at-20 Turn |

Hold-at-20-or-Goal Turn - simulate a single turn of Pig where a player holds at 20 points or the goal score | Hold-at-20 Turn |

Hold-at-20-or-Goal Game - simulate a single game of Pig where a player holds at 20 points or the goal score | Hold-at-20-or-Goal Turn |

Average Pig Turns - average turns of Monte Carlo simulations of hold-at-20-or-goal games | Hold-at-20-or-Goal Game |

Two-Player Pig - simulate a single two-player game of Pig with hold-at-20-or-goal players | Hold-at-20-or-Goal Game |

First-Player Advantage - Monte Carlo simulations estimate the first-player advantage in hold-at-20-or-goal Pig | Two-Player Pig |

Pig Game - the user plays Pig versus a hold-at-20-or-goal player | Two-Player Pig |

O-O Pig Game - the user plays Pig versus a hold-at-20-or-goal player. Implementation is object-oriented. | |

Pig Game GUI - the user plays Pig versus a keep-pace-and-end-race player via a GUI |

Different subsets of these exercises may be chosen according to different
desired goals/emphases. For example:__
Pig Game Milestone track:__ Hold-at-20 Turn,
Hold-at-20-or-Goal Turn,
Hold-at-20-or-Goal Game, Two-Player Pig,
Pig Game

Creating variations of the these exercises is simple. One can both vary the simulated play policy, and/or rule variations.

**Pig Play Policies:** Let *i *be the player's score,* j*
be the opponent's score, and *k* be the current turn total.* *

__Hold at 25 or goal__- Hold at the lesser of 25 and 100 - i.__4 Scoring Turns__- Let*t*be the number of turns in which a player has held so far. Hold at floor((100 - i)/(4 - t)).__Score Base, Keep Pace, and End Race__- If*i*>= 69 or*j*>= 69, roll for the goal. Otherwise, hold at the greater of 19 and*j*- 14.__Keep Pace and End Race__- If*i*>= 71 or*j*>= 71, roll for the goal. Otherwise, hold at 21 + round((*j*-*i*) / 8).__Optimal__- (advanced) see Solving the Dice Game Pig: an introduction to dynamic programming and value iteration

**Game Variations:**

__Piglet (a.k.a. Pigtails)__- Pig with a coin. A "tails" flip is like a "pig" roll of 1. The holding player scores the number of successive "head" flips. Use lower goal values (e.g. 10) and hold values (e.g. 2).__Two-Dice Pig__- This variation is the same as Pig, except:- Two standard dice are rolled. If neither shows a 1, their sum is added to the turn total.
- If a single 1 is rolled, the player scores nothing and the turn ends.
- If two 1s are rolled, the player’s entire score is lost, and the turn ends.

__Big Pig__- This variation is the same as Two-Dice Pig, except:- If two 1s are rolled, the player adds 25 to the turn total.
- If other doubles are rolled, the player adds twice the value of the dice to the turn total.

- Pass the Pigs® - This commercial game, originally called PigMania®, uses small rubber pigs as dice. Rolls are scored depending on whether pigs land on their feet, sides, backs, etc. in various combinations. Data for relative frequencies of these combinations are available from the statistical analysis External Links of the Pass the Pigs® Wikipedia article.
__Hog__- Hog is played as Pig where, at the beginning of a turn, the player decides*how many dice*to roll and rolls them all at once. If any 1's ("hogs") are rolled, there is no score for the turn. Otherwise, the sum of the dice are scored. In either case, it becomes the next player's turn.- A list of Pig game variations is also available.

With the many ways the problem set above may be thus varied, it is not difficult to craft a unique (i.e. not easily plagiarized) assignment experience for your students.

With its simple rules and engaging play, Pig lends itself to many uses across the Computer Science curriculum:

- Pig Computer Science Teaching Resources
- Todd W. Neller, Clifton G.M. Presser, Ingrid Russell, Zdravko Markov.
Pedagogical
Possibilities for the Dice Game Pig.
*Journal of Computing Sciences in Colleges*, vol. 21, no. 6, pp. 149-161, June 2006. (Best paper - 2nd place) - NSF Artificial Intelligence CCLI Project: Solving the Dice Game Pig: an introduction to dynamic programming and value iteration