Hey guys, I'm working on a program for a university course that uses a method called get_line() to recursively calculate a list of successive locations to get from one point on a grid to another. When I run it, I get a stack overflow at the line of the last return statement in the method. I was wondering if I could someone else to look over the method, and see if anything looks totally wrong. the method is provided below:
Thank you for the help!
location is an object containing a row r and a column c.
private Vector<location> get_line(location from, location to) {
location nextLoc = new location();
Vector<location> loc = new Vector<location>();
Random r = new Random();
if(to.r == from.r && to.c == from.c) {
return(loc);
} else {
if(to.r > from.r && to.c > from.c) {
nextLoc.r = from.r + 1;
nextLoc.c = from.c + 1;
} else if(to.r < from.r && to.c < from.c) {
nextLoc.r = from.r - 1;
nextLoc.c = from.c - 1;
} else if(to.r < from.r && to.c > from.c) {
nextLoc.r = from.r - 1;
nextLoc.c = from.c + 1;
} else if(to.r > from.r && to.c < from.c) {
nextLoc.r = from.r + 1;
nextLoc.c = from.c - 1;
} else if(to.r == from.r && to.c > from.c) {
if(r.nextInt(2) == 0) {
nextLoc.r = from.r + 1;
} else {
nextLoc.r = from.r - 1;
}
nextLoc.c = from.c + 1;
} else if(to.r == from.r && to.c < from.c) {
if(r.nextInt(2) == 0) {
nextLoc.r = from.r + 1;
} else {
nextLoc.r = from.r - 1;
}
nextLoc.c = from.c - 1;
} else if(to.r < from.r && to.c == from.c) {
nextLoc.r = from.r - 1;
if(r.nextInt(2) == 0) {
nextLoc.c = from.c + 1;
} else {
nextLoc.c = from.c - 1;
}
} else if(to.r > from.r && to.c == from.c) {
nextLoc.r = from.r + 1;
if(r.nextInt(2) == 0) {
nextLoc.c = from.c + 1;
} else {
nextLoc.c = from.c - 1;
}
}
loc.add(nextLoc);
return(get_line(nextLoc,to)); //stack overflow error occurs here.
}
}