### Thread: Ghost algorithm in pacman, find distance btw target

1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2011
Posts
43
Rep Power
7

#### Ghost algorithm in pacman, find distance btw target

Im implementing ghost algorithm for chasing pacman. I'm using the euclid forumla to find the distance btw the two objects, however I'm supposed to compare two different distances. How can i have two different distances from the same method? And next question is how do I associate this shortes distance with a direction (up,down,left,right)?

This is a rough code snippets:
Code:
``` public void moveGhost() {

while(!checkMove(maze.nextTile(getCurrentPos(), ghostDirection))){
maze.nextTile(getCurrentPos(),ghostDirection);

Tiles TargetRedGhost = maze.nextTile(pacman.getCurrentPos(), pacman.getDirection());
Tiles ghostRed = getCurrentPos();

ghostDirection = calculateDistanceToTarget(ghostRed, TargetRedGhost );
move();
}

public int calculateDistanceToTarget(Tiles ghostRed, Tiles pacman ) {
//Tiles coords1 = character.getCurrentPos();
double x1 = ghostRed.getX();
double y1 = ghostRed.getY();
//Tiles coords2 = character.getCurrentPos();
double x2 = pacman.getX();
double y2 = pacman.getY();

double distance;
distance = Math.sqrt(( Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2))); //or this d=<int>(sqrt(xd*xd+yd*yd));?

//need associate shortest path with direction

return ghostDirection;

}

public void move() {
x += xinc;
y += yinc;
}```
2. How can i have two different distances from the same method?
Pass the method different arguments.
how do I associate this shortest distance with a direction
Return an object of a class that contains the direction and the distance.