-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientSender.java
More file actions
292 lines (246 loc) · 8.22 KB
/
ClientSender.java
File metadata and controls
292 lines (246 loc) · 8.22 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
281
282
283
284
285
286
287
288
289
290
291
292
import java.io.PrintStream;
import javax.swing.JOptionPane;
// Repeatedly reads recipient's nickname and text from the user in two
// separate lines, sending them to the server (read by ServerReceiver
// thread).
public class ClientSender extends Thread {
private String nickname;
private PrintStream server;
private NoughtsCrossesModel model;
private Opponent opp;
private Respond rsp;
private Answer answerEle;
private Thread threadGame, threadControl;
private ControlPanelModel controlModel;
private boolean count = true;
private String answer;
private NoughtsCrossesGUI nottGUI;
/**
*
* @param nickname - nickname of the logged user
* @param server - the server
* @param model - model for the game - all the methods are available from there
* @param opp - opponent that is chosen
* @param rsp - received respond after a request
* @param threadGame - thread of the game GUI
* @param controlModel - model for the control panel (lobby) - all the methods are available from there
* @param threadControl - thread for the control panel GUI
* @param answerEle - answer
* @param nottGUI - the game GUI
*/
ClientSender(String nickname, PrintStream server, NoughtsCrossesModel model, Opponent opp, Respond rsp,
Thread threadGame, ControlPanelModel controlModel, Thread threadControl, Answer answerEle,
NoughtsCrossesGUI nottGUI) {
this.nickname = nickname;
this.server = server;
this.model = model;
this.opp = opp;
this.rsp = rsp;
this.answerEle = answerEle;
this.threadGame = threadGame;
this.controlModel = controlModel;
this.threadControl = threadControl;
this.nottGUI = nottGUI;
}
public void run() {
// start thread for control panel and the game
threadControl.start();
threadGame.start();
// Tell the server what my nickname is:
server.println(nickname);
// set the nickname as a control panel title
controlModel.setNickname(nickname);
while (true) {
String recipient = "";
/**
* First loop when the respond is not Yes
*/
while (!rsp.getRespond().equals("Yes")) {
// The users initially run in this loop while an opponent is not
// chosen
while (controlModel.getSelectedUser() == null) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
* if the answer to a request is No - prints the opponent to
* the server, prints "No" message, set the opponent to null
* - to prevent exit the loop, sets the answer value to its
* original
*/
if (controlModel.getAnswerNo()) {
server.println(opp.getOpponent());
server.println("No");
opp.setOpponent(null);
controlModel.setAnswerNo(false);
}
count = true;
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
* if the user click "Reset" button - prints the opponent
* and GetUsers message to the server
*/
if (controlModel.isUsersSend()) {
server.println(opp.getOpponent());
server.println("GetUsers");
controlModel.setUsersSend(false);
}
/**
* if the user clicks "Scoreboard" - prints the opponent and
* the Scoreboard message to the server
*/
if (controlModel.isScoreboard()) {
server.println(opp.getOpponent());
server.println("Scoreboard");
controlModel.setScoreboard(false);
}
}
/**
* if there is a selected user or the answer is Yes
*/
try {
Thread.sleep(100);
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
answer = answerEle.getAnswer(); // saves the answer
if (controlModel.getSelectedUser() != null
|| (answerEle.getAnswer() != null && answerEle.getAnswer().equals("Yes"))) {
recipient = controlModel.getSelectedUser(); // saves the
// selected user
/**
* first check if the answer is Yes - the game starts
*/
if (answer != null && answer.equals("Yes")) {
count = true; // this variable prevents from infinity
// loops
server.println(opp.getOpponent()); // sends the opponent
// to the server
server.println("Yes"); // sends Yes to the server
model.setIsMyTurn(true); // show that it is this user's
// turn - used to disable
// the buttons when needed
nottGUI.setVisibleGUI(true); // set the game GUI to
// visible because
// initially it is false
model.setInVisible(true); // indicates that the user is
// in game
// loop in this while when the user is in game
while (model.isInVisible()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (!model.isPlaying()) {
System.out.println("Second loop");
String newOpponent = opp.getAdditionalOpponent();
server.println(newOpponent);
server.println("No");
model.setPlaying(true);
}
/**
* if the game ended and there is a winner - prints
* the opponent to the server and sends Increase to
* the server which helps for increasing the score
*/
if (model.isUserWinner()) {
model.setUserWinner(false);
server.println(opp.getOpponent());
server.println("Increase");
JOptionPane.showMessageDialog(null, "Congratulations! " + nickname + " won!");
}
/**
* if a move is made sends the move to the server
*/
if (model.getTurn() != 0) {
model.setIsMyTurn(false);
String cellActiveInt = String.valueOf(model.getTurn());
server.println(opp.getOpponent());
server.println(cellActiveInt);
model.setTurn(0);
}
}
// if the user is not playing anymore the value of these
// variables are set to their initial ones
answer.equals(null);
controlModel.setSelectedUser(null);
answerEle.setAnswer(null);
nottGUI.setVisibleGUI(false);
rsp.setRespond("");
server.println(opp.getOpponent());
server.println("Exit");
} else if (count) { // if the answer is not Yes then the
// server receives the selected opponent
server.println(recipient);
server.println("NotYes");
count = false;
}
}
}
/**
* When the respond is Yes the user loops in the below while, but
* first sets his turn to false, the visibility of the game GUI to
* true and indicates that he is playing
*/
model.setIsMyTurn(false);
nottGUI.setVisibleGUI(true);
model.setInVisible(true);
while (rsp.getRespond().equals("Yes") && model.isInVisible()) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (!model.isPlaying()) {
System.out.println("Second loop");
String newOpponent = opp.getAdditionalOpponent();
server.println(newOpponent);
server.println("No");
model.setPlaying(true);
}
/**
* if the game ended and there is a winner - prints the opponent
* to the server and sends Increase to the server which helps
* for increasing the score
*/
if (model.isUserWinner()) {
System.out.println("Second");
model.setUserWinner(false);
server.println(opp.getOpponent());
server.println("Increase");
JOptionPane.showMessageDialog(null, "Congratulations! " + nickname + " won!");
}
/**
* if a move is made sends the move to the server
*/
if (model.getTurn() != 0) {
model.setIsMyTurn(false);
String cellActiveInt = String.valueOf(model.getTurn());
server.println(opp.getOpponent());
server.println(cellActiveInt);
model.setTurn(0);
}
}
// if the user is not playing anymore the value of these variables
// are set to their initial ones
controlModel.setSelectedUser(null);
answerEle.setAnswer(null);
rsp.setRespond("");
nottGUI.setVisibleGUI(false);
server.println(opp.getOpponent());
server.println("Exit");
}
}
}