Skip to content

Instantly share code, notes, and snippets.

@jakelosh
Last active July 4, 2019 07:13
Show Gist options
  • Save jakelosh/4685daaa4f26b59aa64f16467839a739 to your computer and use it in GitHub Desktop.
Save jakelosh/4685daaa4f26b59aa64f16467839a739 to your computer and use it in GitHub Desktop.
I play a board game with my daughter called "Count Your Chickens". At the start, little chick tokens are scattered throughout the board and the objective is to get them all back in the coop. Each turn you spin a wheel with a number of farm-related items, which determines the number of spaces you move. The number of spaces you move determines the…
import numpy as np
# Create spinner function
def spin():
return np.random.randint(0,5)
spin_categories = {
0: 'pig',
1: 'dog',
2: 'sheep',
3: 'tractor',
4: 'cow',
5: 'fox'
}
# Define the playing board
space_labels = ['start', 'blank', 'sheep', 'pig', 'tractor', 'cow', 'dog', 'pig', 'cow', 'dog',
'sheep', 'tractor', 'blank', 'cow', 'pig', 'blank', 'blank', 'blank', 'tractor','blank',
'tractor', 'dog', 'sheep', 'cow', 'dog', 'pig', 'tractor', 'blank', 'sheep', 'cow',
'blank', 'blank', 'tractor', 'pig', 'sheep', 'dog', 'blank', 'sheep', 'cow', 'pig',
'end']
spaces = [x for x in range(41)]
board = dict(zip(spaces, space_labels))
# Define the board bonus spaces. If you land on one, you get an additional chick.
bonuses = [0] * len(board)
bonuses[4] = 1
bonuses[8] = 1
bonuses[22] = 1
bonuses[35] = 1
bonuses[39] = 1
# Set up the simulation loop
wins = 0
trials = int(1e5)
for i in range(trials):
turns = 0
position = 0
chicks = 0
while position < len(board) - 1 and chicks < 40:
turns += 1
spin = spin_categories[np.random.randint(0,len(spin_categories))]
#spin = 'sheep'
if spin == 'fox':
if chicks > 0:
chicks -= 1
else:
for x in range(position+1, len(board)):
if board[x] == spin or board[x] == 'end':
chicks += (x - position + bonuses[x])
position = x
break
else:
x += 1
#print("turn: " + str(turns).ljust(2), "spin: " + spin.ljust(7), "chicks: " + str(chicks).ljust(2), "position: " + str(position))
if position <= 40 and chicks >= 40:
wins += 1
# Print the results
print("num wins: " + str(wins))
print("num trials: " + str(trials))
print("win ratio: " + str(wins/trials))
print("win/loss ratio: " + str((wins/(trials-wins))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment