Implemented functionality to convert BoardNode to Treeitem for displaying iterations.
This commit is contained in:
parent
c944ce121a
commit
6b88415ac2
@ -84,4 +84,9 @@ public class BoardNode implements Comparable<BoardNode> {
|
||||
public int compareTo(BoardNode o) { // BFS
|
||||
return Integer.compare(this.heuristic, o.heuristic);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.board.toString().replaceAll("\n", " ");
|
||||
}
|
||||
}
|
||||
|
@ -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<BoardNode> algorithm_results;
|
||||
private TreeView<BoardNode> 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) {
|
||||
|
@ -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<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;
|
||||
|
||||
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<BoardNode> getSolvedRootNode() {
|
||||
return solvedRootNode;
|
||||
}
|
||||
|
||||
public ObservableList<BoardNode> getSpanningTree() {
|
||||
return spanningTreeProperty.get();
|
||||
}
|
||||
|
||||
public ObjectProperty<ObservableList<BoardNode>> spanningTreeProperty() {
|
||||
return spanningTreeProperty;
|
||||
}
|
||||
|
||||
public BooleanProperty DFSProperty() {
|
||||
return DFS;
|
||||
@ -108,5 +128,17 @@ public class MainViewModel {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -33,7 +33,7 @@
|
||||
</Menu>
|
||||
</menus>
|
||||
</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" />
|
||||
<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" />
|
||||
|
Loading…
Reference in New Issue
Block a user