I have started a project trying to create a Ken Ken puzzle. If you are not sure what Ken Ken is, it is similar to Sudoku in the way that there can be no duplicated integer values in a row or column.
I am trying to fill a 2D Array with numbers from an Array List that is created for every new row. I make checks to see whether or not the number taken from the Array List does not match any numbers within its own row and column.
When I run my code I get an "Index Out Of Bounds" exception when I try removing the integer value from the list. I'm not sure why this is happening because I think I am getting the right element.
Here is my code:
int GRID_SIZE = 4;
int[][] grid = new int[GRID_SIZE][GRID_SIZE];
List<Integer> nums = new ArrayList<Integer>();
private void populateGrid() {
for (int row = 0; row < GRID_SIZE; row ++) {
// Creates an array of values from 1 to grid size.
for (int i = 1; i <= GRID_SIZE; i++) nums.add(i);
for (int col = 0; col < GRID_SIZE; col++) {
while (nums.size() > 0) {
// Gets a random number from the Array List
int ranNum = nums.get(numGen.nextInt(GRID_SIZE));
// Checks to see if the number is placeable.
if (canPlace(ranNum, row, col)) {
// Places the number in the 2D Array
grid[row][col] = ranNum;
break;
} else {
// Removes duplicate element from the Array List.
nums.remove(ranNum); <------{Index Out Of Bounds Exception]
}
}
}
}
}
private boolean canPlace(int ranNum, int row, int col) {
for (int i = 0; i < GRID_SIZE; i++) {
// Checks if the specified number is already in the row/column.
if (grid[col][i] == ranNum) return false;
if (grid[i][row] == ranNum) return false;
}
return true;
}
I have a few questions about this:
First of all, why am I getting the error that I am?
Secondly is there anything better to use than a 2D Array for the grid and the way I place my numbers?
Lastly, am I using the break correctly?
Thanks in advance for your answers.
How about a different approach to the problem? Start with a valid square and transform it. The two operations, 'exchange two rows' and 'exchange two columns' preserve the properties of the square. This allows you to do two Fisher-Yates shuffles, one on the rows and one on the columns which would give you a valid randomised square as long as you start from a valid square. Constructing an initial valid square is trivial: