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(); Date end = new Date();
if (result.board != null) { if (result.board != null) {
long runtime = end.getTime() - start.getTime(); long runtime = end.getTime() - start.getTime();
timer.add(runtime); timer.add(runtime);
successes++; successes++;
node = result; node = result;

View File

@ -5,8 +5,6 @@ import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.*; import javafx.scene.control.*;
import javax.swing.tree.TreeNode;
public class BoardViewCodeBehind { public class BoardViewCodeBehind {
@FXML @FXML
@ -15,6 +13,9 @@ public class BoardViewCodeBehind {
@FXML @FXML
private Label current_board; private Label current_board;
@FXML
private Label alg_speed;
@FXML @FXML
private Button generate_board; private Button generate_board;
@ -22,7 +23,10 @@ public class BoardViewCodeBehind {
private MenuButton menu_alg; private MenuButton menu_alg;
@FXML @FXML
private ToggleButton showSolvedPath; private ToggleButton expanded;
@FXML
private ToggleButton showOnlySolvedPath;
@FXML @FXML
private MenuItem AStar; private MenuItem AStar;
@ -39,9 +43,6 @@ public class BoardViewCodeBehind {
@FXML @FXML
private Button solve_button; private Button solve_button;
@FXML
private ProgressBar solving_prog;
@FXML @FXML
private Label gen_board_err; private Label gen_board_err;
@ -63,8 +64,8 @@ public class BoardViewCodeBehind {
this.AStar.disableProperty().bindBidirectional(viewModel.AStarProperty()); this.AStar.disableProperty().bindBidirectional(viewModel.AStarProperty());
this.gen_board_err.visibleProperty().bindBidirectional(viewModel.genBoardErrProperty()); this.gen_board_err.visibleProperty().bindBidirectional(viewModel.genBoardErrProperty());
this.no_solv_alg_err.visibleProperty().bindBidirectional(viewModel.solvingAlgErrProperty()); 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) { public void onGenerateBoard(ActionEvent actionEvent) {
@ -72,11 +73,15 @@ public class BoardViewCodeBehind {
} }
public void showSolvedPath(ActionEvent actionEvent) { public void showSolvedPath(ActionEvent actionEvent) {
if (this.viewModel.getSolvedRootNode() != null) {
this.viewModel.updateDisplay();
}
this.spanning_tree.setRoot(viewModel.getSolvedRootNode());
} }
public void onSolveButton(ActionEvent actionEvent) { public void onSolveButton(ActionEvent actionEvent) {
viewModel.solveBoard(); viewModel.solveBoard();
viewModel.updateDisplay();
this.spanning_tree.setRoot(viewModel.getSolvedRootNode()); 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.BFS;
import Assignments.A1.solving_algorithms.UCS; import Assignments.A1.solving_algorithms.UCS;
import javafx.beans.property.*; import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TreeItem; import javafx.scene.control.TreeItem;
import java.util.Date;
public class MainViewModel { public class MainViewModel {
private StringProperty currentBoardProperty, algSpeedProperty; private StringProperty currentBoardProperty, algSpeedProperty;
private BooleanProperty showSolvedPath, DFS, UCS, BFS, AStar, solvingAlgErr, genBoardErr; private BooleanProperty expanded, DFS, UCS, BFS, AStar, solvingAlgErr, genBoardErr;
private DoubleProperty solvingProgressProperty;
private String selectedAlg; 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 BoardNode current = new BoardNode(new Board(), null);
private TreeItem<BoardNode> solvedRootNode = null; private TreeItem<BoardNode> solvedRootNode = null;
private BoardNode solvedRootBoardNode = null;
public MainViewModel() { public MainViewModel() {
this.currentBoardProperty = new SimpleStringProperty(); this.currentBoardProperty = new SimpleStringProperty();
this.algSpeedProperty = new SimpleStringProperty(); this.algSpeedProperty = new SimpleStringProperty();
this.showSolvedPath = new SimpleBooleanProperty(); this.expanded = new SimpleBooleanProperty();
this.solvingProgressProperty = new SimpleDoubleProperty();
this.DFS = new SimpleBooleanProperty(); this.DFS = new SimpleBooleanProperty();
this.UCS = new SimpleBooleanProperty(); this.UCS = new SimpleBooleanProperty();
this.BFS = new SimpleBooleanProperty(); this.BFS = new SimpleBooleanProperty();
@ -83,27 +80,31 @@ public class MainViewModel {
} else if (AStar.getValue()) { } else if (AStar.getValue()) {
solver = new AStar(); solver = new AStar();
} }
Date start = new Date();
BoardNode solved = solver.traverse(this.current.board); 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()); currentBoardProperty.setValue(solved.board.toString());
BoardNode root = solved; BoardNode root = solved;
while (root.parent != null) { while (root.parent != null) {
root = root.parent; 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() { public TreeItem<BoardNode> getSolvedRootNode() {
return solvedRootNode; return solvedRootNode;
} }
public ObservableList<BoardNode> getSpanningTree() {
return spanningTreeProperty.get();
}
public ObjectProperty<ObservableList<BoardNode>> spanningTreeProperty() {
return spanningTreeProperty;
}
public BooleanProperty DFSProperty() { public BooleanProperty DFSProperty() {
return DFS; return DFS;
} }
@ -128,17 +129,31 @@ public class MainViewModel {
return genBoardErr; return genBoardErr;
} }
public TreeItem<BoardNode> rebuildTree(BoardNode root) { private TreeItem<BoardNode> rebuildTree(BoardNode root, boolean expanded) {
TreeItem<BoardNode> treeItem = new TreeItem<>(root); TreeItem<BoardNode> treeItem = new TreeItem<>(root);
// Recursively create TreeItems for child nodes
for (BoardNode child : root.children) { for (BoardNode child : root.children) {
TreeItem<BoardNode> childItem = rebuildTree(child); TreeItem<BoardNode> childItem = rebuildTree(child, expanded);
treeItem.getChildren().add(childItem); treeItem.getChildren().add(childItem);
} }
treeItem.setExpanded(expanded);
return treeItem; 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.MenuBar?>
<?import javafx.scene.control.MenuButton?> <?import javafx.scene.control.MenuButton?>
<?import javafx.scene.control.MenuItem?> <?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.control.ToggleButton?> <?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.control.TreeView?> <?import javafx.scene.control.TreeView?>
<?import javafx.scene.layout.Pane?> <?import javafx.scene.layout.Pane?>
@ -34,10 +33,9 @@
</menus> </menus>
</MenuBar> </MenuBar>
<TreeView fx:id="spanning_tree" layoutX="334.0" layoutY="67.0" prefHeight="416.0" prefWidth="532.0" /> <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" /> <ToggleButton fx:id="expanded" layoutX="547.0" layoutY="33.0" mnemonicParsing="false" onAction="#showSolvedPath" text="Expand Directory" />
<ProgressBar id="solve_board_progress" fx:id="solving_prog" layoutX="500.0" layoutY="538.0" mouseTransparent="true" prefWidth="200.0" progress="0.0" />
<Button id="generate_board" fx:id="generate_board" layoutX="97.0" layoutY="367.0" mnemonicParsing="false" onAction="#onGenerateBoard" text="Generate Random Board" /> <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>
<Font size="41.0" /> <Font size="41.0" />
</font> </font>
@ -52,7 +50,7 @@
</items> </items>
</MenuButton> </MenuButton>
<Label layoutX="728.0" layoutY="507.0" text="Algorithm Speed (ms): " /> <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"> <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>
<Font size="17.0" /> <Font size="17.0" />