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 7838745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:11:41+00:00 2026-06-02T15:11:41+00:00

I have got (IMHO) a strange behaviour in my code. I am currently implementing

  • 0

I have got (IMHO) a strange behaviour in my code. I am currently implementing the minimax algorithm for a Tic Tac Toe game. In my “successor” method I want to determine all possible moves. Here’s the code:

private ArrayList<TicTacToeState[][]> successor(final TicTacToeState[][] field, TicTacToeState s) {
    ArrayList<TicTacToeState[][]> returnList = new ArrayList<TicTacToeState[][]>();
    for (int i = 0; i < TicTacToeGame.FIELDSIZE; i++) {
        for (int j = 0; j < TicTacToeGame.FIELDSIZE; j++) {
            if (field[i][j] == TicTacToeState.Empty) {
                TicTacToeState[][] currentCopy = new TicTacToeState[TicTacToeGame.FIELDSIZE][TicTacToeGame.FIELDSIZE];
                System.arraycopy(field, 0, currentCopy, 0, field.length);
                currentCopy[i][j] = s; // <- field seems to be referenced?!
                returnList.add(currentCopy);
            }
        }
    }
    return returnList;
}

As you can see, I want to get all possible moves and save them into an arraylist. Unfortunately, when setting the value in “currentCopy”, the “field” is also changed. But the field shouldn’t be refrenced, because I copied the array. Where is the mistake? I have already tried using the clone() method on the two dimensional array -> same problem.

Thank you for any help.

(FYI, TicTacToeState is an enumeration including “Player1”, “Player2” and “Empty”)

  • 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-02T15:11:43+00:00Added an answer on June 2, 2026 at 3:11 pm

    Java uses shallow copies. That is, you get a copy but it isn’t what you want here. You want a deep copy. Try manually copying each element into returnList and see what happens.

    Another way to solve this is to make your move, recurse, then unmake the move. Then you don’t need to copy the array at all.

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

Sidebar

Related Questions

I have got some problem implementing bullet physics into my opengl game. The thing
I have got this code: XDocument xdoc = XDocument.Load(URI); XElement root = xdoc.Element(forecast); //get
I have got a piece of code, that should countdown some number (in this
I have got some code to load an assembly and get all types, which
I have got the following code from here to read an Excel file using
have got a problem with do this kind of code , can't figure how
I have got the following code to show a dialog box when the image
I have got a strange and rare problem here. I am using NSTimer event
I have got an app I'm writing and I'm currently considering using MVC to
I have got the following IF condition code: if ((depth <= min_depth ) &&

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.