simulation/ghost1.c
void
attack(int px, int py, int gx, int gy, int direction, double *next_x, double *next_y)
{
	//printf("In Attack\n");

    int mark = 0;
	if (px < gx && gy != 1 && gy != 3) 		// can move left
	{
		*next_x = gx - 1;	// pre move left
  		mark = -1;
	}
	else if (px > gx && gy != 1 && gy != 3) 	// can move right
	{
		*next_x = gx + 1;	// pre move right
		mark = 1;
	}
	else						// can not move left or right
		*next_x = gx;

	if (py < gy && gx != 1 && gx != 3) 		// can move up
	{
		if (mark == 0 || (direction == UP && py != 1 && py !=3) || (direction != UP && px != 1 && px != 3))
		{
			*next_x = gx;		// correct x axis movement
			*next_y = gy - 1;	// move up
		}
		else
			*next_y = gy;		// do not move up
	}
	else if (py > gy && gx != 1 && gx != 3) 	// can move down
	{
		if (mark == 0 || (direction == DOWN && py != 1 && py !=3) || (direction != DOWN && px != 1 && px != 3))
		{
			*next_x = gx;		// correct x axis movement
			*next_y = gy + 1;	// move down
		}
		else
			*next_y = gy;		// do not move down
	}
	else						// can not move up or down
		*next_y = gy;

    // Boundary Check for ghost1 start

    printf(" gx = %d \t gy = %d\n",gx, gy );

    if (gx == 4 && gy == 0 && direction == RIGHT)           // Upper-right corner
    {
        *next_x = gx;       // move - down
        *next_y = gy + 1;
    }

    if (gx == 4 && gy == 0 && direction == UP)
    {
        *next_x = gx - 1;       // move - left
        mark = -1;
    }


    if (gx == 0 && gy == 0 && direction == UP)      // Upper-left corner
    {

        *next_x = gx + 1;       // move - right
        mark = 1;
    }
    if (gx == 0 && gy == 0 && direction == LEFT)
    {
        *next_x = gx;       // move - down
        *next_y = gy + 1;
    }


    if (gx == 0 && gy == 4 && direction == DOWN)        // Lower-left corner
    {
        *next_x = gx + 1;       // move - left
        mark = -1;
    }
    if (gx == 0 && gy == 4 && direction == LEFT)
    {
        *next_x = gx;       // move - up
        *next_y = gy - 1;
    }


    if (gx = 4 && gy == 4 && direction == RIGHT)        // Lower-right corner
    {
        *next_x = gx;       // move - up
        *next_y = gy - 1;
    }
    if (gx = 4 && gy == 4 && direction == DOWN)
    {
        *next_x = gx - 1;       // move - right
        mark = 1;
    }
    // Boundary check end
}
/****************************************
 * attack algorithm
 * move towards the pacman position
 * using shortest path based approach
 ***************************************/
attack algorithm
move towards the pacman position
using shortest path based approach
attack algorithm move towards the pacman position using shortest path based approach
void
attack(int px, int py, int gx, int gy, int direction, double *next_x, double *next_y)
{
    int mark = 0;
	if (px < gx && gy != 1 && gy != 3) 		
	{
		*next_x = gx - 1;	
  		mark = -1;
	}
	else if (px > gx && gy != 1 && gy != 3) 	
	{
		*next_x = gx + 1;	
		mark = 1;
	}
	else						
		*next_x = gx;
	if (py < gy && gx != 1 && gx != 3) 		
	{
		if (mark == 0 || (direction == UP && py != 1 && py !=3) || (direction != UP && px != 1 && px != 3))
		{
			*next_x = gx;		
			*next_y = gy - 1;	
		}
		else
			*next_y = gy;		
	}
	else if (py > gy && gx != 1 && gx != 3) 	
	{
		if (mark == 0 || (direction == DOWN && py != 1 && py !=3) || (direction != DOWN && px != 1 && px != 3))
		{
			*next_x = gx;		
			*next_y = gy + 1;	
		}
		else
			*next_y = gy;		
	}
	else						
		*next_y = gy;
    printf(" gx = %d \t gy = %d\n",gx, gy );
    if (gx == 4 && gy == 0 && direction == RIGHT)           
    {
        *next_x = gx;       
        *next_y = gy + 1;
    }
    if (gx == 4 && gy == 0 && direction == UP)
    {
        *next_x = gx - 1;       
        mark = -1;
    }
    if (gx == 0 && gy == 0 && direction == UP)      
    {
        *next_x = gx + 1;       
        mark = 1;
    }
    if (gx == 0 && gy == 0 && direction == LEFT)
    {
        *next_x = gx;       
        *next_y = gy + 1;
    }
    if (gx == 0 && gy == 4 && direction == DOWN)        
    {
        *next_x = gx + 1;       
        mark = -1;
    }
    if (gx == 0 && gy == 4 && direction == LEFT)
    {
        *next_x = gx;       
        *next_y = gy - 1;
    }
    if (gx = 4 && gy == 4 && direction == RIGHT)        
    {
        *next_x = gx;       
        *next_y = gy - 1;
    }
    if (gx = 4 && gy == 4 && direction == DOWN)
    {
        *next_x = gx - 1;       
        mark = 1;
    }
}
void attack ( int px , int py , int gx , int gy , int direction , double * next_x , double * next_y ) { int mark = 0 ; if ( px < gx && gy != 1 && gy != 3 ) { * next_x = gx - 1 ; mark = -1 ; } else if ( px > gx && gy != 1 && gy != 3 ) { * next_x = gx + 1 ; mark = 1 ; } else * next_x = gx ; if ( py < gy && gx != 1 && gx != 3 ) { if ( mark == 0 || ( direction == UP && py != 1 && py != 3 ) || ( direction != UP && px != 1 && px != 3 ) ) { * next_x = gx ; * next_y = gy - 1 ; } else * next_y = gy ; } else if ( py > gy && gx != 1 && gx != 3 ) { if ( mark == 0 || ( direction == DOWN && py != 1 && py != 3 ) || ( direction != DOWN && px != 1 && px != 3 ) ) { * next_x = gx ; * next_y = gy + 1 ; } else * next_y = gy ; } else * next_y = gy ; printf ( " \t \n " , gx , gy ) ; if ( gx == 4 && gy == 0 && direction == RIGHT ) { * next_x = gx ; * next_y = gy + 1 ; } if ( gx == 4 && gy == 0 && direction == UP ) { * next_x = gx - 1 ; mark = -1 ; } if ( gx == 0 && gy == 0 && direction == UP ) { * next_x = gx + 1 ; mark = 1 ; } if ( gx == 0 && gy == 0 && direction == LEFT ) { * next_x = gx ; * next_y = gy + 1 ; } if ( gx == 0 && gy == 4 && direction == DOWN ) { * next_x = gx + 1 ; mark = -1 ; } if ( gx == 0 && gy == 4 && direction == LEFT ) { * next_x = gx ; * next_y = gy - 1 ; } if ( gx = 4 && gy == 4 && direction == RIGHT ) { * next_x = gx ; * next_y = gy - 1 ; } if ( gx = 4 && gy == 4 && direction == DOWN ) { * next_x = gx - 1 ; mark = 1 ; } }
attack algorithm
 move towards the pacman position
 using shortest path based approach
attack algorithm move towards the pacman position using shortest path based approach
//printf("In Attack\n"); // can move left // pre move left // can move right // pre move right // can not move left or right // can move up // correct x axis movement // move up // do not move up // can move down // correct x axis movement // move down // do not move down // can not move up or down // Boundary Check for ghost1 start // Upper-right corner // move - down // move - left // Upper-left corner // move - right // move - down // Lower-left corner // move - left // move - up // Lower-right corner // move - up // move - right // Boundary check end