Skip to content

Commit

Permalink
Refactor TilePane logic
Browse files Browse the repository at this point in the history
  • Loading branch information
viktigpetterr committed Jul 12, 2021
1 parent b400c25 commit e548d8e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 104 deletions.
109 changes: 5 additions & 104 deletions src/main/java/com/github/viktigpetterr/sudokusolver/javafx/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,119 +24,20 @@ public class App extends Application {

private final Sudoku sudoku = new Sudoku();

/**
* Adds TextField type to the specific TilePane in a sudoku-pattern.
*/
private void newSudokuField(TilePane tilePane, Sudoku sudoku) {

for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
TextField numField = new OneNumberTextField();
numField.setMaxSize(40, 40);
numField.setMinSize(40, 40);
numField.setAlignment(Pos.CENTER);

int i2 = i;
int j2 = j;
if (j < 3 && i < 3) {
numField.setStyle("-fx-background-color: #8c8c8c;");
}
if (j < 3 && i > 5) {
numField.setStyle("-fx-background-color: #8c8c8c;");
}
if (j > 2 && j < 6 && i > 2 && i < 6) {
numField.setStyle("-fx-background-color: #8c8c8c;");

}
if (j > 5 && i < 3) {
numField.setStyle("-fx-background-color: #8c8c8c;");
}
if (j > 5 && i > 5) {
numField.setStyle("-fx-background-color: #8c8c8c;");
}

numField.textProperty().addListener((observable, oldValue, newValue) -> {
try {
Integer.parseInt(newValue);
} catch (NumberFormatException e) {
newValue = "0";
}
sudoku.setValue(i2, j2, Integer.parseInt(newValue));
});
tilePane.getChildren().add(numField);
}
}

}

/**
* Clears the specific TilePane and then adds TextField type to the specific
* TilePane in a sudoku-pattern. Then the TextFields textProperty is filled
* with the corresponding value in sudoku.
*/

private void rebuildSolvedSudokuField(TilePane tilePane, Sudoku sudoku) {
tilePane.getChildren().clear();
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
TextField numField = new OneNumberTextField();
numField.textProperty().set(sudoku.getValueOf(i, j));
numField.setMaxSize(40, 40);
numField.setMinSize(40, 40);
numField.setAlignment(Pos.CENTER);
int i2 = i;
int j2 = j;
if (j < 3 && i < 3) {
numField.setStyle("-fx-background-color: #8c8c8c;");
}
if (j < 3 && i > 5) {
numField.setStyle("-fx-background-color: #8c8c8c;");
}
if (j > 2 && j < 6 && i > 2 && i < 6) {
numField.setStyle("-fx-background-color: #8c8c8c;");

}
if (j > 5 && i < 3) {
numField.setStyle("-fx-background-color: #8c8c8c;");
}
if (j > 5 && i > 5) {
numField.setStyle("-fx-background-color: #8c8c8c;");
}

numField.textProperty().addListener((observable, oldValue, newValue) -> {
try {
Integer.parseInt(newValue);
} catch (NumberFormatException e) {
newValue = "0";
}
sudoku.setValue(i2, j2, Integer.parseInt(newValue));
});
tilePane.getChildren().add(numField);
}
}

}

public void start(Stage stage) {
BorderPane root = new BorderPane();
root.setStyle("-fx-background-color: #e5e5e5;");
TilePane tilePane = new TilePane();
tilePane.setPadding(new Insets(10, 10, 10, 10));
tilePane.setHgap(7);
tilePane.setVgap(7);

newSudokuField(tilePane, sudoku);
root.setCenter(tilePane);
SudokuTilePane sudokuTilePane = new SudokuTilePane(sudoku);
root.setCenter(sudokuTilePane);

Button clear = new Button("Clear");
clear.setStyle("-fx-base: #f27474");
clear.setMaxSize(80, 40);
clear.setMinSize(80, 40);
clear.setOnAction(event -> {
tilePane.getChildren().clear();
sudoku.clear();
newSudokuField(tilePane, sudoku);

sudokuTilePane.refresh(true);
});

Button solve = new Button("Solve");
Expand All @@ -146,7 +47,7 @@ public void start(Stage stage) {
solve.setStyle("-fx-base: #82e584;");
solve.setOnAction(event -> {
if (sudoku.solve()) {
rebuildSolvedSudokuField(tilePane, sudoku);
sudokuTilePane.refresh(false);
} else {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Notification");
Expand Down Expand Up @@ -175,6 +76,6 @@ public void start(Stage stage) {
}

public static void main(String[] args) {
Application.launch(args);
App.launch(args);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.viktigpetterr.sudokusolver.javafx;

import javafx.geometry.Pos;
import javafx.scene.control.TextField;

/**
Expand All @@ -10,6 +11,21 @@
*/
public class OneNumberTextField extends TextField {

private static final String DARK_STYLE = "-fx-background-color: #8c8c8c;";

public OneNumberTextField(boolean styled) {
this.setMaxSize(40, 40);
this.setMinSize(40, 40);
this.setAlignment(Pos.CENTER);
if (styled) {
this.setStyle(DARK_STYLE);
}
}

public void setNumber(String number) {
this.textProperty().set(number);
}

/**
* Overrides replaceText(...) in TextInputControl.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.github.viktigpetterr.sudokusolver.javafx;

import com.github.viktigpetterr.sudokusolver.sudoku.Sudoku;
import javafx.geometry.Insets;
import javafx.scene.layout.TilePane;

public class SudokuTilePane extends TilePane {

private final Sudoku sudoku;

private static final String DEFAULT_VALUE = "0";

public SudokuTilePane(Sudoku sudoku) {
this.sudoku = sudoku;

this.setPadding(new Insets(10, 10, 10, 10));
this.setHgap(7);
this.setVgap(7);

this.refresh(true);
}

private boolean styled(int i, int j) {
return
(j < 3 && i < 3) ||
(j < 3 && i > 5) ||
(j > 2 && j < 6 && i > 2 && i < 6) ||
(j > 5 && i < 3) ||
(j > 5 && i > 5);
}

public void refresh(boolean clear) {
this.getChildren().clear();
for (int i = 0; i < sudoku.dimension(); i++) {
for (int j = 0; j < sudoku.dimension(); j++) {
OneNumberTextField numField = new OneNumberTextField(styled(i, j));
if (!clear) {
numField.setNumber(sudoku.getValueOf(i, j));
}
int finalI = i;
int finalJ = j;
numField.textProperty().addListener((observable, oldValue, newValue) -> {
try {
Integer.parseInt(newValue);
} catch (NumberFormatException e) {
newValue = DEFAULT_VALUE;
}
sudoku.setValue(finalI, finalJ, Integer.parseInt(newValue));
});

this.getChildren().add(numField);
}
}
}

}

0 comments on commit e548d8e

Please sign in to comment.