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.
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).