Implemented functionality to convert BoardNode to Treeitem for displaying iterations.

This commit is contained in:
Jonathan Turner 2024-02-11 04:08:58 -05:00
parent c944ce121a
commit 6b88415ac2
4 changed files with 47 additions and 5 deletions

View File

@ -84,4 +84,9 @@ public class BoardNode implements Comparable<BoardNode> {
public int compareTo(BoardNode o) { // BFS public int compareTo(BoardNode o) { // BFS
return Integer.compare(this.heuristic, o.heuristic); return Integer.compare(this.heuristic, o.heuristic);
} }
@Override
public String toString() {
return this.board.toString().replaceAll("\n", " ");
}
} }

View File

@ -5,10 +5,12 @@ 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
private TreeView<BoardNode> algorithm_results; private TreeView<BoardNode> spanning_tree;
@FXML @FXML
private Label current_board; private Label current_board;
@ -61,6 +63,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());
} }
public void onGenerateBoard(ActionEvent actionEvent) { public void onGenerateBoard(ActionEvent actionEvent) {
@ -73,6 +77,7 @@ public class BoardViewCodeBehind {
public void onSolveButton(ActionEvent actionEvent) { public void onSolveButton(ActionEvent actionEvent) {
viewModel.solveBoard(); viewModel.solveBoard();
this.spanning_tree.setRoot(viewModel.getSolvedRootNode());
} }
public void onAlgChange(ActionEvent actionEvent) { public void onAlgChange(ActionEvent actionEvent) {

View File

@ -8,6 +8,9 @@ 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;
public class MainViewModel { public class MainViewModel {
@ -15,8 +18,11 @@ public class MainViewModel {
private BooleanProperty showSolvedPath, DFS, UCS, BFS, AStar, solvingAlgErr, genBoardErr; private BooleanProperty showSolvedPath, DFS, UCS, BFS, AStar, solvingAlgErr, genBoardErr;
private DoubleProperty solvingProgressProperty; 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;
public MainViewModel() { public MainViewModel() {
this.currentBoardProperty = new SimpleStringProperty(); this.currentBoardProperty = new SimpleStringProperty();
@ -77,12 +83,26 @@ public class MainViewModel {
} else if (AStar.getValue()) { } else if (AStar.getValue()) {
solver = new AStar(); solver = new AStar();
} }
BoardNode solved = solver.traverse(this.current.board); BoardNode solved = solver.traverse(this.current.board);
currentBoardProperty.setValue(solved.board.toString());
System.out.println(solved); BoardNode root = solved;
while (root.parent != null) {
root = root.parent;
}
this.solvedRootNode = rebuildTree(root);
} }
public TreeItem<BoardNode> getSolvedRootNode() {
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;
@ -108,5 +128,17 @@ public class MainViewModel {
return genBoardErr; return genBoardErr;
} }
public TreeItem<BoardNode> rebuildTree(BoardNode root) {
TreeItem<BoardNode> treeItem = new TreeItem<>(root);
// Recursively create TreeItems for child nodes
for (BoardNode child : root.children) {
TreeItem<BoardNode> childItem = rebuildTree(child);
treeItem.getChildren().add(childItem);
}
return treeItem;
}
} }

View File

@ -33,7 +33,7 @@
</Menu> </Menu>
</menus> </menus>
</MenuBar> </MenuBar>
<TreeView fx:id="algorithm_results" 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="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" /> <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" />