Skip to content

Instantly share code, notes, and snippets.

@kenju254
Created August 28, 2014 14:26
Show Gist options
  • Save kenju254/922c90d9df80bc17162e to your computer and use it in GitHub Desktop.
Save kenju254/922c90d9df80bc17162e to your computer and use it in GitHub Desktop.
def play_pig(A, B):
"""Lets play a small game of pig between two players, represented by
their strategy. Each time through the main loop, we ask the current player
for one decision which must be 'hold' or 'roll', and we update the state
accordingly when one player's score exceeds the goal, return that player"""
strategies = [A, B]
state = (0,0,0,0)
while True:
(p, me, you, pending) = state
if me >= goal:
return strategies[p]
elif you >= goal:
return strategies[other[p]]
elif strategies[p](state) == 'hold':
state = hold(state)
else:
state = roll(state, random.randint(1,6))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment