From 6b88415ac20fc071670d266f84a700414763a267 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sun, 11 Feb 2024 04:08:58 -0500 Subject: [PATCH] Implemented functionality to convert BoardNode to Treeitem for displaying iterations. --- .../java/Assignments/A1/models/BoardNode.java | 5 +++ .../A1/view/BoardViewCodeBehind.java | 7 +++- .../Assignments/A1/view/MainViewModel.java | 38 +++++++++++++++++-- src/main/resources/A1/view/BoardView.fxml | 2 +- 4 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/main/java/Assignments/A1/models/BoardNode.java b/src/main/java/Assignments/A1/models/BoardNode.java index cf45344..f979453 100644 --- a/src/main/java/Assignments/A1/models/BoardNode.java +++ b/src/main/java/Assignments/A1/models/BoardNode.java @@ -84,4 +84,9 @@ public class BoardNode implements Comparable { public int compareTo(BoardNode o) { // BFS return Integer.compare(this.heuristic, o.heuristic); } + + @Override + public String toString() { + return this.board.toString().replaceAll("\n", " "); + } } diff --git a/src/main/java/Assignments/A1/view/BoardViewCodeBehind.java b/src/main/java/Assignments/A1/view/BoardViewCodeBehind.java index c36b77c..b9d26ea 100644 --- a/src/main/java/Assignments/A1/view/BoardViewCodeBehind.java +++ b/src/main/java/Assignments/A1/view/BoardViewCodeBehind.java @@ -5,10 +5,12 @@ import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.*; +import javax.swing.tree.TreeNode; + public class BoardViewCodeBehind { @FXML - private TreeView algorithm_results; + private TreeView spanning_tree; @FXML private Label current_board; @@ -61,6 +63,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()); + + } public void onGenerateBoard(ActionEvent actionEvent) { @@ -73,6 +77,7 @@ public class BoardViewCodeBehind { public void onSolveButton(ActionEvent actionEvent) { viewModel.solveBoard(); + this.spanning_tree.setRoot(viewModel.getSolvedRootNode()); } public void onAlgChange(ActionEvent actionEvent) { diff --git a/src/main/java/Assignments/A1/view/MainViewModel.java b/src/main/java/Assignments/A1/view/MainViewModel.java index 2207426..92af376 100644 --- a/src/main/java/Assignments/A1/view/MainViewModel.java +++ b/src/main/java/Assignments/A1/view/MainViewModel.java @@ -8,6 +8,9 @@ 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; public class MainViewModel { @@ -15,8 +18,11 @@ public class MainViewModel { private BooleanProperty showSolvedPath, DFS, UCS, BFS, AStar, solvingAlgErr, genBoardErr; private DoubleProperty solvingProgressProperty; 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; public MainViewModel() { this.currentBoardProperty = new SimpleStringProperty(); @@ -77,12 +83,26 @@ public class MainViewModel { } else if (AStar.getValue()) { solver = new AStar(); } - BoardNode solved = solver.traverse(this.current.board); - - System.out.println(solved); + currentBoardProperty.setValue(solved.board.toString()); + BoardNode root = solved; + while (root.parent != null) { + root = root.parent; + } + this.solvedRootNode = rebuildTree(root); } + public TreeItem getSolvedRootNode() { + return solvedRootNode; + } + + public ObservableList getSpanningTree() { + return spanningTreeProperty.get(); + } + + public ObjectProperty> spanningTreeProperty() { + return spanningTreeProperty; + } public BooleanProperty DFSProperty() { return DFS; @@ -108,5 +128,17 @@ public class MainViewModel { return genBoardErr; } + public TreeItem rebuildTree(BoardNode root) { + TreeItem treeItem = new TreeItem<>(root); + + // Recursively create TreeItems for child nodes + for (BoardNode child : root.children) { + TreeItem childItem = rebuildTree(child); + treeItem.getChildren().add(childItem); + } + + return treeItem; + } + } diff --git a/src/main/resources/A1/view/BoardView.fxml b/src/main/resources/A1/view/BoardView.fxml index 1e26e87..3d12642 100644 --- a/src/main/resources/A1/view/BoardView.fxml +++ b/src/main/resources/A1/view/BoardView.fxml @@ -33,7 +33,7 @@ - +