Subversion Repositories programming

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
423 ira 1
#!/usr/bin/env python
2
 
3
__author__    = "Ira W. Snyder (devel@irasnyder.com)"
4
__copyright__ = "Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)"
5
__license__   = "GNU GPL v2 (or, at your option, any later version)"
6
 
7
from PyCompat import *
8
import re
9
 
10
# Solution to the Monkey and Banana Problem
11
 
12
class Action (object):
13
	def __init__ (self, State):
14
		self.State = State
15
		self.PC = []
16
		self.AL = []
17
		self.DL = []
18
 
19
	def __repr__ (self):
20
		return '%s' % self.State
21
 
22
	def meetsPreconditions (self):
23
		for e in self.PC:
24
			if e not in self.State:
25
				return False
26
 
27
		return True
28
 
29
	def takeAction (self):
30
		if not self.meetsPreconditions ():
31
			raise ValueError
32
 
33
		for e in self.AL:
34
			self.State.add (e)
35
 
36
		for e in self.DL:
37
			self.State.remove (e)
38
 
39
		# Return the new State
40
		return self.State
41
 
42
	def isGoal (self):
43
		return 'HasBanana()' in self.State
44
 
45
	def findMonkeyPosition (self):
46
		regex = re.compile ('At\(Monkey,(.+)\)')
47
 
48
		for e in self.State:
49
			if regex.match (e):
50
				return regex.match (e).group (1)
51
 
52
	def findBoxPosition (self):
53
		regex = re.compile ('At\(Box,(.+)\)')
54
 
55
		for e in self.State:
56
			if regex.match (e):
57
				return regex.match (e).group (1)
58
 
59
	def monkeyOnBox (self):
60
		return 'OnBox()' in self.State
61
 
62
 
63
class Walk (Action):
64
	(DOOR, WINDOW, MIDDLE) = ('Door', 'Window', 'Middle')
65
 
66
	def __init__ (self, State, F, T):
67
		Action.__init__ (self, State)
68
		self.F = F
69
		self.T = T
70
 
71
		self.PC = ['At(Monkey,%s)' % (F,) , 'OnFloor()']
72
		self.AL = ['At(Monkey,%s)' % (T,)]
73
		self.DL = ['At(Monkey,%s)' % (F,)]
74
 
75
class Push (Action):
76
	(DOOR, WINDOW, MIDDLE) = ('Door', 'Window', 'Middle')
77
 
78
	def __init__ (self, State, F, T):
79
		Action.__init__ (self, State)
80
		self.F = F
81
		self.T = T
82
 
83
		self.PC = ['At(Monkey,%s)' % (F,) , 'At(Box,%s)' % (F,) , 'OnFloor()']
84
		self.AL = ['At(Monkey,%s)' % (T,) , 'At(Box,%s)' % (T,)]
85
		self.DL = ['At(Monkey,%s)' % (F,) , 'At(Box,%s)' % (F,)]
86
 
87
class Climb (Action):
88
	(UP, DOWN) = ('Up', 'Down')
89
 
90
	def __init__ (self, State, D):
91
		Action.__init__ (self, State)
92
		self.D = D
93
 
94
		if D == self.UP:
95
			self.PC = ['At(Monkey,%s)' % (self.findBoxPosition()), \
96
						'At(Box,%s)' % (self.findMonkeyPosition()), 'OnFloor()']
97
			self.AL = ['OnBox()']
98
			self.DL = ['OnFloor()']
99
		else: # D == DOWN
100
			self.PC = ['At(Monkey,%s)' % (self.findBoxPosition()), \
101
						'At(Box,%s)' % (self.findMonkeyPosition()), 'OnBox()']
102
			self.AL = ['OnFloor()']
103
			self.DL = ['OnBox()']
104
 
105
class Grab (Action):
106
	(DOOR, WINDOW, MIDDLE) = ('Door', 'Window', 'Middle')
107
 
108
	def __init__ (self, State):
109
		Action.__init__ (self, State)
110
		self.PC = ['OnBox()', 'At(Monkey,%s)' % (self.MIDDLE), 'NoBanana()']
111
		self.AL = ['HasBanana()']
112
		self.DL = ['NoBanana()']
113
 
114
# vim: set ts=4 sts=4 sw=4:
115