提示数独谜题的单个值
我希望有人能够帮助我。 我正在制作一款Sudoku游戏,并且我编写了一个解决整个难题的类(它当然与另一个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网格。 如果一个正方形有一个值,它显然会被跳过。 如果一个正方形是空的,它将被填充在控制台中确切位置的数字,从而解决这个难题。
我希望我能够正确地解释自己,并且我希望这个代码足以让某个人帮助我开始解决问题。 非常感谢!
我猜想实现“提示”按钮的唯一可能方式是对代码进行轻微的重构:
更新
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;
}
}
添加一个可传递给solvePuzzle函数的布尔变量或标志。 在solvePuzzle函数中,如果设置了提示标志,则在第一个解决的框中断开。
链接地址: http://www.djcxy.com/p/96169.html