-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessMenuController.java
More file actions
226 lines (170 loc) · 7.87 KB
/
Copy pathChessMenuController.java
File metadata and controls
226 lines (170 loc) · 7.87 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
package edu.unlv.mis768.finalproject;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextInputDialog;
import javafx.stage.Stage;
//Author: Sergio Torres and William Brasic.
//The purpose of this class is to control database operations in a visual display and launch the game.
public class ChessMenuController {
@FXML
private Button deleteHistoryButton;
@FXML
private Button loadHistoryButton;
@FXML
private Button newGameButton;
@FXML
private Label promptLabel;
@FXML
private Button reviseHistoryButton;
@FXML
private TextArea resultArea;
/**
* Event handler for the "New Game" Button
* It allows the players to be set for the match and pass players to the next scene.
* @throws IOException
*/
public void changeSceneToBoard(ActionEvent e) throws IOException {
//Generate dialog box to collect match info in local variables.
TextInputDialog playerOne = new TextInputDialog();
TextInputDialog playerTwo = new TextInputDialog();
TextInputDialog matchNum = new TextInputDialog();
//Set header for dialogs and Show the dialog boxes in order.
playerOne.setHeaderText("Enter first player name.");
playerTwo.setHeaderText("Enter second player name.");
matchNum.setHeaderText("Enter a reference ID");
playerOne.showAndWait();
playerTwo.showAndWait();
matchNum.showAndWait();
//Retrieve inputs from dialogs to string values.
String firstName = playerOne.getEditor().getText();
String secondName = playerTwo.getEditor().getText();
String id= matchNum.getEditor().getText();
//Set strings to player objects.
Player contestantOne = new Player(firstName);
Player contestantTwo = new Player(secondName);
//Create ChessResult Object to add challenge to database using currentGame variable.
ChessResult currentGame = new ChessResult(id, contestantOne, contestantTwo);
//Initialize instance of DAO implementation and assign to variable.
ChessResultsDAOImplementation create = new ChessResultsDAOImplementation();
//Call createChessResult function to update database.
if (create.createChessResult(currentGame)) {
//If flag from method is true, set resultArea text to confirm.
resultArea.setText("Let the game begin!");
}
else {
//If false tell the user the match needs to be remade.
resultArea.setText("Your match is missing information.Try again.");
}
// Instantiate the FXMLLoader object for loading the UI
FXMLLoader loader = new FXMLLoader();
// specify the file location for the FXML file for the next window
loader.setLocation(getClass().getResource("ChessGame.fxml"));
// load the UI for the next window
Parent parent = loader.load();
// set the scene
Scene scene = new Scene(parent);
// access the controller class for the next window via the FXML loader
ChessGameController controller = loader.getController();
//Initialize ChessGame with contestants using ChessGame variable freshMatch.
ChessGame freshMatch = new ChessGame();
//Start game using contestant player variables.
freshMatch.startGame(contestantOne, contestantTwo);
// call the method in the controller class for the next window
// so that the string can be passed
controller.initData(freshMatch);
// get the current stage, using the ActionEvent object
Stage stage = (Stage) (((Node) e.getSource()).getScene().getWindow());
//Prevent stage from being resized.
stage.setResizable(false);
// change the title
stage.setTitle("Active Chess Match");
// set the new scene to the stage
stage.setScene(scene);
// show the stage
stage.show();
}
/**This method serves as an event listener for the loadHistoryButton and calls the database to load previous matches.
*
*/
public void loadHistoryButtonListener(ActionEvent e) {
//Generate dialog box to collect player names in local variables.
TextInputDialog playerOne = new TextInputDialog();
//Set header for dialog and Show the dialog box for input.
playerOne.setHeaderText("Player to search.");
playerOne.showAndWait();
//Retrieve input from dialog to string values.
String firstName = playerOne.getEditor().getText();
//Set string to player object.
Player contestant = new Player(firstName);
//Initialize instance of DAO implementation and assign to variable.
ChessResultsDAOImplementation search = new ChessResultsDAOImplementation();
//Call getTotalGamesPlayed function to search database of games played and set to text area.
resultArea.setText(search.getTotalGamesPlayed(contestant));
}
/**This method serves as an event listener for the deleteHistoryButton and calls the database to delete a match by number.
*/
public void deleteHistoryButtonListener(ActionEvent e) {
//Generate dialog box to collect match number in local variable.
TextInputDialog match = new TextInputDialog();
//Set header for dialog and Show the dialog box for input.
match.setHeaderText("Enter Match Number.");
match.showAndWait();
//Retrieve input from dialog to string values.
String matchToDelete = match.getEditor().getText();
//Set string to new ChessResult object deleteResult
ChessResult deleteResult = new ChessResult();
//Call setMatchID and assign to deleteResult.
deleteResult.setMatchID(matchToDelete);
//Initialize instance of DAO implementation and assign to variable.
ChessResultsDAOImplementation remove = new ChessResultsDAOImplementation();
//Call deleteChessResult function to search database of games played and set to table view.
if (remove.deleteChessResult(deleteResult)) {
//If flag from method is true, set resultArea text to confirm.
resultArea.setText("The match has been successfully removed");
}
else {
//If false tell the user the match could not be found.
resultArea.setText("The match could not be found.");
}
}
/**This method serves as an event listener for the reviseHistoryButton and calls the database to change a record.
*/
public void reviseHistoryButtonListener(ActionEvent e) {
//Generate dialog boxes to collect players and match number in local variables.
TextInputDialog player1 = new TextInputDialog();
TextInputDialog player2 = new TextInputDialog();
TextInputDialog match = new TextInputDialog();
//Set header for dialog and Show the dialog box for input.
match.setHeaderText("Enter Match # to Update.");
match.showAndWait();
player1.setHeaderText("Who was player 1?");
player1.showAndWait();
player2.setHeaderText("Who was player 2?");
player2.showAndWait();
//Retrieve input from dialogs to string values.
Player revisedFirst = new Player(player1.getEditor().getText());
Player revisedSecond = new Player(player2.getEditor().getText());
String matchRecord = match.getEditor().getText();
//Set local variables to new ChessResult object updateResult
ChessResult updateResult = new ChessResult(matchRecord,revisedFirst,revisedSecond);
//Initialize instance of DAO implementation and assign to variable.
ChessResultsDAOImplementation update = new ChessResultsDAOImplementation();
//Call updateChessResult function to search database for record to update.
if (update.updateChessResult(updateResult)) {
//If flag from method is true, set resultArea text to confirm change.
resultArea.setText("The match has been successfully updated");
}
else {
//If false tell the user the match could not be found.
resultArea.setText("The match could not be found.");
}
}
}