-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameRound.java
199 lines (168 loc) · 8.08 KB
/
GameRound.java
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
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundPosition;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundSize;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.sql.Connection;
public class GameRound extends Application{
public boolean fiftyFiftyAvailable=true;
public boolean phoneFriendAvailable=true;
public boolean askAudienceAvailable=true;
public static Connection connection;
public static int questionId;
public static String questionText;
private int score = 0;
private int correctAnswer;
GameRound(Connection connection){
this.connection = connection;
}
@Override
public void start(Stage primaryStage) {
Questions questionsInstance = new Questions();
// Set up the root layout
BorderPane root = new BorderPane();
// Set up the background
BackgroundImage backgroundImage = new BackgroundImage(
new javafx.scene.image.Image("purple_background.jpg", 1000, 600, false, true),
BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT,
BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT
);
root.setBackground(new Background(backgroundImage));
// Set up the text label
Label label = new Label("Game Round");
label.setStyle("-fx-font-size: 24; -fx-text-fill: black;");
Label scoreLabel = new Label("Score: " + score);
scoreLabel.setStyle("-fx-font-size: 18; -fx-text-fill: black;");
// Set up the layout for the labels
VBox labelScoreBox = new VBox(5);
labelScoreBox.getChildren().addAll(scoreLabel);
labelScoreBox.setAlignment(Pos.TOP_RIGHT);
// Set up the buttons
Button button1 = new Button();
Button button2 = new Button();
Button button3 = new Button();
Button button4 = new Button();
// Assign random answers to each button
questionsInstance.displayRandomQuestionAndAnswers(button1, button2, button3, button4, label);
// Set up the layout for buttons on the left (button1 and button2)
VBox leftButtons = new VBox(10);
leftButtons.getChildren().addAll(button1, button2);
leftButtons.setAlignment(Pos.CENTER);
// Set up the layout for buttons on the right (button3 and button4)
VBox rightButtons = new VBox(10);
rightButtons.getChildren().addAll(button3, button4);
rightButtons.setAlignment(Pos.CENTER);
// Add elements to the top-left corner of the root layout
HBox topLeftButtons = new HBox(10);
Button fiftyFiftyButton = new Button("FiftyFifty");
Button phoneFriendButton = new Button("PhoneFriend");
Button askAudienceButton = new Button("AskAudience");
topLeftButtons.getChildren().addAll(fiftyFiftyButton, phoneFriendButton, askAudienceButton);
root.setTop(topLeftButtons);
// Create a VBox for the label and set its alignment
VBox labelBox = new VBox(label);
labelBox.setAlignment(Pos.CENTER);
// Create an HBox for the label and answer buttons with spacing
HBox centerBox = new HBox(20);
centerBox.getChildren().addAll(leftButtons, labelBox, labelScoreBox, rightButtons);
centerBox.setAlignment(Pos.CENTER);
// Add elements to the root layout
root.setCenter(centerBox);
// Create the scene
Scene scene = new Scene(root, 1000, 600);
// Set up the stage
primaryStage.setTitle("Game Round");
primaryStage.setScene(scene);
primaryStage.show();
// Set up the correct answer
correctAnswer = questionsInstance.getCorrectQuestionNumber();
questionText = questionsInstance.getQuestionText();
System.out.println(correctAnswer);
// Set up button click actions
button1.setOnAction(event -> checkAnswer(button1.getText(), button1, button2, button3, button4, label, scoreLabel));
button2.setOnAction(event -> checkAnswer(button2.getText(), button1, button2, button3, button4, label, scoreLabel));
button3.setOnAction(event -> checkAnswer(button3.getText(), button1, button2, button3, button4, label, scoreLabel));
button4.setOnAction(event -> checkAnswer(button4.getText(), button1, button2, button3, button4, label, scoreLabel));
fiftyFiftyButton.setOnAction(event -> showFiftyFiftyPopup(questionsInstance.getQuestionId()));
fiftyFiftyButton.setOnAction(event -> {
if (fiftyFiftyAvailable) {
showFiftyFiftyPopup(questionsInstance.getQuestionId());
fiftyFiftyButton.setDisable(true);
fiftyFiftyButton.setVisible(false);
}
});
phoneFriendButton.setOnAction(event -> showPhoneFriendPopup(questionsInstance.getQuestionId(),questionsInstance.getQuestionText()));
phoneFriendButton.setOnAction(event -> {
if (phoneFriendAvailable) {
showPhoneFriendPopup(questionsInstance.getQuestionId(),questionsInstance.getQuestionText());
phoneFriendButton.setDisable(true);
phoneFriendButton.setVisible(false);
}
});
askAudienceButton.setOnAction(event -> showAskAudiencePopup(questionsInstance.getQuestionId()));
askAudienceButton.setOnAction(event -> {
if (askAudienceAvailable) {
showAskAudiencePopup(questionsInstance.getQuestionId());
askAudienceButton.setDisable(true);
askAudienceButton.setVisible(false);
}
});
}
private void showPhoneFriendPopup(int questionId,String questionText) {
System.out.println(questionText);
PhoneFriend phoneFriendInstance = new PhoneFriend(questionId,correctAnswer,questionText);
phoneFriendAvailable = false;
phoneFriendInstance.use();
}
private void showAskAudiencePopup(int questionId) {
AskAudience askAudienceInstance = new AskAudience(connection, questionId);
String audienceResult = askAudienceInstance.use();
askAudienceAvailable = false;
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Ask the Audience Result");
alert.setHeaderText(null);
alert.setContentText(audienceResult);
alert.showAndWait();
}
private void showFiftyFiftyPopup(int questionId) {
fiftyFiftyAvailable = false;
FiftyFifty fiftyFiftyInstance = new FiftyFifty(connection,questionId);
fiftyFiftyInstance.use();
}
private void checkAnswer(String selectedAnswer, Button button1, Button button2, Button button3, Button button4,Label label,Label scoreLabel) {
int selectedAnswerNumber = Integer.parseInt(selectedAnswer.replaceAll("[^0-9]", ""));
if (selectedAnswerNumber == correctAnswer) {
showPopup("Correct!");
new Thread(() -> {
score++;
Platform.runLater(() -> scoreLabel.setText("Score: " + score));
}).start();
} else {
showPopup("Incorrect. You Lost!");
Platform.exit();
}
Questions questionsInstance = new Questions();
questionsInstance.displayRandomQuestionAndAnswers(button1, button2, button3, button4,label);
correctAnswer = questionsInstance.getCorrectQuestionNumber();
System.out.println(correctAnswer);
}
private void showPopup(String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Result");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
}