This program is using A* search and it supposed to find the shortest path for Pacman in the program as(px,py) or currentCell to catch the ghost in the program as(gx,gy)or goalCell.for computing the heuristic value it should use Manhattan distance method
and the formula f (n) = alpha * g(n)+beta * h(n)
1-my problem is that I cant get pacman to move more than 3 cells and after that it start's from the begining and get into an endless loop. the loop is in the function findShortestPath at line 164.in other words the ArrayList possibleMoves get filled with only three different cells over and over!
2-I don't know if my way in computing the cost is correct and if I supposed to change the alpha and beta values in the fuction computeCost() or not at line 97.
Expand|Select|Wrap|Line Numbers
- import java.util.*;
- public class ShortestPath
- {
- private static myComparator<Board> mycomp = new myComparator<Board>();
- private static PriorityQueue<Board> open = new PriorityQueue<Board>(20,mycomp);
- private static PriorityQueue<Board> closed = new PriorityQueue<Board>(20,mycomp);
- private static int[][] mazePlan;
- private static Cell startCell;
- //*** inner class represents the board state
- public static class Board
- {
- private Cell currentCell = new Cell();
- private Cell goalCell = new Cell();
- private int movementCost;
- private Board parent;
- public Board()
- {
- currentCell.setX(0);
- currentCell.setY(0);
- goalCell.setX(0);
- goalCell.setY(0);
- movementCost = 0;
- parent = null;
- }
- public Board(int px,int py,int gx,int gy)
- {
- currentCell.setX(px);
- currentCell.setY(py);
- goalCell.setX(gx);
- goalCell.setY(gy);
- movementCost = 0;
- parent = null;
- }
- public Board(Cell pac, Cell ghost)
- {
- currentCell = pac;
- goalCell = ghost;
- movementCost = 0;
- parent = null;
- }
- public Cell getCurrentCell()
- {
- return this.currentCell;
- }
- public Cell getGoalCell()
- {
- return this.goalCell;
- }
- public Board getParent()
- {
- return this.parent;
- }
- public int getCost()
- {
- return this.movementCost;
- }
- public void setParent(Board p)
- {
- this.parent = p;
- }
- public void setCost(int n)
- {
- this.movementCost = n;
- }
- //return true if the pacman's and ghost's cells
- //are the same in both boards
- public boolean isEqual(Board other)
- {
- if(this.currentCell.getX()== other.currentCell.getX())
- {
- if(this.currentCell.getY() == other.currentCell.getY())
- {
- if(this.goalCell.getX()== other.goalCell.getX())
- {
- if(this.goalCell.getY()==other.goalCell.getY())
- return true;
- }
- }
- }
- return false;
- }
- //to compute f(n)= (alpha)*g(n)+ (beta)*h(n)
- //the 2 and 10 in the following function represent the
- // alpha and beta
- public void computeCost()
- {
- int f,g,h;
- g = 2* (Math.abs(startCell.getX()-this.currentCell.getX())+
- Math.abs(startCell.getY()-this.currentCell.getY()));
- h = 10*(Math.abs(this.currentCell.getX()-this.goalCell.getX())+
- Math.abs(this.currentCell.getY()-this.goalCell.getY()));
- f = g+h;
- this.movementCost = f;
- }
- }//end of class Board
- //*** this is used to create a toal order on Board objects
- static class myComparator<E extends Board>
- implements Comparator<E>
- {
- public int compare(E a, E b)
- {
- //*** to reverse order of priority use:
- // return a.getPriority() - b.getPriority();
- return b.getCost() - a.getCost();
- }
- } // myComparator
- //te search for the shortest path
- //return an ArrayList of cells representing the path that Pacman
- //will follow to catch the ghost
- public static ArrayList<Cell> findShortestPath(int px, int py,int gx, int gy, int[][] mPlan)
- {
- startCell = new Cell(px,py);//the starting cell for pacman
- Cell tempPac = new Cell(px,py);
- Cell tempGh = new Cell(gx,gy);
- mazePlan = mPlan;
- ArrayList<Cell> pacPath = new ArrayList<Cell>();
- //***temp going to be added to open list
- Board tempOpenBoard = new Board(tempPac,tempGh);
- //***temp going to be added to close list
- Board tempCloseBoard = new Board();
- Board board = new Board();
- boolean foundGhost = false;
- ArrayList<Board> path = new ArrayList<Board>();
- ArrayList<Board> possibleMoves = new ArrayList<Board>();
- //compute the movement cost for the current board
- tempOpenBoard.computeCost();
- open.add(tempOpenBoard);
- while(!(foundGhost) && !(open.isEmpty()))
- {
- //polling the first board and transfer it to close
- tempCloseBoard = open.poll();
- closed.add(tempCloseBoard);
- if(tempCloseBoard.currentCell.isEqual(tempCloseBoard.goalCell))
- {
- foundGhost = true;
- //finding the path by going back throw the parents
- path.add(tempCloseBoard);
- while((board = tempCloseBoard.getParent())!= null)
- {
- path.add(board);
- }
- }
- else//****THE ENDLESS LOOP HAPPENNING HERE****
- {
- //initilize possibleMoves
- possibleMoves = new ArrayList<Board>();
- //at most four moves for each cell and each possible
- //move will return a board
- possibleMoves = createPossibleMoves(tempCloseBoard);
- for(int i=0;i< possibleMoves.size();i++)
- { //check if this board wasn't visited before
- if(!(closed.contains(possibleMoves.get(i))))
- {
- open.add(possibleMoves.get(i));
- }
- }
- }
- //initilize tempCloseBoard
- tempCloseBoard = new Board();
- }
- if(path.size() == 0)
- for(int j=path.size()-1;j>=0; j--)
- pacPath.add(path.get(j).getCurrentCell());
- return pacPath;
- }
- //create the possible new boards for the currentBoard
- public static ArrayList<Board> createPossibleMoves(Board currentBoard)
- {
- ArrayList<Board> tempList = new ArrayList<Board>();
- Board tempUp = new Board();
- Board tempDown = new Board();
- Board tempLeft = new Board();
- Board tempRight = new Board();
- tempUp = new Board(currentBoard.getCurrentCell(),currentBoard.getGoalCell());
- tempDown = new Board(currentBoard.getCurrentCell(),currentBoard.getGoalCell());
- tempRight = new Board(currentBoard.getCurrentCell(),currentBoard.getGoalCell());
- tempLeft = new Board(currentBoard.getCurrentCell(),currentBoard.getGoalCell());
- tempUp = up(tempUp);
- if(tempUp != null)
- tempList.add(tempUp);
- tempDown = down(tempDown);
- if(tempDown != null)
- tempList.add(tempDown);
- tempLeft = left(tempLeft);
- if(tempLeft != null)
- tempList.add(tempLeft);
- tempRight = right(tempRight);
- if(tempRight != null)
- tempList.add(tempRight);
- return tempList;
- }
- public static Board up(Board current)
- {
- Board newBoard = null;
- int possibleX = current.currentCell.getX()-1;
- int possibleY = current.currentCell.getY();
- Cell newCell = new Cell(possibleX,possibleY);
- Cell goalCell = current.goalCell ;
- //the value in mazePlane after moving the cell up
- int mazeValue = mazePlan[possibleX][possibleY];
- switch(mazeValue)
- {
- case 0: //power pellet
- {newBoard = new Board(newCell,goalCell);System.out.println("pellet");
- newBoard.computeCost();
- newBoard.setCost(newBoard.movementCost +10);
- newBoard.setParent(current);
- break;
- }
- case 1: //wall
- {System.out.println("UP wall");
- return null;
- }
- case 2://stripe
- {newBoard = new Board(newCell,goalCell);
- newBoard.computeCost();
- newBoard.setCost(newBoard.movementCost +20);
- newBoard.setParent(current);
- break;
- }
- case 3://pirate
- {newBoard = new Board(newCell,goalCell);
- newBoard.computeCost();
- newBoard.setCost(newBoard.movementCost +30);
- newBoard.setParent(current);
- break;
- }
- }//switch
- System.out.print(newBoard.currentCell.getX());System.out.println(newBoard.currentCell.getY()+"U");
- return newBoard;
- }
- //***these functions the same as up() just the possibleX
- //**and possibleY are different
- public static Board down(Board current)
- public static Board left(Board current)
- public static Board right(Board current)
- }
any idea is appreciated
Thanks in advance