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

1

3 Answers 3

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);
}
Sign up to request clarification or add additional context in comments.

Comments

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);
}

2 Comments

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 You are right I was wrong, you will have to use for loop to copy array. I've amended answer with included loop.
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.

2 Comments

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 it would, you're not making a deep enough copy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.