Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. world.loadGraph(roomGraph)
  2.  
  3. # UNCOMMENT TO VIEW MAP
  4. world.printRooms()
  5.  
  6.  
  7. player = Player("Name", world.startingRoom)
  8.  
  9.  
  10. # FILL THIS IN
  11. # I will use this travesal path array to pass the tests
  12. traversalPath = []
  13. # traversal obj will contain all my rooms
  14. traversalRooms = {}
  15.  
  16. backtrack_stack = []
  17.  
  18. traversalMaze = True
  19.  
  20. while traversalMaze:
  21. # get the current_room we are in
  22. current_room = player.currentRoom.id
  23. print(f"We are at room {current_room}")
  24. print(traversalRooms)
  25. # if the room we are in doesnt exist on the graph yet
  26. # lets add the exits object to it
  27. if current_room not in traversalRooms:
  28. # instantiate a all exits obj
  29. all_possible_exits = {}
  30. for exit in player.currentRoom.getExits():
  31. all_possible_exits[exit] = "?"
  32. traversalRooms[current_room] = all_possible_exits
  33. # we need all of the current exits for the room
  34. all_current_exits = traversalRooms[current_room]
  35. if "n" in all_current_exits and all_current_exits["n"] == "?":
  36. # print('north')
  37. # append the direction we are going to the traversalPath
  38. traversalPath.append("n")
  39. # travel to the north since north exists
  40. player.travel("n")
  41. # get the new room we are in
  42. room_to_the_north = player.currentRoom.id
  43. traversalRooms[current_room]["n"] = room_to_the_north
  44. # add it if it doesnt exist on the graph or else we will have a broken graph
  45. if room_to_the_north not in traversalRooms:
  46. all_possible_exits = {}
  47. for exit in player.currentRoom.getExits():
  48. all_possible_exits[exit] = "?"
  49. traversalRooms[room_to_the_north] = all_possible_exits
  50. else:
  51. # set the current room's opposite to be the entrace that we have which is current_room
  52. traversalRooms[room_to_the_north]["s"] = current_room
  53. # add it to the backtrack stack
  54. backtrack_stack.append('s')
  55. elif "s" in all_current_exits and all_current_exits["s"] == "?":
  56. # print('south')
  57. # append the direction we are going to the traversalPath
  58. traversalPath.append("s")
  59. # travel to the south since south exists
  60. player.travel("s")
  61. # get the new room we are in
  62. room_to_the_south = player.currentRoom.id
  63. traversalRooms[current_room]["s"] = room_to_the_south
  64. if room_to_the_south not in traversalRooms:
  65. all_possible_exits = {}
  66. for exit in player.currentRoom.getExits():
  67. all_possible_exits[exit] = "?"
  68. traversalRooms[room_to_the_south] = all_possible_exits
  69. else:
  70. # set the current room's opposite to be the entrace that we have which is current_room
  71. traversalRooms[room_to_the_south]["n"] = current_room
  72. # add it to the backtrack stack
  73. backtrack_stack.append('n')
  74. elif "w" in all_current_exits and all_current_exits["w"] == "?":
  75. # rinse and repeat with the algo on south and north
  76. # print('west')
  77. traversalPath.append("w")
  78. # print('debugging')
  79. player.travel("w")
  80. room_to_the_west = player.currentRoom.id
  81. traversalRooms[current_room]["w"] = room_to_the_west
  82. if room_to_the_west not in traversalRooms:
  83. all_possible_exits = {}
  84. for exit in player.currentRoom.getExits():
  85. all_possible_exits[exit] = "?"
  86. traversalRooms[room_to_the_west] = all_possible_exits
  87. else:
  88. # set the current room's opposite to be the entrace that we have which is current_room
  89. traversalRooms[room_to_the_west]["e"] = current_room
  90. # add it to the backtrack stack
  91. backtrack_stack.append('e')
  92. elif "e" in all_current_exits and all_current_exits["e"] == "?":
  93. # rinse and repeat with the algo on south and north
  94. # print('west')
  95. traversalPath.append("e")
  96. # print('debugging')
  97. player.travel("e")
  98. room_to_the_east = player.currentRoom.id
  99. traversalRooms[current_room]["e"] = room_to_the_east
  100. if room_to_the_east not in traversalRooms:
  101. all_possible_exits = {}
  102. for exit in player.currentRoom.getExits():
  103. all_possible_exits[exit] = "?"
  104. traversalRooms[room_to_the_east] = all_possible_exits
  105. else:
  106. # set the current room's opposite to be the entrace that we have which is current_room
  107. traversalRooms[room_to_the_east]["w"] = current_room
  108. # add it to the backtrack stack
  109. backtrack_stack.append('w')
  110. else:
  111. # if we have no backtrack to do we will break the while loop
  112. if len(backtrack_stack) == 0:
  113. print(' \n\n\n\n\n\nend\n\n\n\n\n\n')
  114. traversalMaze = False
  115. else:
  116. # Just like DFS we get the last element and work on it.
  117. # So we can backtrack properly
  118. path_to_backtrack_travel = backtrack_stack.pop()
  119. # Travel back to the backtrack travel path
  120. player.travel(path_to_backtrack_travel)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement