Due: Thursday, April 23rd, 11:59pm
Homework will be turned in online (canvas) in written form, saved as a PDF.
(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 a few turns as possible.
Find a coin and play Piglet to get a feel for the game.
Answer the following questions:
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 file or 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 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.
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.
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 than either TIMID_BOT or BRAVE_BOT.
Return to the IMGD 2905 home page