#!/usr/bin/env python class Features(object): ballFar = True ballOnLeft = True class Goalkeeper(object): def __init__(self, features): self.features = features self.diveFramesRemaining = 0 self.lastDiveCommand = None def next(self): features = self.features if self.diveFramesRemaining: self.diveFramesRemaining -= 1 return self.lastDiveCommand else: if features.ballFar: return 'stand' else: if features.ballOnLeft: command = 'diveLeft' else: command = 'diveRight' self.lastDiveCommand = command self.diveFramesRemaining = 29 return command class GoalkeeperWithGenerator(object): def __init__(self, features): self.features = features def behavior(self): while True: features = self.features if features.ballFar: yield 'stand' else: if features.ballOnLeft: command = 'diveLeft' else: command = 'diveRight' for i in xrange(30): yield command import random f = Features() g1 = Goalkeeper(f) g2 = GoalkeeperWithGenerator(f).behavior() for i in xrange(10000): f.ballFar = random.random() > 0.1 f.ballOnLeft = random.random() < 0.5 g1action = g1.next() g2action = g2.next() print "%s\t%s\t%s\t%s" % ( f.ballFar, f.ballOnLeft, g1action, g2action) assert(g1action == g2action)