How come this just hangs, after debugging I found it never returns True for precondition even though it should on the very first go around....ugh!!!
#!/usr/local/bin/python
#Scott Landau
#CS 380
#Assignment 1 Programming Portion in Python
#Created - 4/6/09
#Last Edited - 4/8/09
import pdb
#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):
return True
else:
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(0, (n), 1):
for j in range(0, (n), 1):
allRules.append([i,j])
return
#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] < 0) or (pos[0] > (n-1)) or (pos[1] < 0) or (pos[1] > (n-1))):
return True
else:
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:
return True
elif outOfBounds(newpos):
return False
else:
return True
blocked(newpos,state,direction)
#precondition to the actions checker
def precondition(rule,state):
if not blocked(rule,state,(allDirections[0])):
if not blocked(rule,state,(allDirections[1])):
if not blocked(rule,state,(allDirections[2])):
if not blocked(rule,state,(allDirections[3])):
if not blocked(rule,state,(allDirections[4])):
if not blocked(rule,state,(allDirections[5])):
if not blocked(rule,state,(allDirections[6])):
if not blocked(rule,state,(allDirections[7])):
return True
else:
return False
#running a simple test using all the functions
pdb.set_trace()
makeRules(n)
while not goal(state):
for l in range(0, (len(allRules)), 1):
if precondition((allRules[l]),state):
applyRule((allRules[l]),state)
print state
print state