i have been @ 2 hours , cannot life of me find out why method returning infinite loop. know implementation not best in terms of efficiency, suggestions :)
driver.java
import java.util.scanner; public class driver { static board b; public static void main(string[] args){ while(init()){ init(); } system.out.println("thanks testing out a* program"); } private static boolean init(){ system.out.println("would play? (\"y\",\"n\")"); scanner s = new scanner(system.in); string input = s.nextline(); if(!input.equals("y")){ return false; } b = new board(3,3); system.out.print(b.tostring()); while(getstartvalue() == false){ getstartvalue(); } while(getdestinationvalue() == false){ getdestinationvalue(); } system.out.print(b.tostring()); b.sethvalueallnodes(); b.createpath(b.getstartposition()); system.out.print(b.tostring()); return true; } private static boolean getstartvalue() { scanner s = new scanner(system.in); system.out.println("please enter starting x position"); int startx = s.nextint(); system.out.println("please enter starting y position"); int starty = s.nextint(); if(b.getboard()[startx][starty].getstatus() == 'x'){ system.out.println("please enter position not obstacle"); return false; } else{ b.getboard()[startx][starty].setstatus('s'); b.setstartposition(startx, starty); return true; } } private static boolean getdestinationvalue() { scanner s = new scanner(system.in); system.out.println("please enter ending x position"); int endx = s.nextint(); system.out.println("please enter ending y position"); int endy = s.nextint(); if(b.getboard()[endx][endy].getstatus() == 'x'){ system.out.println("please enter position not obstacle"); return false; } else if(b.getboard()[endx][endy].getstatus() == 's'){ system.out.println("please enter postition not starting value"); return false; } else{ b.getboard()[endx][endy].setstatus('e'); b.setendposition(endx, endy); return true; } } } node.java
public class node { private int x; private int y; //controls if transversable private char status; //f = g+h private int f; private int g; private int h; private node parent; node(char status, int x, int y){ this.status = status; this.x = x; this.y = y; this.g=0; this.h=0; this.f=0; } public int getx() { return x; } public void setx(int x) { this.x = x; } public int gety() { return y; } public void sety(int y) { this.y = y; } public char getstatus() { return status; } public void setstatus(char status) { this.status = status; } public int getf() { return f; } public void setf(int f) { this.f = f; } public int getg() { return g; } public void setg(int g) { this.g = g; } public int geth() { return h; } public void seth(int h) { this.h = h; } public node getparent() { return parent; } public void setparent(node parent) { this.parent = parent; } @override public string tostring() { if(this.getstatus() == 'o'){ return "o (" + this.getx() + ", " + this.gety() + ", " + this.geth() + ", " + this.getg() + ", " +this.getf() + ")"; } else if(this.getstatus() == 's'){ return "s (" + this.getx() + ", " + this.gety() + ")"; } else if(this.getstatus() == 'e'){ return "e (" + this.getx() + ", " + this.gety() + ")"; } else{ return "x (" + this.getx() + ", " + this.gety() + ")"; } } @override public boolean equals(object obj) { if (this == obj) return true; if( !(obj instanceof node)) return false; node nodeobj = (node) obj; return (nodeobj.getx() == this.getx() && nodeobj.gety() == this.gety()); } } board.java
import java.util.arraylist; import java.util.random; public class board { private int length; private int width; private node[][] board; private arraylist<node> openlist = new arraylist<>(); private arraylist<node> closedlist = new arraylist<>(); private node startposition; private node endposition; public node[][] getboard() { return board; } public void setboard(node[][] board) { this.board = board; } board(int length, int width){ board = new node[length][width]; random rand = new random(); for(int row=0; row < board.length; row++){ for(int col=0; col < board[row].length; col++){ int randomnum = rand.nextint(10); if(randomnum == 0){ board[row][col] = new node('x',row,col); } else{ board[row][col] = new node('o',row,col); } } } } public node getstartposition() { return this.startposition; } public void setstartposition(int x, int y) { board[x][y].setstatus('s'); board[x][y].setparent(null); startposition = new node('s', x, y); } public node getendposition() { return endposition; } public void setendposition(int x, int y) { board[x][y].setstatus('e'); endposition = new node('e', x, y); } public void sethvalueallnodes(){ for(int row=0; row < board.length; row++){ for(int col=0; col < board[row].length; col++){ int xdistance = math.abs(endposition.getx() - board[row][col].getx()); int ydistance = math.abs(endposition.gety() - board[row][col].gety()); board[row][col].seth(xdistance + ydistance); } } } public void createpath(node n){ node minimum; system.out.println("running node " + n.getx() + n.gety()); //1. add starting square open list if(n.getstatus() == 's'){ openlist.add(n); } //d1. stop when open list empty if(openlist.isempty() ){ return; } //d4. add target square closed list if(n.equals(endposition)){ return; } //2a. find lowest f score in our list int index = 0; int indexmin = 0; minimum = openlist.get(0); for(node node: openlist){ if(node.getf() < minimum.getf()){ minimum = node; indexmin = index; } index++; } //2b. switch minimum node closed list //remove node openlist openlist.remove(indexmin); //add close list closedlist.add(minimum); //c. each of 8 squares adjacent square addtoopenlist(minimum); createpath(minimum); system.out.println("our open list contains: "); for(node n1 : openlist){ system.out.print("x: " + n1.getx() + "y: " + n1.gety()); } system.out.println("our closed list contains: "); for(node n2 : closedlist){ system.out.print("x: " + n2.getx() + "y: " + n2.gety()); } system.out.println(); } private void addtoopenlist(node n) { boolean inclosedlist = false; boolean inopenlist = false; for(int i= -1; < 2; i++){ for(int j = -1; j < 2; j++){ inclosedlist = false; inopenlist = false; //c1. if not walkable or in closed list ignore int xpos = n.getx() + i; int ypos = n.gety() + j; if(xpos == -1 || ypos == -1){ system.out.println("our node out of bounds: " + "x: " + xpos + "y: " + ypos); } else{ for(node node : closedlist){ if(node.equals(board[xpos][ypos])){ inclosedlist = true; system.out.println("our node in closed list: " + "x: " + xpos + "y: " + ypos); } } if(inclosedlist == false){ //c2. if isnt on open list, add open list. make current square parent of square. record f, g, h of square system.out.println("our node notttttttt in closed list: " + "x: " + xpos + "y: " + ypos); int gcounter = (i + j == 1)? 10 : 14; int g = n.getg() + gcounter; int f = g + board[xpos][ypos].geth(); for(node node1 : openlist){ system.out.println("our node in loop: " + "x: " + xpos + "y: " + ypos); if(node1.equals(board[xpos][ypos])){ system.out.println("our node in open list: " + "x: " + xpos + "y: " + ypos); inopenlist = true; //c3.if in open list already, check see if path square better, using g cost measure if(g < board[xpos][ypos].getg()){ board[xpos][ypos].setg(g); board[xpos][ypos].setf(f); board[xpos][ypos].setparent(n); } } } if(inopenlist == false){ system.out.println("we here with" + "x: " + xpos + "y: " + ypos); board[xpos][ypos].setg(g); board[xpos][ypos].setf(f); board[xpos][ypos].setparent(n); openlist.add(board[xpos][ypos]); } } } } } } @override public string tostring() { string output = ""; for(int row=0; row < board.length; row++){ for(int col=0; col < board[row].length; col++){ output += board[row][col].tostring() + "\t"; } output += "\n"; } return output; } }
two mistakes @ first sight, don't know if suggestions semantically correct, won't deeper question.
have at: https://stackoverflow.com/help/how-to-ask
and try ask question again further assistance , reverted downvote.
mistakes:
wrong:
while(init()){ init(); } right:
while(init()); wrong:
while(getstartvalue() == false){ getstartvalue(); } while(getdestinationvalue() == false){ getdestinationvalue(); } right:
while(!getstartvalue()); while(!getdestinationvalue());
Comments
Post a Comment