Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- getDirection:
- Given a target point, a ghost point, find the closest path.
- Lines 9-13:
- Go to the four squares adjacent to the ghost, and get the distance between that square and the target.
- The array contains the square of the distance (We're only doing comparisons, so ignore the sqrt part it doesn't matter)
- Lines 15-23:
- Set pref to 0, set directions to { 0, 1, 2, 3 }, move distances to distancesSorted
- Line 25:
- sort distancesSorted
- Line 27:
- Go through each distance in order of magnitude (That's why we sorted)
- The smaller ones are the ones we want to go to, e.g.
- P = Pacman
- G = Ghost
- 0-3 = UP DOWN LEFT RIGHT, respectively
- P
- 0
- 2G3
- 1
- 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).
- Then,
- distances:
- 0 1 2 3
- [ 36, 64, 49, 36 ] <- Remember, dist squared
- distancesSorted:
- 0 1 2 3
- [ 64, 49, 36, 36 ]
- We loop starting at 3 (since smaller means closer, and the Ghost should go up in that case)
- Line 33:
- Get the distance (36 for the first iteration)
- Line 36-38:
- If dist[val] == 36, push val onto poss.
- Since dist[0] == dist[3] == 36, 0 and 3 will be pushed.
- Line 41:
- The ghost can go either up or right to go to the pacman, so shuffle that array to pick one randomly.
- Line 44:
- Get the first direction in the array (It'll be something random, either 0 or 3 we don't know).
- Let's say that 3 was the direction that was chosen.
- Line 47-52:
- Iterate through the array of choices [0-3] and remove the one that we chose,
- since it no longer matters.
- Now,
- directions only has [ 0, 1, 2 ]
- Line 55:
- Since we picked three, the most preferred direction was 3 (Tied with 0, and broke the tie with randomness).
- Line 56:
- Loop back and do it all over again, this time with line 36 only going through [ 0, 1, 2 ]
- since we already figured out that 3 was the most preferred.
- -----
- Now we have our list of values in order of preference
- [ 3, 0, 2, 1 ]
- The 0, 2, 1 came in because they were the values 36, 49, 64, and our algorithm only gets the smallest one each time
- If we ran the program again, pref might be [ 0, 3, 2, 1 ]
- Line 61:
- Go through the preferences in order of preference
- Line 67-70:
- 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,
- going on to the next option. Say to the right of the Ghost is a wall, then the if fails,
- we don't break and then i++ happens and now we loop back and
- get pref.at(1), which is 0.
- Now, if that's not a wall, and we aren't moving opposite that direction, well let's go that way.
- We don't want to move opposite or else the ghosts will flip back and forth. They must always go
- stright, left or right, but not backwards unless you frighten them or they swap between
- chasing modes.
- Line 74:
- Return dir
- Line 77-97:
- Figure out which direction you're going, and then set dir to be the opposite.
- Now change current direction to that on line 95, and also return it. (We took the Ghost by
- reference so that we could change it inside the method)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement