gpt4 book ai didi

JavaFX 应用程序卡住,我不知道为什么

转载 作者:行者123 更新时间:2023-11-30 07:43:58 25 4
gpt4 key购买 nike

所以,我制作了一个数独求解器作为一个项目。除了一件事之外,解决方案工作正常。当我在我知道不起作用的位置插入数字时,程序会卡住并崩溃。我想我已经找到了问题所在,它似乎陷入了无限循环,但我不明白为什么?

请记住,我是 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);
}

}

最佳答案

问题似乎是你的solve(int i, int j)被一遍又一遍地调用(隐性),如果没有找到解决方案,它会卡住GUI。看来你的算法没有很好地实现。

通常,最好的建议是在单独的线程中(即不在 GUI 线程中)执行计算量大的代码。

关于JavaFX 应用程序卡住,我不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34202481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com