java - StackOverflowError during recursive calls to find closest empty index in 2D array -


i trying use recursive approach figure out closest empty element in 2d array if input index has element. example, call method tryplant(int x, int y) put symbol in 2d (32x32) array:

  1. ecoarray[x][y] == "." (empty) fill "~"
  2. if there element besides ".", recursively find next empty spot in same row first, column.

the code method is:

public void tryplant(int x, int y){     if (this.ecoarray[x][y] == ".") {         this.ecoarray[x][y] = "~";     }else{         if((y - 1) >= 0) {             tryplant(x, y - 1);         }         else if((y + 1) <= 32){             tryplant(x, y + 1);         }else if((x - 1) >= 0 ) {             tryplant(x - 1, y);         }else if((x + 1) <= 32){             tryplant(x + 1, y);         }     } } 

i calling method in class this:

plant p1;  private void initplants(){         int size = p1.initpop;         (int = 0; < size; i++) {             int randx = randgen();             int randy = randgen();             plant plant = new plant(randx, randy, this.ecoarray);         }     } 

the randgen() method returns random integer between 0-31.

sometimes random generator gives me indexes such not collide other objects. example of (the | obstacles):

planting trees

i want know why stackoverflow error , can fix it. if need other portions of code, please ask. pretty new java.

edit

public static int left = 0; public static int right = 0;     public void tryplant(int x, int y){         if (this.ecoarray[x][y] == ".") {             this.ecoarray[x][y] = "~";         }else{              if((y - 1) >= 0) {                 this.left++;                 tryplant(x, y - 1);             }             else if((y + this.left + 1) <= 32){                 tryplant(x, y + this.left + 1);                 this.left = 0;             }else if((x - 1) >= 0 ) {                 this.right++;                 tryplant(x - 1, y);             }else if((x + this.right + 1) <= 32){                 tryplant(x + + this.right + 1, y);                 this.right = 0;             }         }     } 

approximately happens is: each time function called stored in memory stack. when function return, stored cleared. but, if call many functions, fullfil memory , stackoverflow error sent.

you stackoverflow error because somewhere have loop in algorithm. cause many call tryplant().

  • first, when find way pass, return. (where char == '~')
  • second, there obstacle, return. (where char == '|')
  • third, don't try directions:

        if ((y - 1) >= 0) {         this.left++;         tryplant(x, y - 1);     } else if ((y + this.left + 1) <= 32){         tryplant(x, y + this.left + 1);         this.left = 0;     } else if ((x - 1) >= 0 ) {         this.right++;         tryplant(x - 1, y);     } else if ((x + this.right + 1) <= 32){         tryplant(x + + this.right + 1, y);         this.right = 0;     } 

i tried , seems work:

public class test {      private char[][] ecoarray;      public test() {         string[] stringarray = new string[] {         "||.|.||..|..||.|.||||||||..||.||",         "||||....||..|||..||....|.||..|||",         "||||||...|.|||...||.|..||..||||.",         "||...||.|.|||.||||.|||||.|...||.",         "|.|....|.|||||||||..||.|.|.||...",         "..|||.||||...|..||.||..|..||||.|",         "..|.||||||..||.||||..|||.|.|...|",         "|||||.||.|||...||...||..||.|||..",         "||||.|..||||||..|.|||...||.||.|.",         "|||.|||||.|||||.||||.|....||||||",         "||...||||||.|.|||||||||||.|.|.||",         "|.|.||||||||.||||....|.||||.||||",         "||..||.||||.|..||.|||..||.|.||||",         "..||..|..||.|.|||..|||..|||||.|.",         "||||.|.||.||||.|||||..|||.|.....",         "..|.|.|||..|||..||.||||.|||.|..|",         "||||.|..|||||||.|||||.||.|.|....",         "..|...||...|||||.|...|..|...|||.",         "..|||||||..||...||||||..|..|||||",         "||||..||.|.|||||.||||.|||||.||..",         "|||||.||||.|....||||....||.||...",         "||..||.|||||.||||||..||..|....||",         "|.||||.||..|...|.|..|||.|.|||.||",         "...||||.|..|||.|||..|.||...|.|||",         ".||||.|..|.|..||..||..||..||||||",         "|||.|||..|||..||||.||||.|.||.|||",         "|||||.||...|.|.|.||...|||..|.|||",         ".||||.|.|.|||...|||.|||.....||||",         "|||.|||.|.|...||.|....||||.|.|||",         ".||||||.|||||||...||..|||.||||.|",         "||||.|||||.|.||.||||..|.|||.||||",         "|.|.||||....|.||||||||.|||||.|.|"         };          this.ecoarray = new char[32][32];         int index = 0;         (string s : stringarray) {             this.ecoarray[index] = s.tochararray();              ++index;         }     }      public void tryplant(int x, int y){         if (this.ecoarray[x][y] == '~' || this.ecoarray[x][y] == '|') {             return;         }         if (this.ecoarray[x][y] == '.') {             this.ecoarray[x][y] = '~';              if ((y - 1) >= 0) {                 tryplant(x, y - 1);             }              if((y + 1) < 32){                 tryplant(x, y + 1);             }             if((x - 1) >= 0 ) {                 tryplant(x - 1, y);             }             if((x + 1) < 32){                 tryplant(x + 1, y);             }         }     }      private void displayinconsole() {         (char[] cl : this.ecoarray) {             system.out.println(cl);         }            }      public static void main(string[] args) {         (int x = 0; x < 32; x++) {             (int y = 0; y < 32; y++) {                 test t = new test();                 t.tryplant(x, y);                 system.out.println("after tryplant('" + x + "','" + y + "'): ");                 t.displayinconsole();             }         }     }  } 

is doing need ? don't understand want start , want stop...

the result big sorry...


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -