Advertisement
Nickpips

Logic

Mar 26th, 2016 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. getDirection:
  2.  
  3. Given a target point, a ghost point, find the closest path.
  4.  
  5. Lines 9-13:
  6. Go to the four squares adjacent to the ghost, and get the distance between that square and the target.
  7. The array contains the square of the distance (We're only doing comparisons, so ignore the sqrt part it doesn't matter)
  8.  
  9. Lines 15-23:
  10. Set pref to 0, set directions to { 0, 1, 2, 3 }, move distances to distancesSorted
  11.  
  12. Line 25:
  13. sort distancesSorted
  14.  
  15. Line 27:
  16. Go through each distance in order of magnitude (That's why we sorted)
  17. The smaller ones are the ones we want to go to, e.g.
  18. P = Pacman
  19. G = Ghost
  20. 0-3 = UP DOWN LEFT RIGHT, respectively
  21. P
  22. 0
  23. 2G3
  24. 1
  25.  
  26. Say the dist from 0 to P is 6, from 3 is 6, from 2 is 7, and from 1 is 8 (Not mathematically possible, but doesn't matter).
  27. Then,
  28.  
  29. distances:
  30. 0 1 2 3
  31. [ 36, 64, 49, 36 ] <- Remember, dist squared
  32.  
  33. distancesSorted:
  34. 0 1 2 3
  35. [ 64, 49, 36, 36 ]
  36.  
  37. We loop starting at 3 (since smaller means closer, and the Ghost should go up in that case)
  38.  
  39. Line 33:
  40. Get the distance (36 for the first iteration)
  41.  
  42. Line 36-38:
  43. If dist[val] == 36, push val onto poss.
  44. Since dist[0] == dist[3] == 36, 0 and 3 will be pushed.
  45.  
  46. Line 41:
  47. The ghost can go either up or right to go to the pacman, so shuffle that array to pick one randomly.
  48.  
  49. Line 44:
  50. Get the first direction in the array (It'll be something random, either 0 or 3 we don't know).
  51. Let's say that 3 was the direction that was chosen.
  52.  
  53. Line 47-52:
  54. Iterate through the array of choices [0-3] and remove the one that we chose,
  55. since it no longer matters.
  56. Now,
  57. directions only has [ 0, 1, 2 ]
  58.  
  59. Line 55:
  60. Since we picked three, the most preferred direction was 3 (Tied with 0, and broke the tie with randomness).
  61.  
  62. Line 56:
  63. Loop back and do it all over again, this time with line 36 only going through [ 0, 1, 2 ]
  64. since we already figured out that 3 was the most preferred.
  65.  
  66. -----
  67.  
  68. Now we have our list of values in order of preference
  69. [ 3, 0, 2, 1 ]
  70.  
  71. The 0, 2, 1 came in because they were the values 36, 49, 64, and our algorithm only gets the smallest one each time
  72. If we ran the program again, pref might be [ 0, 3, 2, 1 ]
  73.  
  74. Line 61:
  75. Go through the preferences in order of preference
  76.  
  77. Line 67-70:
  78. The first two just check if I'm going to run into a wall, in which case if I am the if fails and we do nothing,
  79. going on to the next option. Say to the right of the Ghost is a wall, then the if fails,
  80. we don't break and then i++ happens and now we loop back and
  81. get pref.at(1), which is 0.
  82. Now, if that's not a wall, and we aren't moving opposite that direction, well let's go that way.
  83. We don't want to move opposite or else the ghosts will flip back and forth. They must always go
  84. stright, left or right, but not backwards unless you frighten them or they swap between
  85. chasing modes.
  86.  
  87. Line 74:
  88. Return dir
  89.  
  90. Line 77-97:
  91. Figure out which direction you're going, and then set dir to be the opposite.
  92. Now change current direction to that on line 95, and also return it. (We took the Ghost by
  93. reference so that we could change it inside the method)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement