Reputation: 1889
I'm making a game tree for a tic tac toe.
I have a method called buildGameTree
which gets a TreeNode
(the treeNode has an array of 80 childrens) and it calculates every kind of possible move. Each move is a 1 children of course.
Here is the error I get:
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at Main.buildGameTree(Main.java:169)
at Main.buildGameTree(Main.java:218)
at Main.buildGameTree(Main.java:218)
...
at Main.buildGameTree(Main.java:218)
And here is my code:
private void buildGameTree(TreeNode t1)
{
String[][] ar1 = (String[][]) t1.getData(); //ar1 is a game board
if(!gameOver(t1))
{
//printTree(t1);
int[][]ar2 = new int[81][2];
int line = 0;
for(int k=0;k<SIZE;k++) //looking for ""
for(int j=0;j<SIZE;j++,line++)
{
if(ar1[k][j].equals(""))
{
ar2[line][0] = k;
ar2[line][1] = j;
}
else
{
ar2[line][0] = -1;
ar2[line][1] = -1;
}
}
String[][][]ar3 = new String[80][9][9]; // array of game boards
for(int k=0;k<ar3.length;k++)// filling the array.. ar1 is a game board
{
ar3[k] = ar1;
}
for(int k=0;k<ar3.length;k++)// making a move
{
int i1 = ar2[k][0];
int i2 = ar2[k][1];
if(!(i1 == -1 || i2 == -1))
if(num%2==0)
ar3[k][i1][i2] = "X";
else
ar3[k][i1][i2] = "O";
}
TreeNode<String[][]>[] ar4 = new TreeNode[80];
for(int k=0;k<ar3.length;k++)
{
ar4[k] = new TreeNode<String[][]>(ar3[k]);
}
t1.setChildren(ar4);
for(int k=0;k<ar4.length;k++)
{
buildGameTree(ar4[k]);
}
}
}
Sorry for putting so much code lines but it's the only way to show my problem.
Line 169 is:
if(!gameOver(t1))
Line 218 is:
buildGameTree(ar4[k]);
Maybe my tree is to big to be saved in the memory?
btw the game board is an array of 9x9, empty block is "", and you have "X" and "O" of course. ar2 is kind of a table of indexes which will be the next moves in the game.
EDIT
public boolean gameOver(TreeNode t1)
{
String[][] ar1 = (String[][]) t1.getData();
for(int k=0;k<ar1.length;k++)
{
for(int j=0;j<ar1.length;j++)
if(ar1[k][j].equals(""))
return false;
}
return true;
}
EDIT I added some print lines n stuff to find what is causing the error and I found out that the first board is fine and then something wierd happens: In the print function I change the "" to "^" so we could see the board
^^^^^^^^^
^^^^^^^^^
^^^^^^^^^
^^^^^^^^^
^^^^^^^^^
^^^^^^^^^
^^^^^^^^X
^^^^^^^^^
^^^^^^^^^
OOOOOOOOO
OOOOOOOOO
OOOOOOOOO
OOOOOOOOO
OOOOOOOOO
OOOOOOOOO
OOOOOOOOX
OOOOOOOOO
OOOOOOOO^
OOOOOOOOO
OOOOOOOOO
OOOOOOOOO
OOOOOOOOO
OOOOOOOOO
OOOOOOOOO
OOOOOOOOX
OOOOOOOOO
OOOOOOOO^
As you can see it's making many moves instead of 1 since almost the whole board is covered by "O" and then it's stays the same that's the reason I get the overflow exception. What is wrong with my code? That's must to be over here:
for(int k=0;k<ar3.length;k++)// making a move
{
int i1 = ar2[k][0];
int i2 = ar2[k][1];
if(!(i1 == -1 || i2 == -1))
if(num%2==0)
ar3[k][i1][i2] = "X";
else
ar3[k][i1][i2] = "O";
}
As I said ar3 is an array of game boards or game options.. for each ar3[k] I make a diffrent move only if it's not equals to -1 the block content (means there is something into it X or O).
EDIT Since I got an answer why it's overflowing I will close this question and open another one regarding to my new problem thanks.
Upvotes: 1
Views: 236
Reputation: 30875
THe problem you have is that your coude is a infinite loop.
The argument you passs to internal call of buildGameTree(TreeNode)
(line 218) do not return false from gameOver(TreeNode)
. There fore your code in each step create the tree.
Upvotes: 1