Copy objects correctly

I am currently trying to make a program to play chess.

I have the following problem.
I want to duplicate my object from my board-class and check and evaluate the position.

However when I apply a method to a direct copy of the object it also applies it to the original object.

How can I fix that?

I hope this helps:

I already tried something simular and it didn’t work.

If it helps this is my code


    ArrayList<Integer>[] m=getmoves(white_move);
    float besteval=-200;
    int in=-1;
    ArrayList<Piece> backup[]=new ArrayList[2];
    backup[0]=black;
    backup[1]=white;
    for (int i=0; i<rek[0].size(); i++) {
      Board bn=(new Board(white,black,white_move)).moved(m[0].get(i), m[1].get(i), m[2].get(i), m[3].get(i));
      if (b.suchen((int)m[2].get(i), (int)m[3].get(i))!=null) 
     float ev=((white_move)?-1.0:1.0)*bn.evaluate_position();
      if (ev>besteval) {
        besteval=ev;
        in=i;
      }
    }
    int rand=int(random(0, m[0].size()));
    if (in<0) in=int(random(0, m[0].size()));
    white=backup[0];
    black=backup[1];
    move_request(m[0].get(in), m[1].get(in), m[2].get(in), m[3].get(in));
  }

thanks for posting the code (it really helps to do so in the first place) ! But it’s hard to follow without the context. Could you point to the code where you “tried something similar” ?

As Chrisir wrote, implementing a copy function like

class ClassName {
  ...
  ...
  ClassName copy () {
    ClassName newElement = new ClassName(); 

    newElement.x= x; 
    newElement.y= y; 
    newElement.col= col; 
    // copy all properties you need here

    return newElement;  
  }
}

would be a solution. Or, you can define a constructor so that passing the “original” instance as an argument of the constructor will copy all the member variables.

class ClassName {
  ...
  ...
  ClassName (ClassName original) {
    x = original.x;
    y = original.y;
    col = original.col;
    // copy all properties you need here
  }
}

Note that if you have a property that is an instance of a class (e.g., ArrayList) inside the class then you have to somehow copy that object as well. This is why “deep” copy cannot be generalized especially when you have a circular dependency.

This is the version I use right now:

  Board copy() {
    return new Board(white,black,white_moves);
  }

The approach I chose before is

  Board copy() {
    Board nb=new Board();
nb.white.clear();
nb.black.clear();
for(int i=0;i<white.size();i++) nb.white.add(white.get(i));
for(int i=0;i<black.size();i++) nb.black.add(black.get(i));
nb.white_moves=white_moves;
return nb;
  }

ok I think the problem could be that white and black are ArrayLists. If that is the case, as I noted above, the cloned Board shares the same white and black which can be an issue. You need to make new lists and copy each element from the list (and… if these elements seem to be instances of Piece, so you need to make copy method in this class as well).

I tried copying the ArrayLists by just adding the Pieces but as new objects and it worked!
Thanks for your help!