Ramaraunt1

algorithm pseudocode cheat sheet

Mar 7th, 2017
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. RECURSIVE BACKTRACKING ALGORITHM PSEUDOCODE
  2.  
  3. *CODE STRUCTURE*
  4.  
  5. create stack
  6. create list for pathed tiles
  7. pick start tile
  8. add start tile to list
  9. add start tile to stack
  10. assign start tile to cur_tile
  11. WHILE stack is not empty{
  12. IF cur_tile has neighbor unpathed
  13. {
  14. connect and move to random neighbor
  15. }
  16. ELSE IF cur_tile has no neihbors unpathed
  17. {
  18. pop from path and assign poped to cur_tile
  19. }
  20. }
  21.  
  22.  
  23. A* PATHFINDING ALGORITHIM PSEUDOCODE
  24.  
  25. *TERMS*
  26.  
  27. h-score - the heuristic cost of a tile, or how far it is from the end_tile
  28. g-score - the gained cost of a tile, or how much was spent to get to it.
  29. m-score - the move score, or the h-score + g-score
  30.  
  31. *CODE STRUCTURE*
  32.  
  33. *CREATE EVENT CODE*
  34.  
  35. create a grid of pathing nodes.
  36. create a list of neighbors for each node containing the surrounding nodes of each node
  37. create a parent variable for each node and set them to null
  38. create a child variable for each node and set to null
  39.  
  40. *FUNCTION TO ACTUALLY PATHFIND*
  41.  
  42. next_node_in_path pathfinding_function(start_node,end_node)
  43. {
  44. create OPEN priority list
  45. create CLOSED list
  46. cur_node = start_node
  47. add cur_node to CLOSED
  48. WHILE cur_node != end_node
  49. {
  50. FOR each neighbor of current node
  51. {
  52. IF neighbor is traversible (not a wall) and not in CLOSED
  53. {
  54. IF neighbor is not in OPEN
  55. {
  56. h-score of neighbor
  57. get g-score of neighbor
  58. get m-score of neighbor
  59. add neighbor to OPEN with priority of m-score
  60. }
  61. ELSE IF new g-score to neighbor is smaller than previous g-score path
  62. {
  63. calculate new g-score
  64. get m-score of neighbor
  65. add neighbor to OPEN with a priority of m-score
  66. }
  67. }
  68. }
  69. IF open is empty (no paths left!)
  70. {
  71. delete datastructures CLOSED and OPEN to avoid memory leaks!
  72. return NULL
  73. }
  74. else
  75. {
  76. new_node = ds_priority_delete_min(OPEN);
  77. new_node.parent = cur_node
  78. new_node = cur_node
  79. add cur_node to CLOSED
  80. }
  81. }
  82. WHILE cur_node != start_node
  83. {
  84. cur_node.parent.child = cur_node;
  85. cur_node = cur_node.parent;
  86. }
  87. delete datastructures CLOSED and OPEN to avoid memory leaks!
  88. return cur_node.child
  89. }
Advertisement
Add Comment
Please, Sign In to add comment