0

I have an algorithm to generate a solved Sudoku board. In my tests I want to check whether two subsequent calls to the fillBoard() method, generate a different board, with this code:

SudokuBoard sb = new SudokuBoard();
sb.fillBoard();
int [][] board1 = sb.getBoard();
sb.fillBoard();
int [][] board2 = sb.getBoard();
int same = 0;

for(int i = 0; i < 9; i++)
{
    for(int y = 0; y < 9; y++)
    {
        if(board1[i][y] == board2[i][y])
            same++;
    }
}
assertTrue(same < 10);

When I assign sb.getBoard() to board2, board1 changes as well. How can that be solved?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • [How do I copy a 2 Dimensional array in Java?](https://stackoverflow.com/questions/5617016/how-do-i-copy-a-2-dimensional-array-in-java) – OH GOD SPIDERS Mar 16 '22 at 13:36
  • You either need to clone / copy the return value of `getBoard()` (or change `getBoard()` to return a copy), change `SudokuBoard` to use a new array for each `fillBoard()` call, or create a new `SudokuBoard`. – Mark Rotteveel Mar 16 '22 at 13:36

0 Answers0