-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConnecFourMain.java
More file actions
280 lines (228 loc) · 9.88 KB
/
Copy pathConnecFourMain.java
File metadata and controls
280 lines (228 loc) · 9.88 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.Pane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.scene.input.MouseEvent;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.shape.Shape;
import java.awt.Dimension;
import java.awt.Toolkit;
public class ConnecFourMain extends Application {
public static void main(String[] args) {
launch();
}
@Override
public void start(Stage stage) {
// Set tile
stage.setTitle("Connect Four");
Player user = new RealPlayer("Red");
Player user2 = new RealPlayer("Yellow");
// Dimension of screen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
// Intro Scene - label
Label intro_label = new Label("Welcome to Connect Four");
intro_label.setFont(new Font("Arial", 30));
intro_label.relocate((width / 2 - 180), 150);
// Intro Scene - button
Button one_player_button = new Button("1 Player");
one_player_button.setFont(new Font("Times New Roman", 20));
one_player_button.setMinHeight(30);
one_player_button.setMaxHeight(40);
one_player_button.setMinWidth(200);
one_player_button.setMaxWidth(400);
one_player_button.setPrefHeight(250);
one_player_button.setPrefWidth(35);
Button two_players_button = new Button("2 Players");
two_players_button.setFont(new Font("Times New Roman", 20));
two_players_button.setMinHeight(30);
two_players_button.setMaxHeight(40);
two_players_button.setMinWidth(200);
two_players_button.setMaxWidth(400);
two_players_button.setPrefHeight(250);
two_players_button.setPrefWidth(35);
// Intro Scene - one player settings
Label one_player_label = new Label("Choose your color");
one_player_label.setFont(new Font("Arial", 20));
RadioButton yellow_button = new RadioButton("Yellow Coin");
yellow_button.setFont(new Font("Times New Roman", 15));
RadioButton red_button = new RadioButton("Red Coin");
red_button.setFont(new Font("Times New Roman", 15));
ToggleGroup coin_buttons = new ToggleGroup();
yellow_button.setToggleGroup(coin_buttons);
red_button.setToggleGroup(coin_buttons);
VBox emptyOne = new VBox();
VBox coin_options = new VBox(5, one_player_label, yellow_button, red_button);
VBox one_player_settings = new VBox(one_player_button, coin_options);
// Intro Scene - two players settings
VBox two_players_settings = new VBox(two_players_button);
// Intro Scene - AI and play
Label ai_label = new Label("Choose AI level");
ai_label.setFont(new Font("Arial", 20));
RadioButton ai_button_one = new RadioButton("Level 1");
ai_button_one.setFont(new Font("Times New Roman", 15));
RadioButton ai_button_two = new RadioButton("Level 2");
ai_button_two.setFont(new Font("Times New Roman", 15));
ToggleGroup ai_buttons = new ToggleGroup();
ai_button_one.setToggleGroup(ai_buttons);
ai_button_two.setToggleGroup(ai_buttons);
VBox ai_setting = new VBox(5, ai_label, ai_button_one, ai_button_two);
// Intro Scene - play button
Button play_button = new Button("Play");
play_button.setFont(new Font("Times New Roman", 20));
play_button.setMinHeight(30);
play_button.setMaxHeight(40);
play_button.setMinWidth(200);
play_button.setMaxWidth(400);
play_button.setPrefHeight(250);
play_button.setPrefWidth(35);
// Intro Scene - all settings combined
HBox settings = new HBox(20, one_player_settings, two_players_settings);
settings.relocate((width / 2 - 200), (height / 2 - 100));
// Button actions
two_players_button.setOnAction(e -> {
one_player_settings.getChildren().setAll(one_player_button, emptyOne);
settings.getChildren().setAll(one_player_settings, two_players_settings);
ai_setting.getChildren().setAll(ai_label, ai_button_one, ai_button_two);
yellow_button.setSelected(false);
red_button.setSelected(false);
ai_button_one.setSelected(false);
ai_button_two.setSelected(false);
});
one_player_button.setOnAction(e -> {
one_player_settings.getChildren().setAll(one_player_button, coin_options);
settings.getChildren().setAll(one_player_settings, two_players_settings);
ai_setting.getChildren().setAll(ai_label, ai_button_one, ai_button_two);
yellow_button.setSelected(false);
red_button.setSelected(false);
ai_button_one.setSelected(false);
ai_button_two.setSelected(false);
});
yellow_button.setOnAction(e -> {
// instantiate player with yellow coin
settings.getChildren().setAll(one_player_settings, two_players_settings, ai_setting);
user.setColor("Yellow");
});
red_button.setOnAction(e -> {
// instantiate player with red coin
settings.getChildren().setAll(one_player_settings, two_players_settings, ai_setting);
user.setColor("Red");
});
ai_button_one.setOnAction(e -> {
// instantiate simple ai
ai_setting.getChildren().setAll(ai_label, ai_button_one, ai_button_two, play_button);
if (user.getColor().equals("Red")) {
Player ai = new FoolAI("Yellow");
} else {
Player ai = new FoolAI("Red");
}
});
ai_button_two.setOnAction(e -> {
// instantiate complex ai
ai_setting.getChildren().setAll(ai_label, ai_button_one, ai_button_two, play_button);
if (user.getColor().equals("Yellow")) {
Player ai = new FoolAI("Red");
} else {
Player ai = new FoolAI("Yellow");
}
});
play_button.setOnAction(e -> {
// Switch to game scene
game(stage);
});
Pane intro_pane = new Pane(intro_label, settings);
Scene intro_scene = new Scene(intro_pane, width, height);
stage.setScene(intro_scene);
stage.setResizable(true);
stage.show();
}
public void game(Stage secondary_stage) {
Board connectFourBoard = new Board();
// creating the circle objects
Circle[][] circle = new Circle[8][8];
// storing the gameboard in an Image variable
Image image = new Image("file:Connect4GameBoard.png");
ImageView imageView = new ImageView(image);
// setting the width and height of the image
imageView.setFitWidth(600);
imageView.setFitHeight(600);
imageView.setPreserveRatio(true);
// SETTNG THE COORDINATES FOR THE FIRST CIRCLE
float xCoord = 70.0f;
float yCoord = 38.0f;
float radius = 30.0f;
// for loop for placing the circles on the board
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
// initializing circle objects
circle[i][j] = new Circle();
// Circle
circle[i][j].setCenterX((float) xCoord);
circle[i][j].setCenterY((float) yCoord);
circle[i][j].setRadius((float) radius);
// Setting color to the circle
circle[i][j].setFill(Color.WHITE);
// Setting the stroke width
circle[i][j].setStrokeWidth(3);
// Setting color to the stroke
circle[i][j].setStroke(Color.DARKBLUE);
// CHANGING THE COLOR OF THE CIRCLE WHEN CLICKED
circle[i][j].setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
((Shape) e.getSource()).setFill(Color.BLUE);
}
});
xCoord += 70;
// checking if i is the 8th item in the row and then placing the circles in the
// next row
// removing the "drifting" of the circles away from the game board
if (j == 7) {
yCoord += 65;
xCoord = 70.0f;
}
if (i == 1 && j == 0) {
yCoord += 5;
}
if (i == 2 && j == 0) {
yCoord += 10;
}
if (i == 4 && j == 0) {
yCoord += 10;
}
if (i == 6 && j == 0) {
yCoord += 5;
}
}
} // end of for loop
// Create an gridPane container and add the Labels.
Group labels = new Group();
labels.getChildren().add(imageView);
// adding each one of the circle objects
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
labels.getChildren().add(circle[i][j]);
}
}
// Setting the back ground color
labels.setStyle("-fx-background-color: BLUE;");
// Create a Scene with the HBox as its root node.
Scene game_scene = new Scene(labels, 600, 600);
secondary_stage.setScene(game_scene);
secondary_stage.show();
}
}