所以,我把Sudoku Solver作为一个项目。除了一件事,解决方案工作正常。当我在已知无法工作的位置插入数字时,程序会冻结并崩溃。我想我找到了问题,它似乎陷入无限循环,但我无法弄清楚为什么?
请记住,我是JavaFX的新手。
提前感谢任何帮助。
数独类
public class Sudoku {
private int board[][];
public Sudoku(){
this.board = new int[9][9];
clear();
initBoard();
}
public void printBoard(){
for(int i = 0; i < 9; i++){
if((i % 3) == 0){
System.out.print("-------------------------" + "\n");
}
for(int n = 0; n <9; n++){
if((n % 3) == 0){
System.out.print("| ");
}
System.out.print(board[i][n] + " ");
}
System.out.print("|" + "\n");
}
System.out.print("-------------------------");
}
public int getValueAt(int col, int row){
return board[col][row];
}
public boolean solve(){
if(solve(0,0)){
return true;
}else{
clear();
return false;
}
}
public void clear(){
for( int row = 0; row < 9; row++ ){
for( int col = 0; col < 9; col++ ){
board[row][col] = 0 ;
}
}
}
public void setBoard(int input[][]){
clear();
board = input;
}
private void initBoard(){
clear();
//Rad 1
board[0][2] = 8;
board[0][5] = 8;
board[0][7] = 6;
board[0][8] = 2;
//Rad 2
board[1][8] = 5;
//Rad 3
board[2][0] = 1;
board[2][2] = 2;
board[2][3] = 5;
//Rad 4
board[3][3] = 2;
board[3][4] = 1;
board[3][7] = 9;
//Rad 5
board[4][1] = 5;
board[4][6] = 6;
//Rad 6
board[5][0] = 6;
board[5][7] = 2;
board[5][8] = 8;
//Rad 7
board[6][0] = 4;
board[6][1] = 1;
board[6][3] = 6;
board[6][5] = 8;
//Rad 8
board[7][0] = 8;
board[7][1] = 6;
board[7][4] = 3;
board[7][6] = 1;
//Rad 9
board[8][6] = 4;
}
private boolean solve(int i, int j){
//Ifall en lösning har hittats
if(i > 8){
return true;
}
//Ifall positionen inte är satt
if(board[i][j] != 0){
//next(i, j);
if(j < 8){
if(solve(i, j+1)){
return true;
}
}else{
if(solve(i+1, 0)){
return true;
}
}
}
//Ifall positionen är satt
else{
for(int n = 1; n < 10; n++){
//Kollar ifall siffran "n" inte finns på någon av raderna
if(rowIsOk(i, n) && colIsOk(j, n) && boxIsOk(i, j ,n)){
board[i][j] = n;
//next(i, j);
if(j < 8){
if(solve(i, j+1)){
return true;
}
}else{
if(solve(i+1, 0)){
return true;
}
}
}
}
board[i][j] = 0;
}
//board[i][j] = 0;
return false;
}
//Kollar om raden är ok
private boolean rowIsOk(int row, int num){
for(int i = 0; i < 9; i++){
if(board[row][i] == num){
return false;
}
}
return true;
}
//Kollar om kolumnen är ok
private boolean colIsOk(int col, int num){
for(int i = 0; i < 9; i++){
if(board[i][col] == num){
return false;
}
}
return true;
}
private boolean boxIsOk(int row, int col, int num){
row = (row/3) * 3;
col = (col/3) * 3;
for(int r = 0; r < 3; r++){
for(int c = 0; c < 3; c++){
if(board[row+r][col+c] == num){
return false;
}
}
}
return true;
}
}
GUI类
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
public class SudokuGUI extends Application{
private Sudoku sudoku = new Sudoku();
private TilePane board;
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage primaryStage){
primaryStage.setTitle("Sudoku Solver: H4xx0r Edition");
BorderPane root = new BorderPane();
board = initBoard();
HBox control = addControl();
root.setCenter(board);
root.setBottom(control);
primaryStage.setScene(new Scene(root, 500, 500));
primaryStage.show();
}
private void updateBoard(TilePane tile){
tile.getChildren().clear();
for(int col = 0; col < 9; col++){
for(int row = 0; row < 9; row++){
tile.getChildren().add(createCell(col, row));
}
}
}
private TilePane initBoard(){
TilePane tile = new TilePane();
tile.setHgap(4);
tile.setVgap(4);
tile.setPadding(new Insets(0, 10, 0, 10));
tile.setPrefColumns(9);
tile.setPrefRows(9);
//tile.setStyle("-fx-background-color: rgba(255, 215, 0, 0.1);");
tile.setAlignment(Pos.CENTER);
tile.getChildren().clear();
for(int col = 0; col < 9; col++){
for(int row = 0; row < 9; row++){
tile.getChildren().add(createCell(col, row));
}
}
return tile;
}
//Skapar "celler" med textfält
private TextField createCell(int col, int row){
TextField txtfield = new TextField();
String text;
if(sudoku.getValueAt(col, row) != 0){
text = Integer.toString(sudoku.getValueAt(col, row));
}else{
text = "0";
}
txtfield.setText(text);
txtfield.setPrefWidth(45);
txtfield.setPrefHeight(45);
txtfield.setAlignment(Pos.CENTER);
//txtfield.setEditable(false);
txtfield.setStyle("-fx-font-weight: bold;");
if(((row < 6 && row > 2) && (col < 6 && col > 2))
|| ((row < 3) && (col < 3)) //Vänster Upp
|| ((row < 3) && (col > 5)) //Höger Upp
|| ((row > 5) && (col > 5)) //Höger Ner
|| ((row > 5) && (col < 3)))//Vänster Ner
{
txtfield.setStyle("-fx-background-color: rgba(255, 178, 0, 0.9);" + "-fx-font-weight: bold;");
}
return txtfield;
}
//LÄgger till knappar i en Box
private HBox addControl(){
HBox hbox = new HBox();
Button solve = new Button("Solve");
Button clear = new Button("Clear");
Button set = new Button("Set");
hbox.setPadding(new Insets(15, 12, 15, 12));
hbox.setSpacing(10);
hbox.setAlignment(Pos.BOTTOM_CENTER);
solve.setPrefSize(100, 20);
solve.setOnAction(e -> solve());
clear.setPrefSize(100, 20);
clear.setOnAction(e -> clear());
set.setPrefSize(100, 20);
set.setOnAction(e -> set());
hbox.getChildren().addAll(solve, clear, set);
return hbox;
}
//Knapp-funktioner
private void solve(){
if(sudoku.solve()){
updateBoard(board);
}else{
sudoku.clear();
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("YOLO SWAGGINS");
alert.setHeaderText("Sudokut SWAG ERROR");
alert.setContentText("Sudokut har inte tillräckligt med SWAG");
}
}
//Hämtar värden från textfälten
private void set(){
int newset[][] = new int[9][9];
int array[] = new int[board.getChildren().size()];
for(int i = 0; i < board.getChildren().size(); i++){
TextField tmp = (TextField) board.getChildren().get(i);
//System.out.println(tmp.getText());
array[i] = Integer.parseInt(tmp.getText());
}
int n = 0;
for(int col = 0; col < 9; col++){
for(int row = 0; row < 9; row++){
newset[col][row] = array[n];
n++;
}
}
sudoku.setBoard(newset);
updateBoard(board);
}
private void clear(){
sudoku.clear();
updateBoard(board);
}
}
答案 0 :(得分:1)
问题似乎是你的solve(int i,int j)被反复调用(recusive),如果没有找到解决方案,会冻结GUI。您的算法似乎没有很好地实现。
通常,在单独的线程中(即不在GUI线程中)执行计算成本高昂的代码是一个很好的建议。