Java 提示数独游戏的单个值
我希望有人能帮我。我正在制作一个数独游戏,我编写了一个类来解决整个谜题(当然,它连接到另一个java文件中的一个动作按钮),它工作得很好 我想知道的是,如果我想做一个“提示”按钮,随机将正确的值应用到一个单独的正方形上,是否有人能给我指出正确的方向 以下是解算器代码:Java 提示数独游戏的单个值,java,arrays,Java,Arrays,我希望有人能帮我。我正在制作一个数独游戏,我编写了一个类来解决整个谜题(当然,它连接到另一个java文件中的一个动作按钮),它工作得很好 我想知道的是,如果我想做一个“提示”按钮,随机将正确的值应用到一个单独的正方形上,是否有人能给我指出正确的方向 以下是解算器代码: public class SudokuSolver { // set unchangeable number of rows to 9 private static final int ROWS = 9; // set unch
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
SudokuSolver公共类{
//将不可更改的行数设置为9
私有静态最终整数行=9;
//将不可更改的列数设置为9
私有静态final int COLUMNS=9;
//将成为解决方案的多维数组
私有int[][]解决方案;
//包含正确拼图值的9x9解决方案多维数组
公共静态int[][]解决方案=新int[行][列];
/**
*SudokuSolver构造函数获取未完成的谜题,并对其调用解算器方法以获得一个
*已完成的拼图。已完成的拼图存储在解决方案多维数组中。
*@param puzzle您要解决的难题。
*/
公共数独游戏(int[][]拼图){
//将谜题解决方案存储到“谜题”参数中
谜题解=谜题;
//解谜
解谜(解谜解,0,0);
}//结束SudokuSolver
/**
*接下来获取拼图数组中的当前位置并将其向前移动。
*next调用solvePuzzle方法继续检查下一个数组位置的值
*如果数组中有非空值,我们可以跳到空值。
*@param拼图正在向前移动的数组
*@param row数组中的行位置
*@param column数组中的列位置
*/
public void next(int[][]拼图,int行,int列){
//如果我们还在拼图栏里
如果(列<8){
//通过在列中填充空的网格方块来解决这个难题
解算拼图(拼图、行、列+1);
}否则{
//否则,请通过在行中填充空网格正方形来解决此难题
解算拼图(拼图,行+1,0);
}
}//下一步结束
/**
*solvePuzzle通过循环一个不完整的数独游戏来解决它。
*它使用下一个函数跳过已经输入的1到9个值,然后用
*新的行和列定位。如果值在其各自的行、列或块中不存在,请解决
*将值添加到数组并更新定位以检查新值
*解决方案被发送到“解决方案”阵列。
*@param谜题-正在解决的不完整数独谜题。
*@param row数组的当前行位置。
*@param col数组的当前列位置。
*/
公共拼图(int[][]拼图,int行,int列){
//如果行值大于8,则应解决每个空值
如果(第8行){
System.out.println(“\n此难题已自动解决!\n应用的解决方案:”);
//写行
对于(int r=0;r 对于(int index=1;index添加可传递到solvePuzzle函数中的布尔变量或标志。在solvePuzzle函数中,如果设置了提示标志,请在第一个已解决的框处断开。我猜实现“提示”按钮的唯一可能方法是通过稍微重构代码:
- 首先,从一开始就解决这个难题,并将解决方案存储在一个内部变量中
- 然后,如果单击“提示”按钮,则从解决方案中选择一个随机单元格并将其打印出来。跟踪填充的单元格,不要从任何单元格中选择
- 当点击“解决”按钮时,打印出整个解决方案
更新
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;
}
}
现在不是在我的电脑上,而是创建了一个使用越来越复杂的策略来找到下一个有效值的解算器。这样我就能够高效地解算它,并一步一步地解释解算的原因。如果我记得的话,我会与大家分享。如果你感兴趣的话。如果你能做到,那就太棒了!这是一个eclipse项目。非常感谢!我不知道应该在我的代码中寻找和实现什么,但我很欣赏这一分享,希望我能得到一些东西来将其写入我的代码中……没问题:)我已经写了很长时间了,但我在eclipse中启动了它,并验证了单元测试是否正常工作。我想单元测试是代码的一个相当好的切入点。如果你能从中受益,也欢迎你直接克隆和使用我的解算器,看起来你一直在使用与我相同的想法。我刚刚抛出了一些it的软件模式:p顺便说一句,策略(解算器算法)是通过解释文本描述来实现的,所以可以继续
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;
}
}