Advertisement
Guest User

Untitled

a guest
Feb 1st, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. import random
  2.  
  3. goalState = [1,2,3,4,5,6,7,8,0]
  4. startState = []
  5. frontier = [[startState]]
  6. currState = []
  7. rand = 0
  8. temp = 0
  9. swap = 0
  10. index = 0
  11.  
  12.  
  13.  
  14. def createPuzzle():
  15. max = len(goalState)
  16. for eachPass in xrange(max):
  17. rand = random.randint(0,8)
  18. if rand not in startState:
  19. startState.append(rand)
  20.  
  21. elif rand in startState:
  22. while rand in startState:
  23. rand = random.randint(0,8)
  24. startState.append(rand)
  25.  
  26.  
  27.  
  28. print startState[0], startState[1], startState[2]
  29. print startState[3], startState[4], startState[5]
  30. print startState[6], startState[7], startState[8]
  31.  
  32.  
  33.  
  34.  
  35. def goal(state):
  36. if state == [1,2,3,4,5,6,7,8,0]:
  37. return True
  38. else:
  39. return False
  40.  
  41. def swap(a,b,currState):
  42.  
  43. print currState
  44.  
  45. currState[a], currState[b] = currState[b], currState[a]
  46. return currState
  47.  
  48. def addFrontier(state, frontier):
  49.  
  50. if not frontier == []:
  51. global currState
  52. currState = frontier[0]
  53. else:
  54. global currState
  55. currState = startState
  56.  
  57. if state[0] == 0:
  58.  
  59. swap(0,1)
  60. frontier.append(currState)
  61.  
  62. swap(0,3)
  63. frontier.append(currState)
  64.  
  65. elif state[1] == 0:
  66.  
  67. swap(1,0,currState)
  68. frontier.append(currState)
  69.  
  70. swap(1,2,currState)
  71. frontier.append(currState)
  72.  
  73. swap(1,4,currState)
  74. frontier.append(currState)
  75.  
  76.  
  77. elif state[2] == 0:
  78.  
  79. swap(2,1,currState)
  80. frontier.append(currState)
  81.  
  82.  
  83. swap(2,5,currState)
  84. frontier.append(currState)
  85.  
  86.  
  87. elif state[3] == 0:
  88.  
  89. swap(3,0,currState)
  90. frontier.append(currState)
  91.  
  92. swap(3,4,currState)
  93. frontier.append(currState)
  94.  
  95.  
  96. swap(3,6,currState)
  97. frontier.append(currState)
  98.  
  99.  
  100. elif state[4] == 0:
  101.  
  102. swap(4,1,currState)
  103. frontier.append(currState)
  104.  
  105.  
  106. swap(4,3,currState)
  107. frontier.append(currState)
  108.  
  109.  
  110. swap(4,5,currState)
  111. frontier.append(currState)
  112.  
  113.  
  114. swap(4,7,currState)
  115. frontier.append(currState)
  116.  
  117.  
  118. elif state[5] == 0:
  119.  
  120. swap(5,2,currState)
  121. frontier.append(currState)
  122.  
  123. swap(5,4,currState)
  124. frontier.append(currState)
  125.  
  126.  
  127. swap(5,8,currState)
  128. frontier.append(currState)
  129.  
  130. elif state[6] == 0:
  131.  
  132. swap(6,3,currState)
  133. frontier.append(currState)
  134.  
  135.  
  136. swap(6,7,currState)
  137. frontier.append(currState)
  138.  
  139.  
  140. elif state[7] == 0:
  141.  
  142. swap(7,4,currState)
  143. frontier.append(currState)
  144.  
  145.  
  146. swap(7,6,currState)
  147. frontier.append(currState)
  148.  
  149. swap(7,8,currState)
  150. frontier.append(currState)
  151.  
  152. elif state[8] == 0:
  153.  
  154. swap(8,5,currState)
  155. frontier.append(currState)
  156.  
  157.  
  158. swap(8,7,currState)
  159. frontier.append(currState)
  160.  
  161. def search(state,frontier):
  162.  
  163. addFrontier(state, frontier)
  164. print frontier
  165.  
  166.  
  167.  
  168. def main():
  169. createPuzzle()
  170. addFrontier(startState, frontier)
  171. search(startState, frontier)
  172.  
  173.  
  174. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement