java - Astar boundary checking -


so have astar program running , runs 4 outta 6 times, gives 2 different array out of bound errors @ times.

i think issue of boundary checking, think have boundary checks right. can point out issue is?

public class myastar extends astar {     public myastar(int r, int c) { super(r,c); }       static final int new =0;     static final int interior =1;     static final int frontier =2;     int[][] status = new int[rows][cols]; //initialize new matrix of 0s--keep track of status of node      public coord search(int sy, int sx, int ty, int tx)     {         coord start = new coord (sy, sx);         start.dist = 0;         start.cost=0;         coord current = null;          //create frontier heap         pheap<coord> frontier = new pheap<coord>(new coord[1000]);          //insert start frontier         frontier.insert(start);          //status         status [sy][sx] = frontier;          //boolean value of when stop while         boolean stop = false;          //cost         int []cost = {1, 0, 0, 5};          while(!stop && frontier.size() != 0)         {                         current = frontier.deletetop();             status[current.y][current.x] = interior;              int = current.y;             int j = current.x;              coord neighborn = new coord(i-1, j); //north             coord neighborw = new coord(i, j-1); //west             coord neighbore = new coord(i, j+1); //east             coord neighbors = new coord(i+1, j); //south              if (i>0 && i-1>0){             //north             neighborn.dist = current.dist + 1;             neighborn.cost = neighborn.dist + cost[m[i-1][j]] + ddist(i-1, j, ty, tx);             }              if (j>0 && j-1>0){              //west             neighborw.dist = current.dist + 1;             neighborw.cost = neighborw.dist + cost[m[i][j-1]] + ddist(i, j-1, ty, tx);             }              if (j<cols && j+1<cols){             //east             neighbore.dist = current.dist + 1;             neighbore.cost = neighbore.dist+ cost[m[i][j+1]] + ddist(i, j+1, ty, tx);             }              if (i<rows && i+1<rows ){             //south             neighbors.dist = current.dist + 1;             neighbors.cost = neighbors.dist+ cost[m[i+1][j]] + ddist(i+1, j, ty, tx);             }              boolean = true;             if(i<=0){a=false;}             boolean b = false;             boolean c = false;             boolean d = false;             if(neighborw.compareto(neighborn) > 0){b=true;a=false;}             if(j<=0){b=false;}             if(neighbore.compareto(neighborw) > 0){c=true;b=false;}             if(i>=cols){c=false;}             if(neighbors.compareto(neighbore) > 0){d=true;c=false;}             if(j>=rows){d=false;}               if(status[i-1][j] != frontier && status[i-1][j] != interior && i>0 && a)// m[i-1][j] == open && i>0 && a)             {                 frontier.insert(neighborn); //north                 neighborn.prev = current;                 status[i-1][j] = frontier;                 system.out.println("north");             }             if(status[i][j-1] != frontier && status[i][j-1] != interior && j>0 && b)// m[i][j-1] == open && j>0 && b)             {                 frontier.insert(neighborw); //west                 neighborw.prev = current;                 status[i][j-1] = frontier;                 system.out.println("west");             }             if(status[i][j+1] != frontier && status[i][j+1] != interior && j<cols && c)// m[i][j+1] == open && j<cols && c)             {                 frontier.insert(neighbore); //east                 neighbore.prev = current;                 status[i][j+1] = frontier;                 system.out.println("east");             }             if(status[i+1][j] != frontier && status[i+1][j] != interior && i<rows && d)// m[i+1][j] == open && i<rows && d)             {                 frontier.insert(neighbors); //south                 neighbors.prev = current;                 status[i+1][j] = frontier;                 system.out.println("south");             }             if(i==ty && j == tx){stop = true;}         }         return current;     } } 

if(status[i-1][j] != frontier && status[i-1][j] != interior && i>0 && a)

you should put i > 0 before i - 1 let short circuit prevent i - 1 being -1.

and also, i>0 && i-1>0 j > 1.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -