Implemented Code Behind and VM file.

This commit is contained in:
Jonathan Turner 2024-02-11 03:39:09 -05:00
parent 1e554fbdb0
commit c944ce121a
3 changed files with 216 additions and 4 deletions

View File

@ -249,10 +249,9 @@ public class Board {
// } // }
@Override @Override
public String toString() { public String toString() {
String result = ""; String result = pieces[0] + " " + pieces[1] + " " + pieces[2] + "\n"
for (int i = 0; i < 9; i++) { + pieces[3] + " " + pieces[4] + " " + pieces[5] + "\n"
result += pieces[i] + " "; + pieces[6] + " " + pieces[7] + " " + pieces[8];
}
return result; return result;
} }

View File

@ -0,0 +1,101 @@
package Assignments.A1.view;
import Assignments.A1.models.BoardNode;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
public class BoardViewCodeBehind {
@FXML
private TreeView<BoardNode> algorithm_results;
@FXML
private Label current_board;
@FXML
private Button generate_board;
@FXML
private MenuButton menu_alg;
@FXML
private ToggleButton showSolvedPath;
@FXML
private MenuItem AStar;
@FXML
private MenuItem BFS;
@FXML
private MenuItem DFS;
@FXML
private MenuItem UCS;
@FXML
private Button solve_button;
@FXML
private ProgressBar solving_prog;
@FXML
private Label gen_board_err;
@FXML
private Label no_solv_alg_err;
private MainViewModel viewModel;
public BoardViewCodeBehind() {
this.viewModel = new MainViewModel();
}
@FXML
private void initialize() {
this.current_board.textProperty().bindBidirectional(viewModel.getCurrentBoardProperty());
this.DFS.disableProperty().bindBidirectional(viewModel.DFSProperty());
this.UCS.disableProperty().bindBidirectional(viewModel.UCSProperty());
this.BFS.disableProperty().bindBidirectional(viewModel.BFSProperty());
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) {
this.viewModel.generateBoard();
}
public void showSolvedPath(ActionEvent actionEvent) {
}
public void onSolveButton(ActionEvent actionEvent) {
viewModel.solveBoard();
}
public void onAlgChange(ActionEvent actionEvent) {
}
public void onDFS(ActionEvent actionEvent) {
viewModel.setSelectedAlg("DFS");
this.menu_alg.textProperty().set(this.menu_alg.getItems().get(0).textProperty().get());
}
public void onUCS(ActionEvent actionEvent) {
viewModel.setSelectedAlg("UCS");
this.menu_alg.textProperty().set(this.menu_alg.getItems().get(1).textProperty().get());
}
public void onBFS(ActionEvent actionEvent) {
viewModel.setSelectedAlg("BFS");
this.menu_alg.textProperty().set(this.menu_alg.getItems().get(2).textProperty().get());
}
public void onAStar(ActionEvent actionEvent) {
viewModel.setSelectedAlg("AStar");
this.menu_alg.textProperty().set(this.menu_alg.getItems().get(3).textProperty().get());
}
}

View File

@ -0,0 +1,112 @@
package Assignments.A1.view;
import Assignments.A1.models.Board;
import Assignments.A1.models.BoardGenerator;
import Assignments.A1.models.BoardNode;
import Assignments.A1.models.Solver;
import Assignments.A1.solving_algorithms.AStar;
import Assignments.A1.solving_algorithms.BFS;
import Assignments.A1.solving_algorithms.UCS;
import javafx.beans.property.*;
public class MainViewModel {
private StringProperty currentBoardProperty, algSpeedProperty;
private BooleanProperty showSolvedPath, DFS, UCS, BFS, AStar, solvingAlgErr, genBoardErr;
private DoubleProperty solvingProgressProperty;
private String selectedAlg;
private BoardNode current = new BoardNode(new Board(), null);
public MainViewModel() {
this.currentBoardProperty = new SimpleStringProperty();
this.algSpeedProperty = new SimpleStringProperty();
this.showSolvedPath = new SimpleBooleanProperty();
this.solvingProgressProperty = new SimpleDoubleProperty();
this.DFS = new SimpleBooleanProperty();
this.UCS = new SimpleBooleanProperty();
this.BFS = new SimpleBooleanProperty();
this.AStar = new SimpleBooleanProperty();
this.solvingAlgErr = new SimpleBooleanProperty();
this.genBoardErr = new SimpleBooleanProperty();
this.solvingAlgErr.set(false);
this.genBoardErr.set(false);
}
public String getSelectedAlg() {
return this.selectedAlg;
}
public void setSelectedAlg(String value) {
this.selectedAlg = value;
this.DFS.set(value.equals("DFS"));
this.UCS.set(value.equals("UCS"));
this.BFS.set(value.equals("BFS"));
this.AStar.set(value.equals("AStar"));
}
public StringProperty getCurrentBoardProperty() {
return this.currentBoardProperty;
}
public void generateBoard() {
this.current = new BoardNode(BoardGenerator.generateBoard(), null);
currentBoardProperty.set(this.current.board.toString());
}
public void solveBoard() {
if (selectedAlg == null || selectedAlg.isEmpty()) {
this.solvingAlgErr.setValue(true);
return;
}
this.solvingAlgErr.setValue(false);
if (current.board.equals(new Board())) {
this.genBoardErr.setValue(true);
return;
}
this.genBoardErr.setValue(false);
Solver solver = null;
if (DFS.getValue()) {
// solver = new DFS();
} else if (UCS.getValue()) {
solver = new UCS();
} else if (BFS.getValue()) {
solver = new BFS();
} else if (AStar.getValue()) {
solver = new AStar();
}
BoardNode solved = solver.traverse(this.current.board);
System.out.println(solved);
}
public BooleanProperty DFSProperty() {
return DFS;
}
public BooleanProperty UCSProperty() {
return UCS;
}
public BooleanProperty BFSProperty() {
return BFS;
}
public BooleanProperty AStarProperty() {
return AStar;
}
public BooleanProperty solvingAlgErrProperty() {
return solvingAlgErr;
}
public BooleanProperty genBoardErrProperty() {
return genBoardErr;
}
}