Subversion Repositories programming

Rev

Rev 423 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 423 Rev 429
Line 5... Line 5...
5
__license__   = "GNU GPL v2 (or, at your option, any later version)"
5
__license__   = "GNU GPL v2 (or, at your option, any later version)"
6
 
6
 
7
from PyCompat import *
7
from PyCompat import *
8
import re
8
import re
9
 
9
 
-
 
10
LOCATIONS = (DOOR, WINDOW, MIDDLE) = ('Door', 'Window', 'Middle')
-
 
11
BOX_LOC   = (UP, DOWN) = ('Up', 'Down')
-
 
12
 
10
# Solution to the Monkey and Banana Problem
13
# Solution to the Monkey and Banana Problem
11
 
14
 
12
class Action (object):
15
class Action (object):
13
	def __init__ (self, State):
16
	def __init__ (self, State):
14
		self.State = State
17
		self.State = State
15
		self.PC = []
18
		self.PC = [] # Preconditions
-
 
19
		self.NPC= [] # Negative Preconditions
16
		self.AL = []
20
		self.AL = [] # Add List
17
		self.DL = []
21
		self.DL = [] # Delete List
18
 
22
 
19
	def __repr__ (self):
23
	def __repr__ (self):
20
		return '%s' % self.State
24
		return '%s' % self.State
21
 
25
 
22
	def meetsPreconditions (self):
26
	def meetsPreconditions (self):
23
		for e in self.PC:
27
		for e in self.PC:
24
			if e not in self.State:
28
			if e not in self.State:
25
				return False
29
				return False
26
 
30
 
-
 
31
		for e in self.NPC:
-
 
32
			if e in self.State:
-
 
33
				return False
-
 
34
 
27
		return True
35
		return True
28
 
36
 
29
	def takeAction (self):
37
	def takeAction (self):
30
		if not self.meetsPreconditions ():
38
		if not self.meetsPreconditions ():
31
			raise ValueError
39
			raise ValueError
Line 59... Line 67...
59
	def monkeyOnBox (self):
67
	def monkeyOnBox (self):
60
		return 'OnBox()' in self.State
68
		return 'OnBox()' in self.State
61
 
69
 
62
 
70
 
63
class Walk (Action):
71
class Walk (Action):
64
	(DOOR, WINDOW, MIDDLE) = ('Door', 'Window', 'Middle')
-
 
65
 
-
 
66
	def __init__ (self, State, F, T):
72
	def __init__ (self, State, F, T):
67
		Action.__init__ (self, State)
73
		Action.__init__ (self, State)
68
		self.F = F
74
		self.F = F
69
		self.T = T
75
		self.T = T
70
 
76
 
71
		self.PC = ['At(Monkey,%s)' % (F,) , 'OnFloor()']
77
		self.PC = ['At(Monkey,%s)' % (F,) , 'OnFloor()']
-
 
78
		self.NPC= ['OnBox()', 'At(Monkey,%s)' % (T,)]
72
		self.AL = ['At(Monkey,%s)' % (T,)]
79
		self.AL = ['At(Monkey,%s)' % (T,)]
73
		self.DL = ['At(Monkey,%s)' % (F,)]
80
		self.DL = ['At(Monkey,%s)' % (F,)]
74
 
81
 
75
class Push (Action):
82
class Push (Action):
76
	(DOOR, WINDOW, MIDDLE) = ('Door', 'Window', 'Middle')
-
 
77
 
-
 
78
	def __init__ (self, State, F, T):
83
	def __init__ (self, State, F, T):
79
		Action.__init__ (self, State)
84
		Action.__init__ (self, State)
80
		self.F = F
85
		self.F = F
81
		self.T = T
86
		self.T = T
82
 
87
 
83
		self.PC = ['At(Monkey,%s)' % (F,) , 'At(Box,%s)' % (F,) , 'OnFloor()']
88
		self.PC = ['At(Monkey,%s)' % (F,) , 'At(Box,%s)' % (F,) , 'OnFloor()']
-
 
89
		self.NPC= ['At(Monkey,%s)' % (T,) , 'At(Box,%s)' % (T,), 'OnBox()']
84
		self.AL = ['At(Monkey,%s)' % (T,) , 'At(Box,%s)' % (T,)]
90
		self.AL = ['At(Monkey,%s)' % (T,) , 'At(Box,%s)' % (T,)]
85
		self.DL = ['At(Monkey,%s)' % (F,) , 'At(Box,%s)' % (F,)]
91
		self.DL = ['At(Monkey,%s)' % (F,) , 'At(Box,%s)' % (F,)]
86
 
92
 
87
class Climb (Action):
93
class Climb (Action):
88
	(UP, DOWN) = ('Up', 'Down')
-
 
89
 
-
 
90
	def __init__ (self, State, D):
94
	def __init__ (self, State, D):
91
		Action.__init__ (self, State)
95
		Action.__init__ (self, State)
92
		self.D = D
96
		self.D = D
93
 
97
 
94
		if D == self.UP:
98
		if D == UP:
95
			self.PC = ['At(Monkey,%s)' % (self.findBoxPosition()), \
99
			self.PC = ['At(Monkey,%s)' % (self.findBoxPosition()), \
96
						'At(Box,%s)' % (self.findMonkeyPosition()), 'OnFloor()']
100
						'At(Box,%s)' % (self.findMonkeyPosition()), 'OnFloor()']
-
 
101
			self.NPC= ['OnBox()']
97
			self.AL = ['OnBox()']
102
			self.AL = ['OnBox()']
98
			self.DL = ['OnFloor()']
103
			self.DL = ['OnFloor()']
99
		else: # D == DOWN
104
		else: # D == DOWN
100
			self.PC = ['At(Monkey,%s)' % (self.findBoxPosition()), \
105
			self.PC = ['At(Monkey,%s)' % (self.findBoxPosition()), \
101
						'At(Box,%s)' % (self.findMonkeyPosition()), 'OnBox()']
106
						'At(Box,%s)' % (self.findMonkeyPosition()), 'OnBox()']
102
			self.AL = ['OnFloor()']
107
			self.AL = ['OnFloor()']
103
			self.DL = ['OnBox()']
108
			self.DL = ['OnBox()']
104
 
109
 
105
class Grab (Action):
110
class Grab (Action):
106
	(DOOR, WINDOW, MIDDLE) = ('Door', 'Window', 'Middle')
-
 
107
 
-
 
108
	def __init__ (self, State):
111
	def __init__ (self, State):
109
		Action.__init__ (self, State)
112
		Action.__init__ (self, State)
110
		self.PC = ['OnBox()', 'At(Monkey,%s)' % (self.MIDDLE), 'NoBanana()']
113
		self.PC = ['OnBox()', 'At(Monkey,%s)' % (MIDDLE), 'NoBanana()']
-
 
114
		self.NPC= ['OnFloor()']
111
		self.AL = ['HasBanana()']
115
		self.AL = ['HasBanana()']
112
		self.DL = ['NoBanana()']
116
		self.DL = ['NoBanana()']
113
 
117
 
114
# vim: set ts=4 sts=4 sw=4:
118
# vim: set ts=4 sts=4 sw=4:
115
 
119