Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8969051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:32:22+00:00 2026-06-15T17:32:22+00:00

I’m making a game tree for a tic tac toe. I have a method

  • 0

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 9×9, 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:32:22+00:00Added an answer on June 15, 2026 at 5:32 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array which has BIG numbers and small numbers in it. I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a text area in my form which accepts all possible characters from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.