def hold(state): (p, me, you, pending) = state return (other[p], you, me+pending, 0) def roll(state, d): (p, me, you, pending) = state if d == 1: return (other[p], you, me+1, 0) else: return (p, me, you, pending+d) otehr = {1:0, 0:1} def test_actions(): s = (0, 10, 20, 30) assert hold(s) == (1, 20, 40, 0) assert roll(s, 6) == (0, 1, 20, 36) assert roll(s, 1) == (1, 20, 11, 0) return 'test_actions passes'
import random possible_moves = ['roll', 'hold'] def clueless(state): return random.choice(possible_moves)
def play_pig(A, B): 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))