Due: *Monday, April 11th, 11:59pm*

Homework will be turned in online (canvas) in written form, saved as a PDF.

Total points: 23

(Fun facts: A group of young pigs is a "drift", and a group of older pigs is a "sounder".)

Piglet is a one player game where the goal is to get 10 points in as few turns as possible.

- Each turn, the pot starts empty
- Player choice:
*go*or*stay* - If
*stay*, player adds pot to points and turn ends - If
*go*, player flips coin:*Tails*busts and pot empties and turn ends*Heads*add 2 to pot and go back to choice

- Player choice:
- Get to 10 points to win
- A lower number of turns is better

Find a coin and play Piglet to get a feel for the game.

Answer the following questions:

(1 point) What is the probability a player wins on their first turn?

(1 point) What is the expected number of points for the first flip?

*Hint:*The expected value is what you could expect in the "long run" after many trials, obtained by multiplying each event by its probability. Consider the number of points if successful (heads), the number of points if bust (tails) and the likelihood of each.(3 points) Complete the following table for 10 rows (i.e., 10 flips), also filling in the "Expected Points" column if the player stays after that many flips in a row (not counting any previous or following points). As in a normal game, if the player busts, the flipping stops. However, unlike a normal game, in this example, if the player doesn't bust, the Pot may become larger than 10 and the player will continue to flip.

Flips Chance Not Busting Pot Expected Points 1 0.5 2 ??? 2 0.5^2 4 ??? 3 0.5^3 6 ??? ...

- (1 points) If a player flips a coin only once and then always stays (ends the turn), after 20 turns what is the probability that they have 4 or fewer points?
*Hint:*think of the kind of distribution this is and try a formula.

Copy and paste the below Python script into a Jupyter notebook and run it. Study what it does. Run it some more until you mostly understand it.

```
#
# Piglet - a single-player coin-flip game.
# v1.1
#
import random
# Game settings.
WIN = 10 # Number of points to win one game
OUTCOMES = 2 # Number of outcomes (e.g., 2 for coin, 6 for d6)
NUM_GAMES = 1 # Number of games to play
#######################################
# Pick random integer from 1 to OUTCOMES.
# Add number to pot.
# If pick 1, pot empties.
# Return pot.
def go(pot):
number = random.randint(1,OUTCOMES)
if number == 1:
print("Bust!")
pot = 0
else:
print(number)
pot += number
return pot
#######################################
# Players - Return True if go, False if stay.
# Human has choice.
def HUMAN() :
while True:
choice = input()
if choice == '1':
return True
if choice == '2':
return False
# Brave bot always goes.
def BRAVE_BOT(points, pot) :
if points + pot >= WIN:
return False
return True
# Timid bot always stays.
def TIMID_BOT(points, pot) :
if pot == 0:
return True
return False
#######################################
# Play one game.
# Return number of turns it took to win.
def play():
turn, pot, points = 1, 0, 0
while points < WIN :
print("Turn:", turn, " ", end="")
print("Points:", points, " ", end="")
print("Pot:", pot, " ", end="")
print(" 1-Go or 2-Stay? ", end="")
## Uncomment only one of the below players.
choice = HUMAN()
#choice = TIMID_BOT(points, pot)
#choice = BRAVE_BOT(points, pot)
if choice == False:
print("STAY")
points += pot
turn += 1
pot = 0
continue
if choice == True:
print("GO: ", end="")
pot = go(pot)
if pot == 0: # Bust
turn += 1
continue
return turn-1
########################################
# Play game(s).
game = []
for x in range(NUM_GAMES):
print("------------------")
print("Playing game:", x)
game.append(play())
# Print results.
print("------------------------------------")
for x in range(NUM_GAMES):
print("Game", x, "turns:", game[x])
```

The game defaults to being played with a person (`HUMAN`

). However, there are two different bots (`TIMID_BOT`

or `BRAVE_BOT`

) that can play the game instead. Playing the game with a bot provides for a way of simulating gameplay without having to do the same testing with people.

- (5 points) Which bot is better,
`TIMID_BOT`

or`BRAVE_BOT`

?

- Run experiments that gather data needed to answer this question.
- Analyze the results: compute statistics, draw charts/tables, and describe the analysis.
- Provide a conclusion that answers the question from the data gathered.

Make sure to included details on the above (i.e., experiments, analysis, interpretation) in your answer!

Pig is like Piglet, but instead of flipping a coin, players roll one 6-sided die.

- Each turn, the pot starts empty
- Player choice:
*go*or*stay* - If
*stay*, player adds pot to points and turn ends - If
*go*, player rolls die:*1*busts and the pot empties and the turn ends*2-6*add number of pips to pot and go back to the choice

- Player choice:
- Get to 50 points to win
- A lower number of turns is better

Find a die and play Pig to get a feel for the game.

Answer the following questions:

- (1 point) What is the probability of
*not*busting on a single roll? - (1 point) What is the average pot if the player does not bust on single roll?
- (1 point) What is the expected number of points a player gets for a single roll?
- (1 point) How many ways are there to get
*exactly*4 points on one turn? (*Optional*- what is the probability of doing so?) - (3 points) Draw a chart depicting the expected number of points versus number of consecutive rolls.
*Hint:*see Question #3 above.

Modify the Piglet Python script from Part 1 to play Pig.

- (5 points) Which bot is better,
`TIMID_BOT`

or`BRAVE_BOT`

?

- Run experiments that gather data needed to answer this question.
- Analyze the results: compute statistics, draw charts/tables, and describe the analysis.
- Provide a conclusion that answers the question from the data gathered.

Make sure to included details on the above (i.e., experiments, analysis, interpretation) in your answer!

*Bonus:* Develop a `BETTER_BOT`

, one that wins in fewer rolls on average than either `TIMID_BOT`

or `BRAVE_BOT`

.

- Once developed, describe the logic behind your bot.
- Run experiments that gather data needed to evaluate your
`BETTER_BOT`

compared to the other two bots. - Draw charts, provide statistics and analyze the data to support your comparison.
- Is your bot a
`BEST_BOT`

, as good as possible? Why or why not?

Return to the IMGD 2905 home page