1

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.

5
  • "Sorry for putting so much code lines" That is not my definition of 'very much', but for better help sooner, post an SSCCE. MY best guess is that if(!gameOver(t1)) is not returning what you expect. Have you run this in a debugger and inspected the values & program flow? Commented Dec 10, 2012 at 14:00
  • Have you read stackoverflow.com/questions/214741/… to understand the error? Commented Dec 10, 2012 at 14:02
  • I know what it means but how can I fix that?How I can build my game tree maybe more efficient to avoid this error Commented Dec 10, 2012 at 14:03
  • I will add this function right now to the thread, refresh in a minute Commented Dec 10, 2012 at 14:06
  • 1
    "That is not my definition of 'very much'.." Unless of course, you are referring to the 719 lines of at Main.buildGameTree(Main.java:218) that I trimmed from the stack trace! Commented Dec 10, 2012 at 14:07

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

You mean it always returns false but when the board is full of "X" and "O" it will return true means game over. but I guess in the middle of that process the stack is overflowed
It always return false because you always analyses empty board.
How come?check the making a move note on the code .. those lines are making a single move and eventualy they will get a board with size-1 empty places and the board will be full. That's theoretically what should happend and I believe practically also if you found flaw in my code I would like to know
I am not a debuger. Please use that tool, and you will know who come.
Edited.. but still can't find the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.