Implemented most functionality for the GUI and error correction. Removed Certain display settings.

This commit is contained in:
Jonathan Turner 2024-02-11 05:09:07 -05:00
parent 6b88415ac2
commit f096276422
4 changed files with 54 additions and 35 deletions

View File

@ -53,6 +53,7 @@ public class Driver {
Date end = new Date();
if (result.board != null) {
long runtime = end.getTime() - start.getTime();
timer.add(runtime);
successes++;
node = result;

View File

@ -5,8 +5,6 @@ import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javax.swing.tree.TreeNode;
public class BoardViewCodeBehind {
@FXML
@ -15,6 +13,9 @@ public class BoardViewCodeBehind {
@FXML
private Label current_board;
@FXML
private Label alg_speed;
@FXML
private Button generate_board;
@ -22,7 +23,10 @@ public class BoardViewCodeBehind {
private MenuButton menu_alg;
@FXML
private ToggleButton showSolvedPath;
private ToggleButton expanded;
@FXML
private ToggleButton showOnlySolvedPath;
@FXML
private MenuItem AStar;
@ -39,9 +43,6 @@ public class BoardViewCodeBehind {
@FXML
private Button solve_button;
@FXML
private ProgressBar solving_prog;
@FXML
private Label gen_board_err;
@ -63,8 +64,8 @@ public class BoardViewCodeBehind {
this.AStar.disableProperty().bindBidirectional(viewModel.AStarProperty());
this.gen_board_err.visibleProperty().bindBidirectional(viewModel.genBoardErrProperty());
this.no_solv_alg_err.visibleProperty().bindBidirectional(viewModel.solvingAlgErrProperty());
this.expanded.selectedProperty().bindBidirectional(viewModel.expandedProperty());
this.alg_speed.textProperty().bindBidirectional(viewModel.algSpeedProperty());
}
public void onGenerateBoard(ActionEvent actionEvent) {
@ -72,11 +73,15 @@ public class BoardViewCodeBehind {
}
public void showSolvedPath(ActionEvent actionEvent) {
if (this.viewModel.getSolvedRootNode() != null) {
this.viewModel.updateDisplay();
}
this.spanning_tree.setRoot(viewModel.getSolvedRootNode());
}
public void onSolveButton(ActionEvent actionEvent) {
viewModel.solveBoard();
viewModel.updateDisplay();
this.spanning_tree.setRoot(viewModel.getSolvedRootNode());
}

View File

@ -8,27 +8,24 @@ import Assignments.A1.solving_algorithms.AStar;
import Assignments.A1.solving_algorithms.BFS;
import Assignments.A1.solving_algorithms.UCS;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TreeItem;
import java.util.Date;
public class MainViewModel {
private StringProperty currentBoardProperty, algSpeedProperty;
private BooleanProperty showSolvedPath, DFS, UCS, BFS, AStar, solvingAlgErr, genBoardErr;
private DoubleProperty solvingProgressProperty;
private BooleanProperty expanded, DFS, UCS, BFS, AStar, solvingAlgErr, genBoardErr;
private String selectedAlg;
private final ObservableList<BoardNode> spanningTree = FXCollections.observableArrayList();
private final ObjectProperty<ObservableList<BoardNode>> spanningTreeProperty = new SimpleObjectProperty<>(spanningTree);
private BoardNode current = new BoardNode(new Board(), null);
private TreeItem<BoardNode> solvedRootNode = null;
private BoardNode solvedRootBoardNode = null;
public MainViewModel() {
this.currentBoardProperty = new SimpleStringProperty();
this.algSpeedProperty = new SimpleStringProperty();
this.showSolvedPath = new SimpleBooleanProperty();
this.solvingProgressProperty = new SimpleDoubleProperty();
this.expanded = new SimpleBooleanProperty();
this.DFS = new SimpleBooleanProperty();
this.UCS = new SimpleBooleanProperty();
this.BFS = new SimpleBooleanProperty();
@ -83,27 +80,31 @@ public class MainViewModel {
} else if (AStar.getValue()) {
solver = new AStar();
}
Date start = new Date();
BoardNode solved = solver.traverse(this.current.board);
Date end = new Date();
long runtime = end.getTime() - start.getTime();
this.algSpeedProperty.setValue(String.valueOf(runtime));
currentBoardProperty.setValue(solved.board.toString());
BoardNode root = solved;
while (root.parent != null) {
root = root.parent;
}
this.solvedRootNode = rebuildTree(root);
this.solvedRootNode = rebuildTree(root, this.expanded.getValue());
this.solvedRootBoardNode = root;
}
public void updateDisplay() {
TreeItem<BoardNode> newRoot = null;
newRoot = this.rebuildTree(this.solvedRootBoardNode, this.expanded.getValue());
this.solvedRootNode = newRoot;
}
public TreeItem<BoardNode> getSolvedRootNode() {
return solvedRootNode;
}
public ObservableList<BoardNode> getSpanningTree() {
return spanningTreeProperty.get();
}
public ObjectProperty<ObservableList<BoardNode>> spanningTreeProperty() {
return spanningTreeProperty;
}
public BooleanProperty DFSProperty() {
return DFS;
}
@ -128,17 +129,31 @@ public class MainViewModel {
return genBoardErr;
}
public TreeItem<BoardNode> rebuildTree(BoardNode root) {
private TreeItem<BoardNode> rebuildTree(BoardNode root, boolean expanded) {
TreeItem<BoardNode> treeItem = new TreeItem<>(root);
// Recursively create TreeItems for child nodes
for (BoardNode child : root.children) {
TreeItem<BoardNode> childItem = rebuildTree(child);
TreeItem<BoardNode> childItem = rebuildTree(child, expanded);
treeItem.getChildren().add(childItem);
}
treeItem.setExpanded(expanded);
return treeItem;
}
public boolean getExpanded() {
return expanded.get();
}
public BooleanProperty expandedProperty() {
return expanded;
}
public String getAlgSpeedProperty() {
return algSpeedProperty.get();
}
public StringProperty algSpeedProperty() {
return algSpeedProperty;
}
}

View File

@ -6,7 +6,6 @@
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuButton?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.control.TreeView?>
<?import javafx.scene.layout.Pane?>
@ -34,10 +33,9 @@
</menus>
</MenuBar>
<TreeView fx:id="spanning_tree" layoutX="334.0" layoutY="67.0" prefHeight="416.0" prefWidth="532.0" />
<ToggleButton fx:id="showSolvedPath" layoutX="545.0" layoutY="34.0" mnemonicParsing="false" onAction="#showSolvedPath" text="Show Solved Path" />
<ProgressBar id="solve_board_progress" fx:id="solving_prog" layoutX="500.0" layoutY="538.0" mouseTransparent="true" prefWidth="200.0" progress="0.0" />
<ToggleButton fx:id="expanded" layoutX="547.0" layoutY="33.0" mnemonicParsing="false" onAction="#showSolvedPath" text="Expand Directory" />
<Button id="generate_board" fx:id="generate_board" layoutX="97.0" layoutY="367.0" mnemonicParsing="false" onAction="#onGenerateBoard" text="Generate Random Board" />
<Label fx:id="current_board" alignment="CENTER" layoutX="104.0" layoutY="129.0" prefHeight="184.0" prefWidth="134.0" text="0 1 2 8 0 4 7 6 5" textAlignment="JUSTIFY" wrapText="true">
<Label fx:id="current_board" alignment="CENTER" layoutX="104.0" layoutY="129.0" prefHeight="184.0" prefWidth="134.0" textAlignment="JUSTIFY" wrapText="true">
<font>
<Font size="41.0" />
</font>
@ -52,7 +50,7 @@
</items>
</MenuButton>
<Label layoutX="728.0" layoutY="507.0" text="Algorithm Speed (ms): " />
<Label layoutX="849.0" layoutY="507.0" text="0" />
<Label fx:id="alg_speed" layoutX="849.0" layoutY="507.0" text="0" />
<Label fx:id="gen_board_err" layoutX="37.0" layoutY="525.0" prefHeight="26.0" prefWidth="325.0" text="You must generate a board before solving" textFill="RED" visible="false">
<font>
<Font size="17.0" />