Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. public static List<Position> findPath(GameState gameState, Position start, Position end) {
  2. // TODO: Remove these comments before making a pull request
  3. // internally doesnt matter how they get to their spot, visualizer needs to be given a path.
  4. // player function just changes the position. Create a new function to just make a path for visualizer.
  5. // new package similar to -> server -> communication -> visualizer called movement instead and do something
  6. // with the package that communicates a list of positions that will give to visualizer.
  7.  
  8. // TODO: Implement A* path finding. Make sure that if start==end, this function returns an EMPTY list.
  9. // return new ArrayList<Position>();
  10.  
  11.  
  12.  
  13. // Case if start and end do not reference positions on the same board
  14. // Case if start == end
  15. // Return empty list
  16. if (start.getBoardID() != end.getBoardID() || start == end) {
  17. return new ArrayList<Position>();
  18. }
  19.  
  20. // Otherwise use A* path finding to find a path
  21. // This will break is getBoardID returns an ID that does not exist cause itll be null
  22. Tile[][] grid = gameState.getBoard(start.getBoardID()).getGrid();
  23.  
  24.  
  25. // current will change depending on the f value of the cells around it, it only begins at 'start'
  26. Position current = start;
  27.  
  28. // 2 Lists, closed list and open list
  29. // Closed list is all the cells that have been visited, maybe a boolean 2d array that will keep track of visited cells
  30. // Open list which keeps track of f values for certain cells, this will likely be the list that will be returned at the end maybe?
  31. // boolean[][] closedList = grid;
  32. // ArrayList openList = new ArrayList<Position>();
  33.  
  34. // DO this 4 times, to check cell above, below, left, and right of current cell (which would be Position point)
  35. if (isValid(gameState, current)) {
  36.  
  37. // If at destination
  38. if (isDestination(current, end)) {
  39. // Add this cell to list then return list
  40. }
  41.  
  42. // Check to see if it is on the closed list already and whether it is blocked, if neither then continue otherwise ignore this cell
  43. else if (isImpassible(gameState, current) == false) {
  44. // do some wacky stuff here
  45. // check this cells new f g and h values, compare to the other 3 and find the best one?
  46. }
  47. }
  48. }
  49.  
  50. // helper function to check whether given cell is valid
  51. public static boolean isValid(GameState gameState, Position point) {
  52. return (point.getX() >= 0) && (point.getX() < gameState.getBoard(point.getBoardID()).getGrid().length) &&
  53. (point.getY() > 0) && (point.getY() < gameState.getBoard(point.getBoardID()).getGrid()[0].length);
  54. }
  55.  
  56. // helper function to check whether the given cell is impassible or not
  57. public static boolean isImpassible(GameState gameState, Position point) {
  58. return gameState.getBoard(point.getBoardID()).getGrid()[point.getX()][point.getY()].getType() == Tile.TileType.IMPASSIBLE;
  59.  
  60. }
  61.  
  62. // helper function to check whether the destination has been reached
  63. public static boolean isDestination(Position point, Position end) {
  64. return (point == end);
  65. }
  66.  
  67. // Assume 3 values per node, 'f' , 'g', 'h'. f = g + h where g is 1 (distance between tiles) and h is total distance from that point to end tile
  68. // h is the heuristic
  69. public static double calculateH(GameState gameState, Position point, Position end) {
  70. return ((double) Math.sqrt(((point.getX() - end.getX()) * (point.getX() - end.getX())) + ((point.getY() - end.getY()) * (point.getY() - end.getY()))));
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement