A matter of coding style:
Please use 4 spaces rather than tabs for your indentations. It's pretty much the standard. Using tabs will get you into real problems sooner or later. For me its very tough to check your code since my editor is set for the customary spaces.
One quick observation:
len(state) seems to increase in increments of 8, so the len(state) == 4 is never reached to exit the loop.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
Test each function individually by inserting print statements. Comment everything you don't want by using triple quotes """. Since you only have one while() loop, start here.
while not goal(state):
print "=====>while not goal(state) ", goal(state), state
for l in range(0, (len(allRules)), 1):
if precondition((allRules[l]),state):
applyRule((allRules[l]),state)
else:
print "Rule cannot be applied, trying next rule in list."
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
A "print 'true'" statement isn't any good for debugging. It doesn't tell you anything and you don't know which function it came from. If you add the print statement to the while() loop it tells you that state[] gets very large, very quickly. That taken along with vegaseat's point takes you to
def goal(state):
print "def goal", n, len(state)
if ((len(state)) == n):
## print "true"
return True
The print statement here makes it obvious that len(state) is never equal to n. How to solve it is up to you. As a general rule though, you never want to use "equals" in a conditional, but "<" or ">" as that is only a single test and will catch an increment that jumps by more than one. While "<=" or ">=" can also be used, it is two tests per loop instead of one. HTH.
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714