top of page

IMPLEMENTATION

This is the part of coding where A* Search algorithm is implemented as Path AStar( with some parameters) in this Pac-Man game
*Note that game implementation use 2D grid for the movements of ghosts and Pac-Man

WhatsApp Image 2020-07-18 at 16.02.22.jp

First, some list  will be created in order to stores all possible nodes and paths.

Next, coders will assign the current location of the ghost which is the number of that node as the first node and current location of Pac-Man as the goal/last node into extend.

WhatsApp Image 2020-07-18 at 20.40.14.jp

Then, it will call the addToTail function that located in another part of coding which called as class Path(). In this function, it will calculate the distance between the ghost and Pac-Man location and assign to a variable called  distToFinish by using the formula in 290.
Now, distToFinish can be assumed as it h(n)

WhatsApp Image 2020-07-18 at 16.03.09.jp

After that, moving to this loop part where it keep looping until it find the optimal path to the goal node where it stores that path in a variable called winning Path. Next, it will update the distance value which is the g(n) located in class Path(). This distance value is the actual cost from the starting node to reach the goal node.

​

In addition, for the next movement in catching Pac-Man, if the path is shorter than the exist one it will update the the winningPath by comparing the distance value of current path and the exist path. The implementation can be seen starting line of code 186.

IMPLEMENTING A* SEARCH ALGORITHM

WhatsApp Image 2020-07-18 at 16.06.03.jp

Despite all of that, another characteristic of A* Search algorithm is only to use the optimal path. Hence, this is the another scenario where the current path for the ghost to catch the Pac-Man is longer than the exist one. To solve that, it will compare the distance value which is the g(n). If distance value of current path is larger than distance of exist path, then it will update the current path to use the exist path that have lower distance. Thus, the ghost is completely implement the A* Seach algorithm where it always choose the ideal path in order to catch the Pac-Man.

WhatsApp Image 2020-07-18 at 20.38.37.jp

Lastly, after some paths that have been created this is the implementation of f(n) = g(n) + h(n) and sorting it in meaningful way into another list called big created at the beginning part.

​

However, sometimes error can happen where there is no path existed for the ghost to choose from its starting node to goal node in the big list. Thus error message "OH NOOO" will be appear at output console.

Wanna see how its looks like?? ?

2020  by Pac-Man. Created by 5 students UPM.

bottom of page