``````#!/usr/local/bin/python
#Scott Landau
#CS 380
#Assignment 1 Programming Portion in Python
#Created - 4/6/09
#Last Edited - 4/7/09

#n is going to be equal to 4 for this queens problem.
n = 4

#Assigning the empty list to initialState.
initialState = []

#Making an allDirections list.
allDirections = [[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[-1,1],[1,-1],[1,1]]

#declare the list 'state' which represents the number of non threatening
#queens that have been placed on the board.
state = []

def goal(state):
if ((len(state)) == n):
print "true"
return True
else:
print "false"
return False

#defining allRules list which the contents of which will be created in next function.
allRules = []
#defining all the rules for an nxn board. assigning these rules to allRules
def makeRules(n):
for i in range(1, (n+1), 1):
for j in range(1, (n+1), 1):
allRules.append([i,j])
return

#making a rule list. this list will probably be refined in future assignments but it will contain the one rule that is applicable in the current state. for #now we will just make it an empty list.
rule = []
#this function will apply the rule that is in the rule list to the given state, which just appends it to the state list.
def applyRule(rule,state):
state.append(rule)
return

#returns true if the row or column of 'pos' is out of bounds.
def outOfBounds(pos):
if ((pos[0] < 1) or (pos[0] > n) or (pos[1] < 1) or (pos[1] > n)):
print "true"
return True
else:
print "false"
return False

#function that will determine which positions queens cannot be placed into because they might eventually run into another queen already on the board.
def blocked(pos,state,direction):
newpos = [(pos[0]+direction[0]),(pos[1]+direction[1])]
if newpos in state:
print "true"
return True
elif outOfBounds(newpos):
print "false"
return False
else:
blocked(newpos,state,direction)

#precondition to the actions checker
def precondition(rule,state):
for k in range(0, (len(allDirections)), 1):
if not blocked(rule,state,(allDirections[k])):
print "true"
return True
else:
print "false"
return False

#running a simple test using all the functions
makeRules(n)
while not goal(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."``````
3
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by woooee

I know it's a problem with either my blocked() or precondition() functions, I just don't know which one and what the problem is.

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.

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."``````

I did test the code using print statements. I also debugged it and stepped through my while loop into functions hoping I would find the problem.

I know when the problem hits, just not exactly what it is. It comes when i am adding another coordinate to state. It adds the first one ([1,1]) fine, but then the blocked function and the precondition function should determine that the next one it runs through, which is [1,2], will eventually run into [1,1] through the direction [0,1], but it never does and it just adds this one to state anyway.

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.