#!/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."
gotm
0
Light Poster
Recommended Answers
Jump to PostA 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.…
Jump to PostTest 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: …
All 5 Replies
gotm
0
Light Poster
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
woooee
814
Nearly a Posting Maven
gotm
0
Light Poster
woooee
814
Nearly a Posting Maven
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.