I am writing a chess game in Java. I have a Board class that contains the String 2D array board. I need to be able to create a tempBoard that copies the mainBoard. Make some changes and run tests on tempBoard without changing the 2d array in mainBoard.
public class Board implements Cloneable {
public String[][] board = new String[][]{
{"bR ", "bN ", "bB ", "bQ ", "bK ", "bB ", "bN ", "bR "},
{"bp ", "bp ", "bp ", "bp ", "bp ", "bp ", "bp ", "bp "},
{" ", "## ", " ", "## ", " ", "## ", " ", "## "},
{"## ", " ", "## ", " ", "## ", " ", "## ", " "},
{" ", "## ", " ", "## ", " ", "## ", " ", "## "},
{"## ", " ", "## ", " ", "## ", " ", "## ", " "},
{"wp ", "wp ", "wp ", "wp ", "wp ", "wp ", "wp ", "wp "},
{"wR ", "wN ", "wB ", "wQ ", "wK ", "wB ", "wN ", "wR "}
};
public Board() {
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
Board tempBoard = (Board ) Mainboard.clone();
tempBoard.updateBoard(move);
^ in another class in main I am attempting to updateBoard on tempBoard but this is also changing the array in mainBoard. Any suggestions?