diff --git a/src/main/java/Assignments/A1/Driver.java b/src/main/java/Assignments/A1/Driver.java index 1137e9e..03da119 100644 --- a/src/main/java/Assignments/A1/Driver.java +++ b/src/main/java/Assignments/A1/Driver.java @@ -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; diff --git a/src/main/java/Assignments/A1/view/BoardViewCodeBehind.java b/src/main/java/Assignments/A1/view/BoardViewCodeBehind.java index b9d26ea..02a448b 100644 --- a/src/main/java/Assignments/A1/view/BoardViewCodeBehind.java +++ b/src/main/java/Assignments/A1/view/BoardViewCodeBehind.java @@ -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()); } diff --git a/src/main/java/Assignments/A1/view/MainViewModel.java b/src/main/java/Assignments/A1/view/MainViewModel.java index 92af376..50d173f 100644 --- a/src/main/java/Assignments/A1/view/MainViewModel.java +++ b/src/main/java/Assignments/A1/view/MainViewModel.java @@ -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 spanningTree = FXCollections.observableArrayList(); - private final ObjectProperty> spanningTreeProperty = new SimpleObjectProperty<>(spanningTree); private BoardNode current = new BoardNode(new Board(), null); private TreeItem 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 newRoot = null; + newRoot = this.rebuildTree(this.solvedRootBoardNode, this.expanded.getValue()); + this.solvedRootNode = newRoot; } public TreeItem getSolvedRootNode() { return solvedRootNode; } - public ObservableList getSpanningTree() { - return spanningTreeProperty.get(); - } - - public ObjectProperty> spanningTreeProperty() { - return spanningTreeProperty; - } - public BooleanProperty DFSProperty() { return DFS; } @@ -128,17 +129,31 @@ public class MainViewModel { return genBoardErr; } - public TreeItem rebuildTree(BoardNode root) { + private TreeItem rebuildTree(BoardNode root, boolean expanded) { TreeItem treeItem = new TreeItem<>(root); - - // Recursively create TreeItems for child nodes for (BoardNode child : root.children) { - TreeItem childItem = rebuildTree(child); + TreeItem 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; + } } diff --git a/src/main/resources/A1/view/BoardView.fxml b/src/main/resources/A1/view/BoardView.fxml index 3d12642..848d5cc 100644 --- a/src/main/resources/A1/view/BoardView.fxml +++ b/src/main/resources/A1/view/BoardView.fxml @@ -6,7 +6,6 @@ - @@ -34,10 +33,9 @@ - - +