Advertisement
Guest User

Untitled

a guest
Dec 11th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. doolhof = [
  2. ['.', '.', '.', '.', '.'],
  3. ['.', '*', '.', '.', '.'],
  4. ['.', '.', 'S', '*', 'F'],
  5. ['.', '*', '.', '.', '.'],
  6. ['.', '*', '*', '.', '.']
  7. ]
  8.  
  9.  
  10.  
  11. #Geeft het korste pad weer
  12. def korstePad(doolhof, startX, startY, finishX, finishY):
  13. index = 1
  14. aantalStappen = 0
  15. loopController = True
  16.  
  17. maxAaantalStappen = berekenMaxAantalStappen(doolhof)
  18.  
  19. PositionCheck(doolhof, startX, startY, index)
  20.  
  21.  
  22. #Vult grid met cijfers om het kortste pad te vinden
  23. while(loopController):
  24.  
  25. for rij in range(len(doolhof)):
  26. for kolom in range(len(doolhof[0])):
  27. if (doolhof[rij][kolom] == index):
  28. PositionCheck(doolhof, rij, kolom, index + 1)
  29.  
  30. index += 1
  31.  
  32. #Als de waarde op de finish is verandert, dan betekent is dit in de laatste iteratie gebeurd. Hierdoor kunnen we aannemen dat de index het # stappen is
  33. if (doolhof[finishX][finishY] != "F"):
  34. loopController = False
  35.  
  36. if (index > maxAaantalStappen ):
  37. loopController = False
  38. print("Pad bestaat niet")
  39. #RETURNEN NAAR MENU ANDERS GAAT DE FUNCTIE GEWOON VERDER NAAR HET VISUALEREN
  40.  
  41.  
  42. aantalStappen = index #Aantal stappen
  43. print(aantalStappen, "Stappen voor van S naar F") #Weghalen
  44.  
  45. visualiseerKorstepad(doolhof, startX, startY, finishX, finishY, index - 1)
  46.  
  47. #Berekent het maximaal aantal stappen
  48. def berekenMaxAantalStappen(doolhof):
  49. lengte = len(doolhof)
  50. breedte = len(doolhof[0])
  51.  
  52. return lengte*breedte
  53.  
  54. def visualiseerKorstepad(doolhof, startX, startY, finishX, finishY, n):
  55. doolhof[finishX][finishY] = "F"
  56. loopController = True
  57.  
  58. visualiseerPad(doolhof, finishX, finishY, n)
  59.  
  60. while (loopController):
  61.  
  62. for rij in range(len(doolhof)):
  63. for kolom in range(len(doolhof[0])):
  64. if (doolhof[rij][kolom] == "O"):
  65. visualiseerPad(doolhof, rij, kolom, n)
  66. #break?
  67.  
  68. n -= 1
  69. if (n == 0):
  70. loopController = False
  71.  
  72. verwijderGetallenInDoolhof(doolhof)
  73. toonDoolhof(doolhof)
  74.  
  75.  
  76. def visualiseerPad(doolhof, x, y, n):
  77. if ((NotoutOfBoundsCheck(x - 1, y) and (doolhof[x - 1][y] == n))):
  78. # Function that checks endposition
  79. doolhof[x - 1][y] = "O"
  80.  
  81. elif ((NotoutOfBoundsCheck(x + 1, y)) and (doolhof[x + 1][y] == n)):
  82. # Function that checks endposition
  83. doolhof[x + 1][y] = "O"
  84.  
  85. elif ((NotoutOfBoundsCheck(x, y - 1)) and (doolhof[x][y - 1] == n )):
  86. # Function that checks endposition
  87. doolhof[x][y - 1] = "O"
  88.  
  89. elif ((NotoutOfBoundsCheck(x, y + 1)) and (doolhof[x][y + 1] == n )):
  90. # Function that checks endposition
  91. doolhof[x][y + 1] = "O"
  92.  
  93.  
  94. #Checkt startPositie
  95. def PositionCheck(doolhof, x, y, n):
  96.  
  97. if ((NotoutOfBoundsCheck(x - 1, y)) and ((doolhof[x - 1][y] == ".") or (doolhof[x - 1][y] == "F"))):
  98. #Function that checks endposition
  99. doolhof[x - 1][y] = n
  100.  
  101. if ((NotoutOfBoundsCheck(x + 1, y)) and ((doolhof[x + 1][y] == ".") or (doolhof[x + 1][y] == "F"))):
  102. # Function that checks endposition
  103. doolhof[x + 1][y] = n
  104.  
  105. if ((NotoutOfBoundsCheck(x, y-1)) and ((doolhof[x][y - 1] == ".") or (doolhof[x][y - 1] == "F"))):
  106. # Function that checks endposition
  107. doolhof[x][y - 1] = n
  108.  
  109. if ((NotoutOfBoundsCheck(x, y + 1)) and ((doolhof[x][y + 1] == ".") or doolhof[x][y + 1] == "F")):
  110. # Function that checks endposition
  111. doolhof[x][y + 1] = n
  112.  
  113. #Controleert out of bounds errors
  114. def NotoutOfBoundsCheck(x, y):
  115.  
  116. # Controleert de lengte van het doolhof
  117. if ((x < 0) or (x >= len(doolhof))):
  118. return False
  119.  
  120. # Controleert de breedte van het doolhof
  121. elif ((y < 0) or (y >= len(doolhof[0]))):
  122. return False
  123.  
  124. else:
  125. return True
  126.  
  127. def toonDoolhof(doolhof):
  128. for i in range(len(doolhof[0]) + 2):
  129. print("-", end=" ")
  130. print('\n')
  131.  
  132. for i in range(len(doolhof)):
  133. if i != 0:
  134. print('\n')
  135. print('|', end=" ")
  136. for j in range(len(doolhof[i])):
  137. print(doolhof[i][j], end=" ")
  138. print('|', end=" ")
  139.  
  140. print('\n')
  141. for i in range(len(doolhof[0]) + 2):
  142. print("-", end=" ")
  143.  
  144. print('\n')
  145.  
  146. def verwijderGetallenInDoolhof(doolhof):
  147.  
  148. for rij in range(len(doolhof)):
  149. for kolom in range(len(doolhof[0])):
  150. if (coordinaatIsInteger(rij, kolom, doolhof)):
  151. doolhof[rij][kolom] = "."
  152.  
  153. def coordinaatIsInteger(rij, kolom, doolhof):
  154.  
  155. if (doolhof[rij][kolom] == "*"):
  156. return False
  157.  
  158. elif (doolhof[rij][kolom] == "S"):
  159. return False
  160.  
  161. elif (doolhof[rij][kolom] == "F"):
  162. return False
  163.  
  164. elif (doolhof[rij][kolom] == "O"):
  165. return False
  166.  
  167. else:
  168. return True
  169.  
  170. korstePad(doolhof, 2, 2, 2, 4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement