345449.vhj5l3oj7.asia
MouseTrap2Test.py
2001-12-26T16:00:00Z
2001-12-26T16:00:00Z
<br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c04:mousetrap2:MouseTrap2Test.py
# A better mousetrap using tables
import string, sys
sys.path += ['../statemachine', '../mouse']
from State import State
from StateMachine import StateMachine
from MouseAction import MouseAction
class StateT(State):
def __init__(self):
self.transitions = None
def next(self, input):
if self.transitions.has_key(input):
return self.transitions[input]
else:
raise "Input not supported for current state"
class Waiting(StateT):
def run(self):
print "Waiting: Broadcasting cheese smell"
def next(self, input):
# Lazy initialization:
if not self.transitions:
self.transitions = {
MouseAction.appears : MouseTrap.luring
}
return StateT.next(self, input)
class Luring(StateT):
def run(self):
print "Luring: Presenting Cheese, door open"
def next(self, input):
# Lazy initialization:
if not self.transitions:
self.transitions = {
MouseAction.enters : MouseTrap.trapping,
MouseAction.runsAway : MouseTrap.waiting
}
return StateT.next(self, input)
class Trapping(StateT):
def run(self):
print "Trapping: Closing door"
def next(self, input):
# Lazy initialization:
if not self.transitions:
self.transitions = {
MouseAction.escapes : MouseTrap.waiting,
MouseAction.trapped : MouseTrap.holding
}
return StateT.next(self, input)
class Holding(StateT):
def run(self):
print "Holding: Mouse caught"
def next(self, input):
# Lazy initialization:
if not self.transitions:
self.transitions = {
MouseAction.removed : MouseTrap.waiting
}
return StateT.next(self, input)
class MouseTrap(StateMachine):
def __init__(self):
# Initial state
StateMachine.__init__(self, MouseTrap.waiting)
# Static variable initialization:
MouseTrap.waiting = Waiting()
MouseTrap.luring = Luring()
MouseTrap.trapping = Trapping()
MouseTrap.holding = Holding()
moves = map(string.strip,
open("../mouse/MouseMoves.txt").readlines())
mouseMoves = map(MouseAction, moves)
MouseTrap().runAll(mouseMoves)
#:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
2001-12-26T16:00:00Z