-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectingUser.java
522 lines (430 loc) · 15.7 KB
/
ConnectingUser.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.net.*;
public class ConnectingUser {
//adress information
private String serverAddress;
//readers and writers for the server.
private PrintWriter out;
private BufferedReader in;
private Socket socket;
//board info
private Board board;
private int playerNum;
private int numSeeds;
private int numPits;
private int time;
private boolean normalConfig = true;
GUI guiBoard;
Timing.Timer timerObj = new Timing.Timer((int)Double.POSITIVE_INFINITY);
//default constructor for client
public ConnectingUser() throws IOException {
serverAddress = "127.0.0.1";
numPits = 6;
playerNum = -1;
time = 0;
//initialize pits and stores to 0; 6 is arbitrary
}
//read an integer in from the user
public String getUserMove() {
String moves = "";
//if there's already a winner, or time is up, just return
if (this.board.checkWinner() || timerObj.timeIsUp()) {
return moves;
}
Board boardCopy = new Board(this.board);
while(!guiBoard.moveMade && !timerObj.timeIsUp()){
//pause for a moment; linux server is slow and can't handle busy waiting
try {
Thread.sleep(1000);
}
catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
if (board.checkWinner()) {
break;
}
}
if (timerObj.timeIsUp()) {
return moves;
}
int index = guiBoard.returnMove();
int valid = -1;
while ((valid != 0) && (board.checkWinner() == false) && !timerObj.timeIsUp()) {
boardCopy = new Board(this.board);
valid = boardCopy.makeMove(this.playerNum, index);
if (valid != -1) {
//make move on this user's board
this.board = boardCopy;
//add to list of moves we will return from this function
moves = moves + Integer.toString(index) + " ";
//print new board
this.board.printBoardPlayer(playerNum);
}
else if (valid == -1) {
System.out.println("Invalid move! Try again:");
guiBoard.setMyMove();
while(!guiBoard.moveMade && !timerObj.timeIsUp()){
//pause for a moment; linux server is slow and can't handle busy waiting
try {
Thread.sleep(1000);
}
catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
if (board.checkWinner()) {
break;
}
}
index = guiBoard.returnMove();
}
if ((valid == 1) && (!board.checkWinner())) {
System.out.println("Go again:");
guiBoard.setMyMove();
this.board.printBoardPlayer(playerNum);
//get the next move
while(!guiBoard.moveMade && !timerObj.timeIsUp()){
//pause for a moment; linux server is slow and can't handle busy waiting
try {
Thread.sleep(1000);
}
catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
if (board.checkWinner()) {
break;
}
}
index = guiBoard.returnMove();
}
if (board.checkWinner()) {
break;
}
}
//if there is already a winner, just return some random move
if (moves == "") {
moves = "1 ";
}
return moves;
}
private void sendUserMoves() throws IOException {
//initial info exchange
socket = new Socket(this.serverAddress, 56789);
//recieve welcome from Mancala
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String response = in.readLine();
ArrayList<String> responseArr = splitOnSpaces(response);
//Check that we recieved a welcome
if (responseArr.get(0).charAt(0) != 'W') {
System.out.println("Was expecting WELCOME, got " + responseArr.get(0));
socket.close();
System.exit(0);
}
else {
System.out.println("WELOME");
}
//read in board configuration
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
response = in.readLine();
responseArr = splitOnSpaces(response);
//check that this is an INFO command
if (responseArr.get(0).charAt(0) != 'I') {
System.out.println("Was expecting INFO, got " + response);
socket.close();
System.exit(0);
}
//load in number of pits
numPits = Integer.parseInt(responseArr.get(1));
//load in playerNum
if (responseArr.get(4).charAt(0) == 'F') {
playerNum = 1;
}
else if (responseArr.get(4).charAt(0) == 'S') {
playerNum = 2;
}
else {
System.out.println("Was expecting player number, got " + responseArr.get(4));
System.exit(0);
}
//load in number of seeds per pit
int numSeeds = Integer.parseInt(responseArr.get(2));
//load in the time
this.time = Integer.parseInt(responseArr.get(3));
//this gets the distribution of seeds according to what was passed in the info statement
//it then goes on the put then in the distribution array
ArrayList<Integer> distribution = new ArrayList<>();
if(responseArr.get(5).charAt(0) == 'R'){
for(int i = 6; i < responseArr.size();i++) {
distribution.add(Integer.parseInt(responseArr.get(i)));
}
//tells the client that this is no longer a normal configuration but a random one
normalConfig = false;
}
//normal configuration is run if the condition is true
if(normalConfig == true) {
guiBoard = new GUI(playerNum, numPits, numSeeds);
//send ready
out = new PrintWriter(socket.getOutputStream(), true);
out.println("READY");
board = new Board(numSeeds, numPits);
}
//otherwise a random configuration is set in place
else{
out = new PrintWriter(socket.getOutputStream(),true);
out.println("READY");
board = new Board(distribution,numPits);
guiBoard = new GUI(board, playerNum,numPits);
}
Thread timeThread;
try {
//recieve a move first
if (playerNum == 2) {
System.out.println("Getting opponent move from Mancala...");
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
response = in.readLine();
System.out.println("Opponent's move: " + response);
//start the timer
timerObj = new Timing.Timer(time);
timeThread = new Thread(timerObj);
timeThread.start();
parseInput(response);
//acknowledge we got the other player's move
out = new PrintWriter(socket.getOutputStream(), true);
out.println("OK");
//make the moves on the board
ArrayList<Integer> opponentMoves = new ArrayList<Integer>(parseForMoves(response));
for (int i = 0; i < opponentMoves.size(); i++) {
this.board.makeMove(otherPlayer(this.getPlayerNum()), opponentMoves.get(i));
this.guiBoard.opponentMove(opponentMoves.get(i));
}
this.guiBoard.setMyMove();
this.board.printBoardPlayer(playerNum);
}
int counter = 0;
while (true) {
//send a move
//pause a moment to let server prepare to recieve move
try {
Thread.sleep(1000);
}
catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println("Sending a move...");
//In the first iteration, if this is player 1 then the timer has not been started yet
if ((counter == 0) && (playerNum == 1)) {
//start the timer
timerObj = new Timing.Timer(time);
timeThread = new Thread(timerObj);
timeThread.start();
}
//pie rule
String move = "-1";
if ((counter == 0) && (playerNum == 2)) {
int choice = JOptionPane.showConfirmDialog(null,"Would you like to make a pie move?", "Pie move",JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
//make the pie move
move = "P";
this.board.pieMove();
this.guiBoard.board = new Board(board);
this.guiBoard.updateGame();
System.out.println("Board after pie move:");
this.board.printBoardPlayer(playerNum);
}
else {
//don't make the pie move
move = this.getUserMove();
}
}
else {
//don't make the pie move
move = this.getUserMove();
}
//stop the timer
boolean overTime = timerObj.timeIsUp();
timerObj.signalStopTimer();
//send any move if we are over time
if (overTime == true) {
//ensure server timer runs out as well
try {
Thread.sleep(5000);
}
catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
move = "0";
}
out = new PrintWriter(socket.getOutputStream(), true);
System.out.println("Sending move " + move + " to server");
out.println(move);
//verify our move from Mancala
System.out.println("Player " + playerNum + " getting verification from server...");
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
response = in.readLine();
System.out.println("Player " + playerNum + " verification from server recieved.");
parseInput(response);
System.out.println("Getting other player's move...");
//get other player's move
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
response = in.readLine();
//start the timer
timerObj = new Timing.Timer(time);
timeThread = new Thread(timerObj);
timeThread.start();
parseInput(response);
//send acknowledgement
out = new PrintWriter(socket.getOutputStream(), true);
out.println("OK");
//verify the moves
//pie move
if (response.startsWith("P") && (counter == 0) && (playerNum == 1)) {
System.out.println("Making pie move...");
this.board.pieMove();
this.guiBoard.board = new Board(board);
this.guiBoard.updateGame();
this.board.printBoardPlayer(playerNum);
System.out.println("Pie move complete");
}
System.out.println("Player " + playerNum + " making server's moves...");
//make other player's moves on the board
ArrayList<Integer> opponentMoves = new ArrayList<Integer>(parseForMoves(response));
for (int i = 0; i < opponentMoves.size(); i++) {
this.board.makeMove(otherPlayer(this.getPlayerNum()), opponentMoves.get(i));
this.guiBoard.opponentMove(opponentMoves.get(i));
}
System.out.println("Server's moves complete");
this.guiBoard.setMyMove();
this.board.printBoardPlayer(playerNum);
counter++;
}
}
finally {
socket.close();
System.out.println("Game has been ended. Exiting");
System.exit(0);
}
}
public void parseInput(String response) throws IOException {
if (response.startsWith("OK")) {
System.out.println("Valid move");
}
else if (response.startsWith("ILLEGAL")) { //ILLEGAL
//get other player's move or winner/loser message
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
response = in.readLine();
//make other player's moves on the board
ArrayList<Integer> opponentMoves = new ArrayList<Integer>(parseForMoves(response));
for (int i = 0; i < opponentMoves.size(); i++) {
this.board.makeMove(otherPlayer(this.getPlayerNum()), opponentMoves.get(i));
this.guiBoard.opponentMove(opponentMoves.get(i));
}
this.guiBoard.setMyMove();
if (response.startsWith("WINNER")) {
System.out.println("Other player sent invalid move. You win! Thanks for playing!");
}
else {
System.out.println("You sent an invalid move. You lose! Thanks for playing!");
}
//Print the final board
this.board.printBoardPlayer(playerNum);
socket.close();
System.exit(0);
}
else if (response.startsWith("TIME")) { //TIME
//get other player's move
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
response = in.readLine();
//make other player's moves on the board
ArrayList<Integer> opponentMoves = new ArrayList<Integer>(parseForMoves(response));
for (int i = 0; i < opponentMoves.size(); i++) {
this.board.makeMove(otherPlayer(this.getPlayerNum()), opponentMoves.get(i));
this.guiBoard.opponentMove(opponentMoves.get(i));
}
this.guiBoard.setMyMove();
if (response.startsWith("WINNER")) {
System.out.println("Other player went over time. You win! Thanks for playing!");
}
else {
System.out.println("You went over time. You lose! Thanks for playing!");
}
this.board.printBoardPlayer(playerNum);
socket.close();
System.exit(0);
}
else if (response.startsWith("WINNER")) { //WINNER
this.board.printBoardPlayer(playerNum);
System.out.println("You win! Thanks for playing");
//print final board
this.board.winningBoard();
this.board.printBoardPlayer(playerNum);
this.guiBoard.updateGame();
socket.close();
System.exit(0);
}
else if (response.startsWith("LOSER")) { //LOSER
this.board.printBoardPlayer(playerNum);
System.out.println("You lose! Thanks for playing");
//print final board
this.board.winningBoard();
this.board.printBoardPlayer(playerNum);
this.guiBoard.updateGame();
socket.close();
System.exit(0);
}
else if (response.startsWith("TIE")) { //TIE
this.board.printBoardPlayer(playerNum);
System.out.println("Tie! Thanks for playing");
//print final board
this.board.winningBoard();
this.board.printBoardPlayer(playerNum);
this.guiBoard.updateGame();
socket.close();
System.exit(0);
}
}
public ArrayList<String> splitOnSpaces(String str) {
ArrayList<String> strArray = new ArrayList<String>();
String currWord = "";
for (int i = 0; i < str.length(); i++) {
if ((str.charAt(i) == ' ') && (currWord != "")) {
strArray.add(currWord);
currWord = "";
}
else if (str.charAt(i) != ' ') {
currWord = currWord + str.charAt(i);
}
}
if (currWord != "") {
strArray.add(currWord);
}
return strArray;
}
public int otherPlayer(int p) {
if (p == 1) {
return 2;
}
else if (p == 2) {
return 1;
}
else {
return -1;
}
}
public int getPlayerNum() {
return playerNum;
}
public static void main(String[] args) throws IOException {
ConnectingUser u = new ConnectingUser();
u.sendUserMoves();
}
ArrayList<Integer> parseForMoves(String movesSent) {
ArrayList<Integer> movesList = new ArrayList<Integer>();
for (int i = 0; i < movesSent.length(); i++) {
if (Character.isDigit(movesSent.charAt(i))) {
movesList.add(Character.getNumericValue(movesSent.charAt(i)));
}
}
return movesList;
}
}