Java Reference
In-Depth Information
that was added to the SudokuGrid.java source file. The purpose of this method
is to convert the two-dimensional grid array into a one-dimensional array of
type int . This then maps directly to a JavaFX Integer[] sequence.
Listing 13.1
Java Method to Convert a Two-Dimensional Array into a
One-Dimensional JavaFX Sequence
/*
* Method to move generated Sudoku game to JavaFX.
* Internally, the Sudoku grid generated by this
* program is represented as a 2-dimensional array
* in Java. JavaFX 1.1 has no notion of
* multi-dimensional sequences (arrays), so flatten
* the grid out to a one-dimensional array of size
* 81 of type int. This should map to a JavaFX
* sequence of type Integer.
*/
public int[] returnGridSequence() {
int[] gridSequence = new int[81];
int index = 0;
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
gridSequence[index++] = grid[i][j];
}
}
return gridSequence;
}
Using this method, we can now tackle the first task, constructing the necessary
JavaFX code to generate a new JavaFX puzzle using the net.sourceforge
.playsudoku package. Listing 13.2 shows the complete newPuzzle() function,
which is contained within the Board.fx source file. Let's first take a look at a few
of the parts of this function to see what's going on. The first lines of newPuzzle()
call the necessary Java methods inside net.sourceforge.playsudoku to gener-
ate a new puzzle:
/*
* Call SudokuGenerator Java code to generate new puzzle.
*/
var sudokuGenerator : SudokuGenerator =
new SudokuGenerator();
sudokuGenerator.generatePuzzle(numHints,
GV.NumDistributuon.random );
var sudokuGrid : SudokuGrid = sudokuGenerator.getGrid();
 
Search WWH ::




Custom Search