#!/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):
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(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)):
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])]
for k in range(0, (len(state)), 1):
if (newpos == state[k]):
return True
print "True"
elif outOfBounds(newpos):
return False
print "False"
else:
blocked(newpos,state,direction)
state = [[1,2],[2,0]]
direction = [1,0]
pos = [1,0]
blocked(pos,state,direction)
That last function there is the one I am having problems with, I think. This is the N-Queens chess board problem, with a 4x4 board, where you have to place n queens so none of them can attack each other. This is just the basics to the whole problem but I just want to have a working "blocked" function. I thought my functions theory was correct, along with the code, but I tried putting test print lines in there to see if it was outputting anything and I am getting no output.
Any help?