Due: Thursday, 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 pigs is called a sounder, while a group of young pigs is called a drift.)
Piglet is a one player game where the goal is to get to 10 points in as few turns as possible.
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 in the pot 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. 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 ??? ...
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.2
#
import random
# Game settings.
= 10 # Number of points to win one game
WIN = 2 # Number of outcomes (e.g., 2 for coin, 6 for d6)
OUTCOMES = 1 # Number of games to play
NUM_GAMES
#######################################
# Pick random integer from 1 to OUTCOMES.
# Add number to pot.
# If pick 1, pot empties.
# Return pot.
def go(pot):
= random.randint(1,OUTCOMES)
number
if number == 1:
print("Bust!")
= 0
pot
else:
print(number)
+= number
pot
return pot
#######################################
# Players - Return True if go, False if stay.
# Human has choice.
def HUMAN() :
= input()
choice if choice == '1':
return True
if choice == '2':
return False
# Timid bot always stays.
def TIMID_BOT(points, pot) :
if pot == 0:
return True
return False
# Brave bot always goes (until enough points to win).
def BRAVE_BOT(points, pot) :
if points + pot >= WIN:
return False
return True
#######################################
# Play one game.
# Return number of turns it took to win.
def play():
= 1, 0, 0
turn, pot, points
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.
= HUMAN()
choice #choice = TIMID_BOT(points, pot)
#choice = BRAVE_BOT(points, pot)
if choice == False:
print("STAY")
+= pot
points += 1
turn = 0
pot continue
if choice == True:
print("GO: ", end="")
= go(pot)
pot if pot == 0: # Bust
+= 1
turn 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.
TIMID_BOT
or
BRAVE_BOT
?Make sure to include 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.
Find a die and play Pig to get a feel for the game.
Answer the following questions:
Modify the Piglet Python script from Part 1 to play Pig.
TIMID_BOT
or
BRAVE_BOT
?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
.
BETTER_BOT
compared to the other two bots.BEST_BOT
, as good as possible? Why or why
not?
Return to the IMGD 2905 home page