If I have a chess board 8x8 And in my copy constructor I want to deep copy it. How would I go about doing it? This is what I have:

public class chessBoard {

private Chess[][]board;

public chessBoard (){

board = new board[8][8];
for (int i = 0; i < 8; i ++){
   for (int j = 0; j < 8; j++){

   board[i][j] == 0;
   }
 }
}

//Is this correct implementation of a Deep copy?
public chessBoard(chessBoard other){

board = new board[8][8];

  for (int i = 0; i < 8; i ++){
      for (int j = 0; j <8 ; j++){

      board[i][j] = other.board[i][j];


      }
    }
}   

Recommended Answers

All 14 Replies

why say deep ? it looks like plain simple copy. However , you might want to add checks for the size of other , instead of hardcoding it to 8.

For a deep copy you need to copy everything, all the way down, so instead of

board[i][j] = other.board[i][j];

it should be

board[i][j] = a copy of (other.board[i][j]);

(how that works depends on how you can copy whatever is on other.board[i][j])

Is there any deepCopy() method in the api ? Also , why make a copy of a copy ? since both the instances are of the same object , and the same type of array , ie chess[][] array is being copied , what does it mean to say deepCopy given this specific example ?

I have a new chessBoard object. This object has a 2D array named board which will eventually store stuff (chess pieces). I want contents from this new chessBoard object to be exactly the same as the paramter's object. So all contents of board from current and parameter must be the same; however, it must be independent, which means if I change one board, it wont affect the other board, hence a deep copy is needed. Sorry if I was unclear.

So I don't know what I should do.

so this is what I have: Is this right?

public chessBoard (chessBoard other){

board = new board[8][8];
other.board = new board[8][8];

for (int i = 0; i <8; i++){
   for (int j = 0; j < 8; j++){

 board[i][j] = other.board[8][8];



    }
}





}
  1. Line 4 erases the current contents of other's board, so that's not a good idea.
  2. Line 9 copes the same cell (8.8) of other's arry to every cell in this's board. Also not a good idea.
  3. Whether you need a deep copy or not depends (as I posted earlier) on what the elements of board are. If they contain status info then you need to populate the new board with copies of those elements. If the elements are primitives or immutable objects then there's no need to create copies of them.

The elements are objects of a different class that represents the pieces or EMPTY. They are not primitives and so I need a deep copy. What should I change, I am not understanding this. How do I get the current board to equal a copy of the other board. How do I copy the other board first?

But are they immutable? Eg if they are Strings or enums then there's no need for a deep copy.
To repeat the info that you already have:

// Your own code for an ordinary (not deep) copy...
public chessBoard(chessBoard other){
  board = new board[8][8];
  for (int i = 0; i < 8; i ++){
    for (int j = 0; j <8 ; j++){
      board[i][j] = other.board[i][j];
    }
  }
} 

If the onbects in the board are not immutable then you replace
board[i][j] = other.board[i][j];
with
board[i][j] = (make a copy of) other.board[i][j];
how you make that copy depends on how the elements are defined, which you haven't shared with us.

Public class chessBoard{

private Chess[][] board;
//representing white turn
boolean turn = false;

public chessBoard(){

turn = false;

board = new Chess[8][8];

for (int i =0; i < 8; i++){
    for (int j = 0; j < 8; j++){

    board[i][j] = Chess.EMPTY //enum 
  }
 }
 }


 /*Question: Is this a proper deep copy of chessBoard object?? */
 public chessBoard (chessBoard other) {

    this.turn = other.turn;
    board = new Chess[8][8];

    for (int i =0; i < 8; i++){
    for (int j = 0; j < 8; j++){

    board[i][j] = other.board[i][j]
  }
}



}

oh sorry. sorry I miswrote the code and explanation: So chessBoard() object has 2 things : 1. 2D array for board that stores enums. 2. whose turn it is. I need to make sure that the chessBoard (chessBoard other) is to be deep copied, not the 2darray but the chessBoard other object (2d array and the turn), So I don't know if I am doing it right, let me re-write it.

Now we know that the elements of board are enums, the question of deep copy for the board becomes irrelevent. Enums are singletons; they cannot be copied.
So forget all about "deep copy".

The code you just posted is a proper implementation of a copy constructor. It will create a new chessBoard whose initial state is an exact copy of the state of chessBoard other. Problem solved.

ps: Java convention is that class names should begin with a capital letter, ie ChessBoard
ps: booleans should have names that make it clear what they represent - "turn" isn't helful, "isWhitesMoveNext" tells you everything, and needs no comments

OK thank you. So the copy constructor will create another ChessBoard object and if I change one the other or current ChessBoard object wont be affected correct?

That's right.

(... Just for the sake of complete accuracy ... unless you have defined your Chess enum to contain fields whose values can be changed. That's unlikely, and probably bad design, but I had to mention it)

Can you explain more about what you meant? This is what I have

public enum Chess {
    EMPTY,PAWN, KNIGHT, BISHOP, ROOK, KING, QUEEN 
}

Don't worry. Your enum is good.
I was just pointing out one possible situation where there could be a problem, but it doesn't apply in your case.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.