Adding queens to my visual chessboard.

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2008
Posts: 33
Reputation: gotm is an unknown quantity at this point 
Solved Threads: 0
gotm gotm is offline Offline
Light Poster

Adding queens to my visual chessboard.

 
0
  #1
Apr 22nd, 2009
So here is my program:

  1. #!/usr/local/bin/python
  2. #Scott Landau
  3. #CS 380
  4. #Assignment 1 Programming Portion in Python
  5. #Created - 4/6/09
  6. #Last Edited - 4/22/09
  7.  
  8. import pdb
  9.  
  10. #n is going to be equal to 4 for this queens problem.
  11. n = 4
  12.  
  13. #Assigning the empty list to initialState.
  14. initialState = []
  15.  
  16. #Making an allDirections list.
  17. allDirections = [[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[-1,1],[1,-1],[1,1]]
  18.  
  19. #declare the list 'state' which represents the number of non threatening
  20. #queens that have been placed on the board.
  21. state = []
  22.  
  23. def goal(state):
  24. if ((len(state)) == n):
  25. return True
  26. else:
  27. return False
  28.  
  29. #defining allRules list which the contents of which will be created in next function.
  30. allRules = []
  31. #defining all the rules for an nxn board. assigning these rules to allRules
  32. def makeRules(n):
  33. for i in range(0, (n), 1):
  34. for j in range(0, (n), 1):
  35. allRules.append([i,j])
  36. return
  37.  
  38. #this function will apply the rule that is in the rule list to the given state, which just appends it to the state list.
  39. def applyRule(rule,state):
  40. state.append(rule)
  41. return
  42.  
  43. #returns true if the row or column of 'pos' is out of bounds.
  44. def outOfBounds(pos):
  45. if ((pos[0] < 0) or (pos[0] > (n-1)) or (pos[1] < 0) or (pos[1] > (n-1))):
  46. return True
  47. else:
  48. return False
  49.  
  50. #function that will determine which positions queens cannot be placed into because they might eventually run into another queen already on the board.
  51. def blocked(pos,state,direction):
  52. newpos = [(pos[0]+direction[0]),(pos[1]+direction[1])]
  53. if newpos in state:
  54. return True
  55. elif outOfBounds(newpos):
  56. return False
  57. else:
  58. return True
  59. blocked(newpos,state,direction)
  60.  
  61.  
  62. #precondition to the actions checker
  63. def precondition(rule,state):
  64. for k in range(0, 8):
  65. if blocked(rule,state,(allDirections[k])):
  66. return False
  67. return True
  68.  
  69. #generating a list of candidate rules
  70. def candidateRules(state):
  71. temp = []
  72. temp = allRules
  73. for l in range(0,(len(state)),1):
  74. number = temp.count(state[l])
  75. for m in range(1,number,1):
  76. temp.remove(state[l])
  77. return temp
  78.  
  79. #applicable rules. use candidate and then precondition
  80. def applicableRules(state):
  81. newRules = candidateRules(state)
  82. for n in range(0,(len(newRules)),1):
  83. if not precondition(newRules[n],state):
  84. newRules.remove(newRules[n])
  85. return newRules
  86.  
  87. #make a rule look "pretty"
  88. def printRule(rule):
  89. print "Place a queen in " + str(rule)
  90. return
  91.  
  92. #same for a state
  93. def printState(state):
  94. hboard = "+---"
  95. vboard = "| "
  96. for a in range(0,n,1):
  97. print hboard + hboard + hboard + hboard
  98. print vboard + vboard + vboard + vboard
  99. print hboard + hboard + hboard + hboard
  100. return
  101.  
  102. #backTrack algorithm
  103. def backTrack(state):
  104. firstState = state[0]
  105. new = state[1:]
  106. number = new.count(firstState)
  107. if (number > 0):
  108. return "FAILED 1"
  109. if (goal(firstState)):
  110. return "GOAL"
  111. if ((len(state)) > depthBound):
  112. return "FAILED 3"
  113.  
  114. ruleSet = applicableRules(firstState)
  115. if (ruleSet == []):
  116. return "FAILED 4"
  117.  
  118. for r in range(0,(len(ruleSet)),1):
  119. newState = applyRule((ruleSet[r]),firstState)
  120. newStateList = state.insert(newState, 1)
  121. path = backTrack(newStateList)
  122. if (path):
  123. return path.append(ruleSet[r])
  124. return "FAILED 5"
  125.  
  126. #test
  127. printState(state)

My state is in the form [[1,1],[2,0]] etc. This is just an example, because this cannot be a valid state according to the problem.

I had to make a visual representation of a chessboard. I did that with printState(state). The problem is, right now it's just printing the chessboard, and I'm confused on how, after passing state, it can actually place a 'Q' in one of the blank spots. Maybe I did this a bad way?

Can anyone help?

Also, I asked my teacher for help on these other two things but, if anyone knows why my candidateRules(state) is printing all the rules all the time, or see any other issues with this, I would appreciate it.

I AM NOT ASKING FOR YOU GUYS TO DO THIS ASSIGNMENT. I seemed to have had someone think that I was doing that the last time I posted a question, but I came with code and have tried my hardest before coming here. There is no point to come here without trying, I'm just trying to get some extra help.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 33
Reputation: gotm is an unknown quantity at this point 
Solved Threads: 0
gotm gotm is offline Offline
Light Poster

Re: Adding queens to my visual chessboard.

 
0
  #2
Apr 22nd, 2009
Oh, and apparently my blocked function is not recursive? I thought it was, but it needs to be, so any help I'll take If not though that's fine too.
Last edited by gotm; Apr 22nd, 2009 at 2:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 12
Reputation: targ is an unknown quantity at this point 
Solved Threads: 4
targ targ is offline Offline
Newbie Poster

Re: Adding queens to my visual chessboard.

 
0
  #3
Apr 22nd, 2009
Originally Posted by gotm View Post
Oh, and apparently my blocked function is not recursive? I thought it was, but it needs to be, so any help I'll take If not though that's fine too.
You have a return before the call to blocked. A function always exits immediately when it reaches a return, skipping any lines that come after it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC