473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

A* search (Pacman game)

Hi everyone

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
  1. import java.util.*;
  2.  
  3.     public  class ShortestPath
  4.    {
  5.       private static myComparator<Board> mycomp = new myComparator<Board>();
  6.       private static PriorityQueue<Board> open = new PriorityQueue<Board>(20,mycomp);
  7.       private static PriorityQueue<Board> closed = new PriorityQueue<Board>(20,mycomp);
  8.       private static int[][] mazePlan;
  9.       private static Cell startCell;
  10.  
  11.        //*** inner class represents the board state
  12.        public static class Board
  13.       {
  14.          private Cell currentCell = new Cell();
  15.          private Cell goalCell = new Cell();
  16.          private int movementCost;
  17.          private Board parent;
  18.  
  19.           public Board()
  20.          {
  21.             currentCell.setX(0);
  22.             currentCell.setY(0);
  23.  
  24.             goalCell.setX(0);
  25.             goalCell.setY(0);
  26.  
  27.             movementCost = 0;
  28.             parent = null;
  29.  
  30.          }
  31.           public Board(int px,int py,int gx,int gy)
  32.          {
  33.             currentCell.setX(px);
  34.             currentCell.setY(py);
  35.  
  36.             goalCell.setX(gx);
  37.             goalCell.setY(gy);
  38.  
  39.             movementCost = 0;
  40.             parent = null;
  41.          }
  42.           public Board(Cell pac, Cell ghost)
  43.          {
  44.             currentCell = pac;
  45.             goalCell = ghost;
  46.             movementCost = 0;
  47.             parent = null;
  48.  
  49.          }
  50.           public Cell getCurrentCell()
  51.          {
  52.             return this.currentCell;
  53.          }
  54.           public Cell getGoalCell()
  55.          {
  56.             return this.goalCell;
  57.          }
  58.           public Board getParent()
  59.          {
  60.             return this.parent;
  61.          }
  62.           public int getCost()
  63.          {    
  64.             return this.movementCost;
  65.          }
  66.  
  67.           public void setParent(Board p)
  68.          {
  69.             this.parent = p;
  70.          }
  71.           public void setCost(int n)
  72.          {
  73.             this.movementCost = n;
  74.          }
  75.  
  76.          //return true if the pacman's and ghost's cells
  77.           //are the same in both boards
  78.           public boolean isEqual(Board other)
  79.          {
  80.             if(this.currentCell.getX()== other.currentCell.getX())
  81.             {
  82.                if(this.currentCell.getY() == other.currentCell.getY())
  83.                {
  84.                   if(this.goalCell.getX()== other.goalCell.getX())
  85.                   {
  86.                      if(this.goalCell.getY()==other.goalCell.getY())
  87.                         return true;
  88.                   }
  89.                }
  90.             }
  91.             return false;
  92.          }
  93.  
  94.           //to compute f(n)= (alpha)*g(n)+ (beta)*h(n)
  95.           //the 2 and 10 in the following function represent the
  96.           // alpha and beta
  97.           public void computeCost()
  98.          {
  99.             int f,g,h;
  100.  
  101.             g = 2* (Math.abs(startCell.getX()-this.currentCell.getX())+
  102.                      Math.abs(startCell.getY()-this.currentCell.getY()));
  103.             h = 10*(Math.abs(this.currentCell.getX()-this.goalCell.getX())+
  104.                      Math.abs(this.currentCell.getY()-this.goalCell.getY()));
  105.             f = g+h;
  106.             this.movementCost = f;
  107.          }
  108.       }//end of class Board
  109.  
  110.       //*** this is used to create a toal order on Board objects
  111.        static class myComparator<E extends Board>
  112.                                         implements Comparator<E> 
  113.       {
  114.           public int compare(E a, E b)
  115.          {
  116.  
  117.                //*** to reverse order of priority use:
  118.                //    return a.getPriority() - b.getPriority();
  119.             return b.getCost() - a.getCost();
  120.          }
  121.  
  122.       } // myComparator
  123.  
  124.    //te search for the shortest path
  125.    //return an ArrayList of cells representing the path that Pacman
  126.    //will follow to catch the ghost
  127.        public static ArrayList<Cell> findShortestPath(int px, int py,int gx, int gy, int[][] mPlan)
  128.       {
  129.          startCell = new Cell(px,py);//the starting cell for pacman
  130.          Cell tempPac = new Cell(px,py);
  131.          Cell tempGh     = new Cell(gx,gy);
  132.          mazePlan = mPlan;
  133.  
  134.          ArrayList<Cell> pacPath = new ArrayList<Cell>();
  135.          //***temp going to be added to open list
  136.          Board tempOpenBoard = new Board(tempPac,tempGh);
  137.          //***temp going to be added to close list
  138.          Board tempCloseBoard = new Board();
  139.          Board board = new Board();
  140.          boolean foundGhost = false;
  141.          ArrayList<Board> path = new ArrayList<Board>();
  142.          ArrayList<Board> possibleMoves = new ArrayList<Board>();
  143.  
  144.           //compute the movement cost for the current board
  145.          tempOpenBoard.computeCost();
  146.          open.add(tempOpenBoard);
  147.  
  148.          while(!(foundGhost) && !(open.isEmpty()))
  149.          {
  150.              //polling the first board and transfer it to close
  151.             tempCloseBoard = open.poll();
  152.             closed.add(tempCloseBoard);
  153.  
  154.             if(tempCloseBoard.currentCell.isEqual(tempCloseBoard.goalCell))
  155.             {
  156.                foundGhost = true;
  157.                //finding the path by going back throw the parents
  158.                path.add(tempCloseBoard);
  159.                while((board = tempCloseBoard.getParent())!= null)
  160.                {
  161.                   path.add(board);
  162.                }
  163.             }
  164.             else//****THE ENDLESS LOOP HAPPENNING HERE****
  165.             {
  166.                  //initilize possibleMoves
  167.                possibleMoves = new ArrayList<Board>();
  168.                //at most four moves for each cell and each    possible
  169.                 //move will return a board
  170.                possibleMoves = createPossibleMoves(tempCloseBoard);
  171.  
  172.                for(int i=0;i< possibleMoves.size();i++)
  173.                {    //check if this board wasn't visited before
  174.                   if(!(closed.contains(possibleMoves.get(i))))
  175.                   {
  176.                      open.add(possibleMoves.get(i));
  177.                   }
  178.                }
  179.             }
  180.  
  181.          //initilize tempCloseBoard
  182.             tempCloseBoard = new Board();
  183.  
  184.          }
  185.  
  186.          if(path.size() == 0) 
  187.          for(int j=path.size()-1;j>=0; j--)
  188.             pacPath.add(path.get(j).getCurrentCell());
  189.          return pacPath;
  190.  
  191.  
  192.       } 
  193.       //create the possible new boards for the currentBoard
  194.        public static  ArrayList<Board> createPossibleMoves(Board currentBoard)
  195.       {
  196.          ArrayList<Board> tempList = new ArrayList<Board>();
  197.          Board tempUp = new Board();
  198.          Board tempDown = new Board(); 
  199.          Board tempLeft = new Board();
  200.          Board tempRight = new Board(); 
  201.          tempUp = new Board(currentBoard.getCurrentCell(),currentBoard.getGoalCell());
  202.          tempDown = new Board(currentBoard.getCurrentCell(),currentBoard.getGoalCell());
  203.          tempRight = new Board(currentBoard.getCurrentCell(),currentBoard.getGoalCell());
  204.          tempLeft = new Board(currentBoard.getCurrentCell(),currentBoard.getGoalCell());
  205.  
  206.          tempUp = up(tempUp);
  207.          if(tempUp != null)
  208.             tempList.add(tempUp);
  209.          tempDown = down(tempDown);
  210.          if(tempDown != null)
  211.             tempList.add(tempDown);
  212.          tempLeft = left(tempLeft);
  213.          if(tempLeft != null)
  214.             tempList.add(tempLeft);
  215.          tempRight = right(tempRight);
  216.          if(tempRight != null)
  217.             tempList.add(tempRight);
  218.  
  219.          return tempList;
  220.       }
  221.  
  222.        public static Board up(Board current)
  223.       {
  224.          Board newBoard = null;
  225.          int possibleX = current.currentCell.getX()-1;
  226.          int possibleY = current.currentCell.getY();
  227.          Cell newCell = new Cell(possibleX,possibleY);
  228.          Cell goalCell =  current.goalCell ;
  229.           //the value in mazePlane after moving the cell up
  230.          int mazeValue = mazePlan[possibleX][possibleY];
  231.  
  232.          switch(mazeValue)
  233.          {  
  234.             case 0: //power pellet
  235.                {newBoard = new Board(newCell,goalCell);System.out.println("pellet");
  236.                   newBoard.computeCost();
  237.                   newBoard.setCost(newBoard.movementCost +10);
  238.                   newBoard.setParent(current);
  239.                   break;
  240.                }
  241.             case 1: //wall
  242.                {System.out.println("UP wall");
  243.                   return null;
  244.                }
  245.             case 2://stripe
  246.                {newBoard = new Board(newCell,goalCell);
  247.                   newBoard.computeCost();
  248.                   newBoard.setCost(newBoard.movementCost +20);
  249.                   newBoard.setParent(current);
  250.                   break;
  251.                }
  252.             case 3://pirate
  253.                {newBoard = new Board(newCell,goalCell);
  254.                   newBoard.computeCost();
  255.                   newBoard.setCost(newBoard.movementCost +30);
  256.                   newBoard.setParent(current);
  257.                   break;
  258.                }
  259.          }//switch
  260.          System.out.print(newBoard.currentCell.getX());System.out.println(newBoard.currentCell.getY()+"U");
  261.          return newBoard;
  262.       }
  263. //***these functions the same as up() just the possibleX
  264. //**and possibleY are different
  265. public static Board down(Board current)
  266. public static Board left(Board current)
  267. public static Board right(Board current)
  268. }
sorry for the very long code but I couldn't get a scrol down code wraper
any idea is appreciated
Thanks in advance
Feb 28 '09 #1
0 2174

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: _V_ | last post by:
Well, I'm not really interested in PacMan. I'm curious about whether C# is good for java-type graphic-oriented 'applets' that will play in a browser (from a web page). If so, is this covered in...
1
by: mirage | last post by:
Hi, Actually, I use C# and WinForms to develop my littles applications. Now, I want develop a game like Tetris or Pacman, and I am asking who is the best API for me... Some of you have Game...
3
by: jcouse | last post by:
I am trying to find if a string exists in a file. If it doesn’t, it should return a “-1” and I’ll make my decision based on that Here is a sniplet of the text file game name mapp...
1
by: fowle040 | last post by:
I underlined and bold print my files. I need to know how to make this code into a working game. The object of the game is to have two players 1- belle and 2-beast. I want them to lose and gain...
33
by: Bertram Trabant | last post by:
Hello, Im working on a little LAN game in the style of old text-only MUD's and would need to find a way to search for a string in a text file (for example for usernames). I know it works in the way...
2
by: Chris | last post by:
I know this probably seems trivial, but I can't seem to find the bug in my alphabeta search implementation. I'm testing it with the game of tic-tac-toe. If the first player plays in a corner,...
1
by: winchester1897 | last post by:
In Flash 8 right now I have a pacman that I can control with the arrow keys. But I need help ristricting the movement of the pacman. I do have a graphic of the board that I want to use but I do not...
0
by: ALIH89 | last post by:
Can someone please help me develop Pacman game using VB6? If anyone knows how to develop Pacman game using VB6 please get in touch. Phone # removed] Or Add Me On Msn: E-mail address removed] ...
1
by: nicom53 | last post by:
i am writing a pacman game for a school project and i am having some problems with getting the logic so that pacman will not walk through the inner walls. I am also having a problem when the ghosts...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.