title: Implementing A Star algorithm on 2d array
body: <p>I am required to code a snake game with AI for a project. I am having trouble coding a shortest path algorithm implemented on a 50x50 2d array. I have written code for the AStar pathfinding algorithm (see code below) but it doesn't seem to work. Can someone please help me correct my code, and also can someone help me to code the Dijktra's algorithm as I am struggling to code it for a 2d array. It is worthwhile mentioning that the shortest path algorithm is for my snake to find the shortest path to get to an apple on the 2d board. Hope someone can help.
  Just to make my question more clear: my problem is that I need to find the shortest path between two points on a 2d  array, as I am a beginner in coding I need help  to code an algorithm to find the shortest path between the initial point and end point, such as Dijktra's or AStar. </p>
  
  <pre><code>        //implementing a*
  public int manhattenDistance(Point current, Point goal){
      return Math.abs(current.getX()-goal.getX())+Math.abs(current.getY()-goal.getY());
  }
  public ArrayList&lt;Point&gt; aStar(Point myHead, Point apple){
      ArrayList&lt;Point&gt; closedSer=new ArrayList&lt;&gt;();
      ArrayList&lt;Point&gt; openSet=new ArrayList&lt;&gt;();
      openSet.add(myHead);
      ArrayList&lt;Point&gt; cameFrom=new ArrayList&lt;&gt;();
  
      int[][] gscore=new int[50][50];
      for(int i=0;i&lt;gscore.length;i++){
          for(int j=0;j&lt;gscore.length;j++)
              gscore[i][j]=Integer.MAX_VALUE;
      }
      gscore[myHead.getX()][myHead.getY()]=0;
  
      int[][] fscore=new int[50][50];
      for(int i=0;i&lt;fscore.length;i++){
          for(int j=0;j&lt;fscore.length;j++)
              fscore[i][j]=Integer.MAX_VALUE;
      }
      fscore[myHead.getX()][myHead.getY()]=manhattenDistance(myHead,apple);
  
      while(!openSet.isEmpty()){
          Point current; int[] fscores=new int[openSet.size()];
          for (int i=0;i&lt;openSet.size();i++){
              Point p=openSet.get(i);
              fscores[i]=manhattenDistance(p,apple);
          }int min=fscores[0], index=openSet.size();
          for(int i=0;i&lt;fscores.length-1;i++){
              if(fscores[i]&lt;fscores[i+1]) {
                  min = fscores[i];
                  index = i;
              }if(fscores[i+1]&lt;min){
                  min=fscores[i+1]; index=i+1;
              }
          }
          current=openSet.get(index-1);
          if(current==apple) return cameFrom;//.toArray(new Point[cameFrom.size()]);// reconstructpath(cameFrom,current);
          openSet.remove(index-1);
          closedSer.add(current);
  
          Point[] currentNeighbourstemp=current.getNeighbours();
          ArrayList&lt;Point&gt; currentNeighbours=new ArrayList&lt;&gt;();
          for(Point n:currentNeighbourstemp)
                  if(isOnBoard(n)) currentNeighbours.add(n);
          /*for(int i=0;i&lt;currentNeighbours.length;i++){
              for(int j=0; j&lt;openSet.size();j++)
                  if(currentNeighbours[i]==openSet.get(j)) continue;;
          }*/
  
          for (Point neighbour:currentNeighbours){
              Double tentative_gscore=gscore[neighbour.getX()][neighbour.getY()]+distanceBetween(neighbour,current);
              boolean in=false;
              for(int i=0;i&lt;openSet.size();i++){//checking if in oppenset
                  if(neighbour==openSet.get(i)) in=true;
              }
              if(!in) openSet.add(neighbour);
              else if(tentative_gscore&gt;=gscore[neighbour.getX()][neighbour.getY()]) continue;
              gscore[neighbour.getX()][neighbour.getY()]=tentative_gscore.intValue();
              fscore[neighbour.getX()][neighbour.getY()]=gscore[neighbour.getX()][neighbour.getY()]+manhattenDistance(neighbour,apple);
          }
      }
      return cameFrom;//.toArray(new Point[cameFrom.size()]);
  }
  
  public Double distanceBetween(Point a,Point b){
      return Math.sqrt((b.getX()-a.getX())*(b.getX()-a.getX())+(b.getY()-a.getY())*(b.getY()-a.getY()));
  }
  public static float invSqrt(float x) {
      float xhalf=0.5f*x;
      int i=Float.floatToIntBits(x);
      i=0x5f3759df-(i&gt;&gt;1);
      x=Float.intBitsToFloat(i);
      x=x*(1.5f-xhalf*x*x);
      return x;
  }
  public float gravityDistance(Point that,Point th){
      if(this.equals(that)) return Float.MAX_VALUE;
      return 20.0f*invSqrt(Math.abs(th.x-that.x)+Math.abs(th.y-that.y));
  }
  </code></pre>