Results 1 to 10 of 10
Thread: java.lang.StackOverflowError
- 09-29-2008, 03:18 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 5
- Rep Power
- 0
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); } } } }
- 09-29-2008, 03:57 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 09-29-2008, 08:30 PM #3
Member
- Join Date
- Sep 2008
- Posts
- 5
- Rep Power
- 0
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:
Resolve(), call the method mover (Move):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(); }
When mover(); find a new state it's use the method setFilho (setChild)
And, it's cause a infinity looping so come the error, can you understand my problem?Java Code:public void setFilho(int tabuleiro[][]) { filho[grau] = [B][COLOR="Red"]new Node(this, tabuleiro, nivel + 1);[/COLOR][/B] grau++; }
What can i do?
thank you! please help me :o
- 09-29-2008, 10:44 PM #4
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?
- 09-30-2008, 03:56 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 09-30-2008, 04:44 AM #6
Member
- Join Date
- Sep 2008
- Posts
- 5
- Rep Power
- 0
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.
- 09-30-2008, 02:27 PM #7
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?
- 09-30-2008, 04:43 PM #8
Member
- Join Date
- Sep 2008
- Posts
- 5
- Rep Power
- 0
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
780Last edited by malstryx; 09-30-2008 at 04:46 PM.
- 09-30-2008, 05:17 PM #9
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-01-2008, 04:14 AM #10
Member
- Join Date
- Sep 2008
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
java.lang.StackOverflowError Exception
By Marcus in forum Web FrameworksReplies: 4Last Post: 08-24-2012, 10:02 PM -
Error: cannot resolve symbol' on Person (java.lang.String, java.lang.String)
By baltimore in forum New To JavaReplies: 2Last Post: 09-18-2008, 07:30 AM -
java.lang.StackoverflowError
By ravisankarvivek in forum New To JavaReplies: 6Last Post: 06-23-2008, 09:05 AM -
java.lang.StackOverFlowError exception
By jayaj in forum NetBeansReplies: 1Last Post: 06-08-2008, 11:17 AM -
java.lang.StackOverflowError
By eva in forum New To JavaReplies: 3Last Post: 12-24-2007, 09:54 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks