🐷
A game of Pig exercise
Write a command-line program that simulates the game of Pig.
Pig is a two-player game played with a 6-sided die.
The game has the following rules:
Each turn, a player repeatedly rolls a die until either a 1 is rolled or the player decides to “hold”:
If the player rolls a 1, they score nothing, and it becomes the next player’s turn.
If the player rolls any other number, it is added to their turn total, and their turn continues. The player can then decide to continue to roll again or “hold”.
If a player chooses to “hold”, their current turn total is added to their score, and it becomes the next player’s turn.
The first player to score 100 or more points wins.
For example, the first player, Donald, begins a turn with a roll of 5. Donald could hold and score 5 points but chooses to roll again. Donald rolls a 2 and could hold with a turn total of 7 points but decides to roll again. Donald rolls a 1 and must end his turn without scoring.
The next player, Alexis, rolls the sequence 4-5-3-5-6, after which she chooses to hold and adds her turn total of 23 points to her score.
The game continues, Donald taking the turn. In this turn, Donald rolls 3-2-4 and decides to hold. His total score so far is 9, and the turn passes to Alexis.
The player reaching a score of 100 or more total points wins.
Write a program in Go that simulates this two-player game. There are no human players, only computers. Each computer player plays with some fixed strategy. An example strategy is “hold after accumulating a score of at least 10 for each turn”. In this strategy, the player will keep on rolling until they accumulate a score of at least 10 in that round. If they roll a 1 before getting a turn total of 10, they score 0 points, and their turn passes to the next player.
Some example rolls for this strategy could be: - 3-4-2-4 (turn total is 13, which is greater than 10) and then the player holds and passes the turn to the next player. - 3-5-1 (turn total is 0, since a 1 is rolled) and then the player must pass the turn to the next player. - 6-4 (turn total is 10), hence player holds and passes the turn to the next player.
Similarly, another computer player may have a strategy to “hold after accumulating a score of at least 25 for each turn”. This player is “greedy” because they try to accumulate more points in a single round. This also means they may roll 1, and not get any points for that round. But, well, that’s their strategy.
The program should run a simulation where 10 such games are played between two players, where each player uses some fixed strategy. The program should output the number of wins and losses of each player.
Story 1
$ ./pig 10 15
Holding at 10 vs Holding at 15: wins: 3/10 (30.0%), losses: 7/10 (70.0%)
Story 2
First, Player 2 always uses a strategy of always holding after accumulating a score of at least 1.
Then, Player 2 always uses a strategy of always holding after accumulating a score of at least 2.
Then, Player 2 always uses a strategy of always holding after accumulating a score of at least 3.
Then, Player 2 always uses a strategy of always holding after accumulating a score of at least 4.
…
Then, Player 2 always uses a strategy of always holding after accumulating a score of at least 20.
Then, Player 2 always uses a strategy of always holding after accumulating a score of at least 22.
Then, Player 2 always uses a strategy of always holding after accumulating a score of at least 23.
…
Then, Player 2 always uses a strategy of always holding after accumulating a score of at least 100.
A side note about what it really means to have “Hold until 1” strategy?
What it really means when a player uses a strategy of always holding after accumulating a score of at least 1? It basically means, the player passes the turn to the next player immediately after rolling the dice. How?
Imagine the player rolls a 1. In that case, their turn ends anyway. If instead, the player rolls a 2. Since 2 is greater than 1, the player accumulates 2 to their score and passes the turn to the next player. Thus, this strategy means the player will only get a single roll. After one roll, they will pass the turn to the next player.
Strategy of always holding after accumulating a score of at least 2 is the same as above. Roll a 1 and let go of the turn. Or, roll any other number (since it is greater than or equal to 2), and let go of your turn.
Strategy of always holding after accumulating a score of at least 3 is a bit different. You can either roll a 1 and pass the turn. Or if you roll a 2, you will continue to roll again (since 2 is less than 3). You’ll then pass on the turn to the next player after your next roll. If however, you roll a 5, then you’ll immediately pass on the turn to the next player (since 5 is greater than 3). Hope you get the idea.
$ ./pig 21 1-100
Holding at 21 vs Holding at 1: wins: 8/10 (80.0%), losses: 2/10 (20.0%)
Holding at 21 vs Holding at 2: wins: 8/10 (80.0%), losses: 2/10 (20.0%)
Holding at 21 vs Holding at 3: wins: 8/10 (80.0%), losses: 2/10 (20.0%)
Holding at 21 vs Holding at 4: wins: 8/10 (80.0%), losses: 2/10 (20.0%)
Holding at 21 vs Holding at 5: wins: 8/10 (80.0%), losses: 2/10 (20.0%)
...
...
Holding at 21 vs Holding at 20: wins: 5/10 (50.0%), losses: 5/10 (50.0%)
Holding at 21 vs Holding at 22: wins: 6/10 (60.0%), losses: 4/10 (40.0%)
...
...
Holding at 21 vs Holding at 100: wins: 10/10 (100.0%), losses: 0/10 (0.0%)
Story 3
Result: Wins, losses staying at k = 36: 670/990 (67.7%), 320/990 (32.3%)
$ ./pig 1-100 1-100
Result: Wins, losses staying at k = 1: 277/990 (28.0%), 713/990 (72.0%)
Result: Wins, losses staying at k = 2: 237/990 (23.9%), 753/990 (76.1%)
Result: Wins, losses staying at k = 3: 322/990 (32.5%), 668/990 (67.5%)
...
...
Result: Wins, losses staying at k = 99: 225/990 (22.7%), 765/990 (77.3%)
Result: Wins, losses staying at k = 100: 210/990 (21.2%), 780/990 (78.8%)
Credits
This problem statement is a slight variation of Pig Codewalk from Go’s official website. I have broken the problem statement into three stories to make it approachable. https://go.dev/doc/codewalk/functions/ (this link also contains an implementation of Pig in functional programming style, so if you get stuck, refer here).
For more info about the Pig problem, refer https://en.wikipedia.org/wiki/Pig_(dice_game)