我希望有人能够帮助我。我正在制作一个数独游戏,我写了一个解决整个谜题的类(当然它连接到另一个java文件中的一个动作按钮),它运行良好。
我想知道的是,如果我想制作一个“提示”按钮,有人可以指出我正确的方向,该按钮随机将正确的值应用于一个单独的方格。
以下是解算器代码:
public class SudokuSolver {
// set unchangeable number of rows to 9
private static final int ROWS = 9;
// set unchangeable number of columns to 9
private static final int COLUMNS = 9;
// the multidimensional array that will be the solution
private int[][] puzzleSolution;
// the 9x9 solution multidimensional array containing the correct puzzle values
public static int[][] solution = new int[ROWS][COLUMNS];
/**
* SudokuSolver constructor takes your incomplete puzzle and calls the solver method on it in order to get a
* completed puzzle. The completed puzzle is stored in a solution multidimensional array.
* @param puzzle the puzzle you want to solve.
*/
public SudokuSolver(int[][] puzzle) {
// store puzzle solution into "puzzle" parameter
puzzleSolution = puzzle;
// solve the puzzle
solvePuzzle(puzzleSolution, 0, 0);
} // end SudokuSolver
/**
* next takes the current position in the puzzle array and moves it forward.
* next calls upon the solvePuzzle method to continue checking values of the next array location. This is done so
* if there is a non empty value in the array, we can skip to an empty one.
* @param puzzle the array that is being moved forward
* @param row the row position in the array
* @param column the column position in the array
*/
public void next(int[][] puzzle, int row, int column) {
// if we are still within the puzzle columns
if (column < 8) {
// solve puzzle by filling empty grid squares in columns
solvePuzzle(puzzle, row, column + 1);
} else {
// otherwise, solve the puzzle by filling empty grid squares in rows
solvePuzzle(puzzle, row + 1, 0);
}
} // end next
/**
* solvePuzzle loops through an incomplete sudoku puzzle in order to solve it.
* It skips already entered 1 to 9 values using the next function, which then calls the updated function with
* new row and column positioning. If a value doesn't already exist in its respective row, column, or block, solve
* adds the value to the array and updates the positioning to check for a new value. At the end of the array, the
* solution is sent to the "solution" array.
* @param puzzle the incomplete sudoku puzzle being solved.
* @param row the current row position of the array.
* @param col the current column position of the array.
*/
public void solvePuzzle(int[][] puzzle, int row, int col) {
// if the row value is above 8, then every empty value should be solved
if (row > 8) {
System.out.println("\nThe puzzle has been automatically solved!\nSOLUTION APPLIED:");
// writing the rows
for (int r = 0; r < ROWS; r++) {
// writing the columns
for (int c = 0; c < COLUMNS; c++) {
// array to hold the solution
int[][] solutionArray;
// places the solved array into the solution array
solutionArray = puzzle;
// print out the solution puzzle to console
System.out.print(solutionArray[r][c] + " ");
} // end writing columns
// this empty println is needed to print the puzzle line by line in the console
System.out.println();
} // end writing rows
} else {
// as long as the value in the array is not zero, skip to zero
if (puzzle[row][col] != 0) {
// move to next position of row and column
next(puzzle, row, col);
} else {
/* checks for duplicate values of 1 to 9 in rows, columns and 3x3 blocks, and if there are none, places
the number in an empty location in the array */
for (int index = 1; index <= 9; index++) {
/* checks that there are no duplicated numbers in the row, column or block, so that we can enter
the correct number into the right position in the array */
if (SudokuChecks.findRowDuplicates(puzzle, row, index) &&
SudokuChecks.findColumnDuplicates(puzzle, col, index) &&
SudokuChecks.findBlockDuplicates(puzzle, row, col, index)) {
// set number at current position to index
puzzle[row][col] = index;
// move to the next position of row and column
next(puzzle, row, col);
}
}
//puzzle[row][col] = 0;
}
}
} // end solvePuzzle
/**
* getSolution is a getter method that will get the values of a solved sudoku.
* @return will return the correct solution to a solved puzzle
*/
public static int[][] getSolution() {
// return the solution
return SudokuSolver.solution;
} // end getSolution
} // end class SudokuSolver
当用户点击解决时,解算器会在每个相应的正方形中放置正确的值,但我正在考虑实施一个提示,只是随机打开一个正方形而不是整个拼图。拼图解决方案始终打印到控制台,当单击“解决”时,解决方案将写入GUI网格。如果一个正方形中有一个值,那么它显然会被跳过。如果一个正方形是空的,它将填充控制台中确切位置的数字,从而解决难题。
我希望我能够正确地解释自己,我希望这段代码足以帮助我帮助我开始使用解决方案。非常感谢!
答案 0 :(得分:2)
我想是实现&#34;提示&#34;的唯一可行方法。按钮是通过对代码的轻微重构:
<强>更新强>
hint
算法的提案:
首先,有两种新方法:
public class SudokuSolver
{
public Coordinates hint(int[][] puzzle, int[][] solved)
{
List<Coordinates> availableCells=getAvailableCells(puzzle);
int chosenIndex=(int)Math.random()*availableCells.size();
Coordinates chosenCell=availableCells.get(chosenIndex);
int chosenRow=chosenCell.getRow();
int choseColumn=chosenCell.getColumn();
puzzle[chosenRow][chosenColumn]=solved[chosenRow][chosenColumn];
return chosenCell;
}
private List<Coordinates> getAvailableCells(int[][] puzzle)
{
List<Coordinates> list=new ArrayList<Coordinates>(MAX_ROWS*MAX_COLUMNS);
// iterate the puzzle and collect every cell ==0
return list;
}
}
还有一个用于细胞坐标的新bean类:
public class Coordinates
{
private int row;
private int column;
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getColumn() {
return column;
}
public void setColumn(int column) {
this.column = column;
}
}
答案 1 :(得分:1)
添加一个可以传递给solvePuzzle函数的布尔变量或标志。在solvePuzzle函数中,如果设置了提示标志,则在第一个已解决的框中断。