0 Replies - 7305 Views - Last Post: 25 May 2012 - 03:07 PM Rate Topic: -----

#1 wildcatqw   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-May 12

Maze recursion PacMan solver.

Posted 25 May 2012 - 03:07 PM

Hello, Im trying to make PacMan in Gridworld and i have everything done except the ghost.

The ghost is supposed to use recursion to find pacman, However my recursion goes into an infinite loop.
here is the code
 public boolean traverse(int row, int col)
{
   boolean done = false;
   if( first == true) /* first is set to true at the start because the transverse maze solver starts from the ghost's location*/
   {
     row = getLocation().getRow();
     col = getLocation().getCol();
     first = false;
   }

   if (getGrid().isValid(new Location(row,col)) && !(getGrid().get(new Location(row,col)) instanceof Board))
   {
      tried[row][col] = 3;
      
      if (row == player.getLocation().getRow() && col == player.getLocation().getCol())
         done = true; //the maze is solved
      else
      {
        
         done = traverse(row+1, col); //down
         if (!done)
            done = traverse(row, col+1); //right
         if (!done)
            done = traverse(row-1, col); //up
         if (!done)
            done = traverse(row, col-1); //left
      }
      if (done) //this location is part of the final path
         tried[row][col] = 2;
   }
   return done;
  }








after the transverse method finishes. it adds all the locations row and col that are equal to 3 in my 2d int array. 3 = path in that array. then the ghost chooses the location in the array list of paths with the shortest distance from him , so he can travel the path in correct order.

^ that is my plan.. and i have coded it out but by transverse method, that will find the route to PacMan and create a path does not work?

Is This A Good Question/Topic? 0
  • +

Page 1 of 1