0

I have a class called Board that contains the following

public class Board  {

protected Piece[][] GameBoard = new Piece[8][8];
ArrayList<Move> BlackBoardmoves = new ArrayList<Move>();
ArrayList <Move> WhiteBoardmoves = new ArrayList<Move>();

I want to create an entirely new object of Board that has 2 entirely seperate ArrayLists I've been reading about how to do this for days and I've tried various methods like implementing cloning or serializable. I've read that the clone interface is broken and that using serializable is going to be much slower so I decided to write my own copy method

void copy(Board c)
{


for(int i =0; i<8; i++)  
{
for(int j=0; j<8; j++)
{
    this.GameBoard[i][j] = c.GameBoard[i][j];
}
}

for(int i=0 ;i<c.BlackBoardmoves.size(); i++)
{
this.BlackBoardmoves.add(c.BlackBoardmoves.get(i));
}

for(int i=0 ;i<c.WhiteBoardmoves.size(); i++)
{
this.WhiteBoardmoves.add(c.WhiteBoardmoves.get(i));
}
}

What I'm currently doing when creating each new object is this

Board obj2 = new Board();
obj2.copy(obj1);

This is a very small part of my project so I've been stuck on it for days and really can't afford to spend more time stuck in this. Thank you a lot:)

  • [copying array](https://stackoverflow.com/questions/5785745/make-copy-of-array-java) and [copying ArrayList](https://stackoverflow.com/questions/6536094/java-arraylist-copy) – XtremeBaumer May 23 '17 at 11:54

3 Answers3

1

First of all I would suggest to make Move and Piece objects immutable. With this approach you'll just need to copy reference on these object without deep cloning.

private static <T> void copy2DArray(T[][] to, T[][] from) {
    for (int i = 0; i < to.length; i++)
        for (int j = 0; j < to[i].length; j++) {
            to[i][j] = from[i][j];
        }
}

void copy(Board c) {
    copy2DArray<Piece>(this.GameBoard, c.GameBoard);
    this.BlackBoardmoves = new ArrayList(c.BlackBoardmoves);
    this.WhiteBoardmoves = new ArrayList(c.WhiteBoardmoves);
}
udalmik
  • 7,838
  • 26
  • 40
0

What exactly are you even trying to do here? How deep do you want the copy to be? You're just copying the content of the old list into the new one, which is probably (for a proper deep copy) not what you want. You're also doing it in a rather inefficient method, why not use the "addAll" method of the List class instead?

But you probably want to create copies of the list entries as well, and maybe deeper than that... Which is impossible to determine as you've not stated your requirements here.

jwenting
  • 5,505
  • 2
  • 25
  • 30
  • I'm trying to implement a minimax AI algorithm which is code recursive and requires me to send a Different board to each level. At the start my approach was so naive all I did was Board obj1 = obj2 . This caused all the levels in recursion to manipulate the original board. so now I'm trying to create a new Board in each level and copy its contents. All I want to do is make sure that adding an item to an arraylist in a board at one level will not affect in anyway the board at another leve. the current code is causing problems – Silverlight May 23 '17 at 12:05
  • @Silverlight it would, you're not making a deep enough copy. – jwenting May 24 '17 at 06:36
0

Inside of Board class you can put method that will return copied object but you will require proper constructor for it. You also have to add the same method inside of Piece class to deepcopy each object from array.

Board(Object[][] GameBoard, ArrayList<Object> BlackBoardObjects, ArrayList <Object> WhiteBoardObjects){
    this.GameBoard = GameBoard;
    this.BlackBoardObjects = BlackBoardObjects;
    this.WhiteBoardObjects = WhiteBoardObjects;
}

public Board getCopy(){
    for(int i = 0; i < GameBoard.length; i++){
        for(int j = 0; j < GameBoard[0].length; j++){
            GameBoardCopy[i][j] = GameBoard[i][j].getCopy();
        }
    }
    ArrayList<Move> BlackBoardObjectsCopy = new ArrayList<Move>(BlackBoardObjects);
    ArrayList <Move> WhiteBoardObjectsCopy = new ArrayList<Move>(WhiteBoardObjects);
    return new Board(GameBoard, BlackBoardObjectsCopy, WhiteBoardObjectsCopy);
}
Michael Dz
  • 3,655
  • 8
  • 40
  • 74
  • does System.arraycopy copy multidimensional arrays or I need to add a for loop to your code before System.arraycopy? This gives me a nullpointer exception for some reason when I call the recursive routine when it didn't other wise:/ – Silverlight May 23 '17 at 13:19
  • @Silverlight You are right I was wrong, you will have to use for loop to copy array. I've amended answer with included loop. – Michael Dz May 23 '17 at 14:27