Results 1 to 10 of 10
  1. #1
    malstryx is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default java.lang.StackOverflowError

    Hi everyone! I have a problem with this code. I'm trying make a puzzle game 3x3
    with "AI". What can i change?

    thank you! :)

    Java Code:
    public class Node {
    		
    		estadosVisitados ev = new estadosVisitados();
    	    int tabuleiro[][];
    	    Node pai;
    	    Node filho[];     
    	    int ehMeta[][] = {{1, 2, 3},
                              {4, 5, 6},
                              {8, 9, 0}};    
    	    int nivel, linha, coluna, grau, mover = 1, tamanho = 3;
    	  
    	    public Node(Node pai, int tabuleiro[][], int nivel) {
    	        this.tabuleiro = tabuleiro;
    	        this.pai = pai;
    	        filho = new Node[4];
    	        this.nivel = nivel;
    	        grau = 0;
    	        ev.estados.add(tabuleiro); 
    	        resolver();
    	    }	    
    	    
    	    public void setFilho(int tabuleiro[][]) {
    	        filho[grau] = new Node(this, tabuleiro, nivel + 1);
    	        grau++;	        
    	    } 
    	    
    	    public void acharPosicaoZero(int tabuleiro[][]) {
    	    	
    	    	boolean encontrado = false;	
    	    	while (encontrado == false) {
    	    		for(int l = 0; l < tabuleiro.length; l++) {
    	    			for(int c = 0; c < tabuleiro.length; c++) {
    	    			
    	    			if(tabuleiro[l][c] == 0) {
    	    				encontrado = true;
    	    				
    	    				linha = l;
    	    				coluna = c;	
    	    				}
    	    		 	}
    	    		}	
    	    	}    	
    	    }
    	    
    	    public void mover(int l, int c) {
    	    	
    	    	int[][] tabuleiroClonado = new int[tamanho][tamanho];	
    	    		try{
    	    			tabuleiroClonado = clonarTabuleiro(tabuleiro);
    	    			tabuleiroClonado[l][c] = tabuleiroClonado[l][c - mover];
    	    			tabuleiroClonado[l][c - mover] = 0;			
    	    			
    	    			if(ev.estados.contains(tabuleiroClonado)) {
    	    				tabuleiroClonado[l][c] = tabuleiroClonado[l][c + mover];
    	        			tabuleiroClonado[l][c + mover] = 0;
    	    			}
    	    			else {
    	    				 this.setFilho(tabuleiroClonado);
    	    				 System.out.println(toString(tabuleiroClonado));	
    	    			}
    	    		}
    	    		catch(Exception e){}
    	    		
    	    		try{
    	    			tabuleiroClonado = clonarTabuleiro(tabuleiro);
    	    			tabuleiroClonado[l][c] = tabuleiroClonado[l][c + mover];
    					tabuleiroClonado[l][c + mover] = 0;
    					
    					if(ev.estados.contains(tabuleiroClonado)) {
    						tabuleiroClonado[l][c] = tabuleiroClonado[l][c - mover];
    						tabuleiroClonado[l][c - mover] = 0;
    					}
    					else{
    						this.setFilho(tabuleiroClonado);
    						System.out.println(toString(tabuleiroClonado));	 
    					}
    				}
    	    		
    	    		catch(Exception e){}
    	    		try{
    	    			tabuleiroClonado = clonarTabuleiro(tabuleiro);
    					tabuleiroClonado[l][c] = tabuleiroClonado[l - mover][c];
    					tabuleiroClonado[l - mover][c] = 0;
    					
    					if(ev.estados.contains(tabuleiroClonado)) {
    						tabuleiroClonado[l][c] = tabuleiroClonado[l + mover][c];
    						tabuleiroClonado[l + mover][c] = 0;					
    					}
    					else {
    						this.setFilho(tabuleiroClonado);
    						System.out.println(toString(tabuleiroClonado));	   
    					}
    			}
    	    		catch(Exception e){}
    	    		try{
    	    			tabuleiroClonado = clonarTabuleiro(tabuleiro);
    					tabuleiroClonado[l][c] = tabuleiroClonado[l + mover][c];
    					tabuleiroClonado[l + mover][c] = 0;
    					
    					if(ev.estados.contains(tabuleiroClonado)) {
    						tabuleiroClonado[l][c] = tabuleiroClonado[l - mover][c];
    						tabuleiroClonado[l - mover][c] = 0;
    					}
    					else {
    						this.setFilho(tabuleiroClonado);
    						System.out.println(toString(tabuleiroClonado));	     				    			
    					}
    			}
    	    		catch(Exception e){}
    	    	}
    	    private int[][] clonarTabuleiro(int[][] tabuleiro) {
    	        int[][] novo = new int[tabuleiro.length][tabuleiro[0].length];
    	        for (int i = 0; i < tabuleiro.length; i++) {
    	            for (int j = 0; j < tabuleiro[0].length; j++) {
    	                novo[i][j] = tabuleiro[i][j];
    	            }
    	        }
    	        return novo;
    	    }
    	    public String toString(int[][] tabuleiro) {
    	        String codigo = "";
    	        for (int i = 0; i < tabuleiro.length; i++) {
    	            for (int j = 0; j < tabuleiro[i].length; j++) {
    	                codigo += tabuleiro[i][j];
    	            }
    	        }
    	        return codigo;
    	    }
    	    
    	    public void resolver() {
    	    	if(ehMeta(this.tabuleiro) == false) {
    	    		acharPosicaoZero(this.tabuleiro);
    	    		mover(this.linha,this.coluna);
    	    	}
    	    		else {
    	    			System.out.println("Solução!");
    	    	}
    	    }
    	    
    	    private boolean ehMeta(int [][]tablero){	
    	    	
     		   if(  tablero[0][0] == ehMeta[0][0] &&
     				tablero[1][0] == ehMeta[1][0] &&
     				tablero[2][0] == ehMeta[2][0] &&
     				tablero[0][1] == ehMeta[0][1] &&
     				tablero[0][2] == ehMeta[0][2] &&
     				tablero[1][1] == ehMeta[1][1] &&
     				tablero[2][2] == ehMeta[2][2] &&
     				tablero[1][2] == ehMeta[1][2] &&
     				tablero[2][1] == ehMeta[2][1]) {
     		return true;
     }
     		   else {
     			   return false;
     		 }
    	}
    }
    Java Code:
    import java.util.*;
    public class estadosVisitados {
    	ArrayList estados = new ArrayList();
    
    }
    Java Code:
    import java.util.ArrayList;
    
    public class Jogo {
    	private int tabuleiro[][];
    	private final int tamanho = 3; 
    
        
    	public Jogo() {
    		tabuleiro = new int[tamanho][tamanho];		
    	}
    	
    	public static void main(String[] args) {
    		Jogo jogo = new Jogo();
    			jogo.inicializarTabuleiro();
    			
    		Node node = new Node(null,jogo.getTabuleiro(),0);
    	}	
    	public int[][] getTabuleiro() {
    		return tabuleiro;
    	}
    	public void inicializarTabuleiro() {
    	   	 
        	ArrayList lista = new ArrayList();
                for (int i = 0; i <= 8; i++) {
                    lista.add(new Integer(i));
                }
     
                for (int i = 0; i < tabuleiro.length; i++) {
                    for (int j = 0; j < tabuleiro[i].length; j++) {
                        int indice = (int) (Math.random() * (lista.size()));
                        tabuleiro[i][j] = ((Integer) lista.get(indice)).intValue();
                        lista.remove(indice);
                }
            } 
        }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You should explain what's your question is, clearly. I don't think anyone wants to test your code and fix it, because it's too long.

  3. #3
    malstryx is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    You should explain what's your question is, clearly. I don't think anyone wants to test your code and fix it, because it's too long.
    Sorry, i'm from Brazil, and i don't speak english very well, but i'll try explain.

    In my puzzle game, when find a new state, it create a new object with the new parameters, and this hapen infinity times. This process cause the error "java.lang.StackOverflowError".

    I'm call the method resolve in the constructor of the class Node:

    Java Code:
    	    public Node(Node pai, int tabuleiro[][], int nivel) {
    	        this.tabuleiro = tabuleiro;
    	        this.pai = pai;
    	        filho = new Node[4];
    	        this.nivel = nivel;
    	        grau = 0;
    	        ev.estados.add(tabuleiro); 
    	        resolver();
    	    }
    Resolve(), call the method mover (Move):

    When mover(); find a new state it's use the method setFilho (setChild)

    Java Code:
     public void setFilho(int tabuleiro[][]) {
    	        filho[grau] = [B][COLOR="Red"]new Node(this, tabuleiro, nivel + 1);[/COLOR][/B]
    	        grau++;	        
    	    }
    And, it's cause a infinity looping so come the error, can you understand my problem?
    What can i do?

    thank you! please help me :o

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    You need to redesign the code to not have the recursive calls.

    Can you make a VERY small program and post it that shows how your code is making the recursive calls?

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    And also if you try to avoid infinite loop. As Norm says write a simple example, and then try to handle the infinite loop. There are lot of ways to do the same thing.

  6. #6
    malstryx is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    You need to redesign the code to not have the recursive calls.

    Can you make a VERY small program and post it that shows how your code is making the recursive calls?
    Hi! This is a small program example for what i'm trying. I know that cause a infinity looping, but i don't achieve to think another solution for my code (up^)

    This is a simple camparison to you understand.

    Java Code:
    public class Test {
    	String x;
    	
    	Test(String x) {
    		x = "Hello";
    		xMethod(x);
    	}
    	public void setX(String x) {
    		this.x = x;
    		new Test(this.x);
    	}
    	public void xMethod(String x) {
    		setX(x);
    	}
    		public static void main(String args[]) {
    			String x = "";
    			new Test(x);	
    		}
    }
    Last edited by malstryx; 09-30-2008 at 04:49 AM.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    Now you need to document WHY you want to do that. It is easy to write such code, but WHY do you need to have a recursive code?
    You create a new instance of Test. The constructor for Test calls xMethod which calls setX which then creates a new instance of Test.
    What is the program requirement for this logic?

  8. #8
    malstryx is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Now you need to document WHY you want to do that. It is easy to write such code, but WHY do you need to have a recursive code?
    You create a new instance of Test. The constructor for Test calls xMethod which calls setX which then creates a new instance of Test.
    What is the program requirement for this logic?

    This was a simple example, i makeing a game puzzle, and i need to do that because:

    i have this begin state

    This is the "X"
    450
    216
    978

    now, when i called the suposted method xMethod, it's create a new state in my true program, in this case two new states:
    405
    216
    978

    OR

    456
    210
    978

    and, for two new state, i called setX, and create a new object each state, and search a new state for the new state(in my true program), and make the looping, until one of the creates objects find the solucion:

    123
    456
    780
    Last edited by malstryx; 09-30-2008 at 04:46 PM.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    Set a limit to the depth of search with a static variable and stop going deeper by using the variable to keep track to the depth.
    Add one going down, subtract one on coming up.
    Design the search to keep a minimum number of nodes around.
    As come up from the path, trim off searched paths to allow the gc to collect used memory.

  10. #10
    malstryx is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Set a limit to the depth of search with a static variable and stop going deeper by using the variable to keep track to the depth.
    Add one going down, subtract one on coming up.
    Design the search to keep a minimum number of nodes around.
    As come up from the path, trim off searched paths to allow the gc to collect used memory.
    Now it's right, i was set a limit and worked! Thank you Norman, you help me open my mind ;)

Similar Threads

  1. java.lang.StackOverflowError Exception
    By Marcus in forum Web Frameworks
    Replies: 4
    Last Post: 08-24-2012, 10:02 PM
  2. Replies: 2
    Last Post: 09-18-2008, 07:30 AM
  3. java.lang.StackoverflowError
    By ravisankarvivek in forum New To Java
    Replies: 6
    Last Post: 06-23-2008, 09:05 AM
  4. java.lang.StackOverFlowError exception
    By jayaj in forum NetBeans
    Replies: 1
    Last Post: 06-08-2008, 11:17 AM
  5. java.lang.StackOverflowError
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-24-2007, 09:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •