-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFXMLDocumentController.java
More file actions
153 lines (137 loc) · 6.3 KB
/
FXMLDocumentController.java
File metadata and controls
153 lines (137 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package CryptarithmeticSolver;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
public class FXMLDocumentController implements Initializable {
@FXML
public TextField wordField;
@FXML
public TextField summationField;
@FXML
private Label wordLabel;
@FXML
private Label summationLabel;
@FXML
private Label timeTaken;
@FXML
Label combinationsTried;
@FXML
private ComboBox<String> operatorComboBox;
@FXML
private ComboBox<String> baseComboBox;
@FXML
private ComboBox<String> algorithmComboBox;
@FXML
public TreeView<String> optionsTree;
@FXML
public ListView solutionsListView;
@FXML
public SplitPane splitPane;
BruteSolve bruteSolution;
SmartSolve smartSolution;
PresentBruteSolution showBruteSolutions;
PresentSmartSolution showSmartSolutions;
ObservableList<String> operators = FXCollections.observableArrayList(
"+", "-", "*", "/"
);
ObservableList<String> baseOptions = FXCollections.observableArrayList(
"10", "11", "12", "13", "14", "15", "16"
);
ObservableList<String> algorithmOptions = FXCollections.observableArrayList(
"Brute Force", "Smart Search"
);
@Override
public void initialize(URL url, ResourceBundle rb) {
//add operators to the operator combobox
operatorComboBox.setItems(operators);
//set the default operator as +
operatorComboBox.getSelectionModel().selectFirst();
//add different bases to the base combobox
baseComboBox.setItems(baseOptions);
//set the default base as 10
baseComboBox.getSelectionModel().selectFirst();
algorithmComboBox.setItems(algorithmOptions);
algorithmComboBox.getSelectionModel().selectFirst();
//fill the tree with sample input
SampleInput treeData = new SampleInput(optionsTree);
optionsTree.getSelectionModel().selectedItemProperty()
.addListener((v, oldValue, newValue) -> {
TreeItem<String> selectedItem = optionsTree.getSelectionModel().getSelectedItem();
if (newValue != null && selectedItem.isLeaf()) {
System.out.println(newValue.getValue());
treeData.updateTextfields(newValue, wordField, summationField);
}
});
}
@FXML
private void solveButtonClicked(ActionEvent event) {
//clear the solutions listView incase it is showing solutions from previous run
solutionsListView.getItems().clear();
//clear the labels from any previous solutions
wordLabel.setText("");
summationLabel.setText("");
//check if input is valid
Validation input = new Validation(wordField.getText(), summationField.getText());
//if the input is valid, prepare the input into a problem that needs solving
if (input.isValid) {
Preperation problem = new Preperation(
wordField.getText(),
summationField.getText(),
operatorComboBox.getSelectionModel().getSelectedItem().toCharArray()[0],
baseComboBox.getSelectionModel().getSelectedItem()
);
//create the table to for smart() to use
problem.makeSmartMatrix();
//check if the problem created from the input is valid
input.postPreperation(problem);
//if the problem is valid, it will be shown in isValid within the input class
if (input.isValid) {
//create a bran object and pass in the problem to solve it. The solutions will be held in the brain
if (algorithmComboBox.getSelectionModel().getSelectedIndex() == 0) {
bruteSolution = new BruteSolve(problem); //find all solutions
showBruteSolutions = new PresentBruteSolution(bruteSolution, solutionsListView);
//change label text to show input
showBruteSolutions.updateStructureLabels(wordLabel, problem, bruteSolution);
showBruteSolutions.updateAnswerLabels(summationLabel, problem, bruteSolution);
timeTaken.setText("Time taken to find all solutions: " + Long.toString(bruteSolution.totalTime) + " milliseconds");
combinationsTried.setText("Number of combinations tried: " + bruteSolution.combinationsTried);
} else if (algorithmComboBox.getSelectionModel().getSelectedIndex() == 1) {
smartSolution = new SmartSolve(problem);//find all solutions
//present the solutions in listView
showSmartSolutions = new PresentSmartSolution(smartSolution, solutionsListView);
//change label text to show input
showSmartSolutions.updateStructureLabels(wordLabel, problem, smartSolution);
showSmartSolutions.updateAnswerLabels(summationLabel, problem, smartSolution);
timeTaken.setText("Time taken to find all solutions: " + Long.toString(smartSolution.totalTime) + " milliseconds");
combinationsTried.setText("Number of combinations tried: " + smartSolution.combinationsTried);
}
}
} else {
input.makeErrorAlert("The input contains invalid characters");
}
}
//onClick event for the Reset button
@FXML
private void resetButtonClicked(ActionEvent event) {
//reset the operator to default operation
operatorComboBox.getSelectionModel().selectFirst();
//reset the base to default base
baseComboBox.getSelectionModel().selectFirst();
//reset the text boxes to default value
wordField.setText("SEND, MORE");
summationField.setText("MONEY");
//clear the solutions listView
solutionsListView.getItems().clear();
}
}