-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
142 lines (115 loc) · 4.19 KB
/
Main.java
File metadata and controls
142 lines (115 loc) · 4.19 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
package pins;
import java.util.ArrayList;
import java.util.HashMap;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.layout.StackPane;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.GridPane;
import javafx.scene.control.TextField;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.TableColumn;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// Sets up variables
Stage window;
Scene scene;
Button button;
Pins guess = new Pins();
// Pins originalPins = new Pins();
ArrayList<Pins> pastGuesses = new ArrayList<Pins>();
ArrayList<HBox> pastGuessHBox = new ArrayList<HBox>();
ArrayList<Circle[]> pastGuessCircles = new ArrayList<Circle[]>();
// Creates the window
primaryStage.setTitle("Mastermind Game");
window = primaryStage;
final Pins originalPins = Pins.generatePins();// But I just need it to
// do this once!!
// Creates the drop down menus
ChoiceBox<String>[] colourMenus = new ChoiceBox[Pins.getNumPins()];
for (int i = 0; i < Pins.getNumPins(); i++) {
colourMenus[i] = new ChoiceBox<>();
for (int j = 0; j < Pins.getPinColours().length; j++) {
colourMenus[i].getItems().add(Pins.getPinColours()[j]);
}
colourMenus[i].setValue(Pins.getPinColours()[0]);
}
// Creates the enter button
button = new Button("Enter Guess");
// Attaches the menus & button to the top section
HBox topMenu = new HBox(10);
topMenu.setPadding(new Insets(20, 20, 20, 20));
for (int i = 0; i < Pins.getNumPins(); i++) {
topMenu.getChildren().add(colourMenus[i]);
}
topMenu.getChildren().addAll(button);
// Creates middle section
VBox guessDisplay = new VBox(10);
// Attaches the top menu & middle section to the screen
BorderPane borderPane = new BorderPane();
borderPane.setTop(topMenu);
borderPane.setCenter(guessDisplay);
// Creates the button's response to being pressed
button.setOnAction(e -> {
pastGuesses.add(guess);
respondToGuess(guess, originalPins, colourMenus, guessDisplay);
});
// Shows the window
scene = new Scene(borderPane, 500, 500);
window.setScene(scene);
window.show();
}
public void respondToGuess(Pins guess, Pins originalPins, ChoiceBox<String>[] colourMenus, VBox guessDisplay) {
// Gets the user's guess & adds to the list of past guesses
for (int i = 0; i < Pins.getNumPins(); i++) {
guess.pins[i] = colourMenus[i].getValue();
}
// Creates the circle output and adds to the screen
HBox hbox = new HBox(10);
Circle[] outputCircles = new Circle[2 * Pins.getNumPins()];
// Circles for the guess
for (int i = 0; i < Pins.getNumPins(); i++) {
outputCircles[i] = new Circle(20.0, Pins.colourOfPins.get(guess.pins[i]));
}
// Circles for the result
int[] a = new int[3];
a = guess.evaluatePins(originalPins);
for (int i = Pins.getNumPins(); i < Pins.getNumPins() + a[0]; i++) {
outputCircles[i] = new Circle(15.0, Color.GREEN);
}
for (int i = Pins.getNumPins() + a[0]; i < Pins.getNumPins() + a[0] + a[1]; i++) {
outputCircles[i] = new Circle(15.0, Color.YELLOW);
}
for (int i = Pins.getNumPins() + a[0] + a[1]; i < Pins.getNumPins() + a[0] + a[1] + a[2]; i++) {
outputCircles[i] = new Circle(15.0, Color.RED);
}
for (int i = 0; i < 2 * Pins.getNumPins(); i++) {
hbox.getChildren().add(outputCircles[i]);
}
guessDisplay.getChildren().add(hbox);
}
}