Due: Thursday, April 22nd, 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.
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 his/her 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 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.0
#
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.
TIMID_BOT
or BRAVE_BOT
?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.
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