-
Notifications
You must be signed in to change notification settings - Fork 0
/
Manager.java
466 lines (376 loc) · 14.3 KB
/
Manager.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
import java.util.*;
import java.io.*;
import java.net.*;
import java.util.concurrent.Semaphore;
import java.lang.*;
class Manager {
private Player player1;
private Player player2;
//public final Semaphore moveSem;
public final Semaphore messageSem1;
public final Semaphore messageSem2;
public final Semaphore p1MoveSem;
public final Semaphore p2MoveSem;
protected ServerSocket listener;
protected PrintWriter out;
protected Socket socket;
protected BufferedReader in;
//Don't allow a getter for board, as we want the manager to be the only one changing it
private Board mainBoard;
int time;
int numSeeds;
int numPits;
String gameType;
int numSeconds;
//Holds AI process if it exists
Process AIprocess;
//by default player 1 is a user, player 2 is AI
Manager(int seeds, int pits, String gameType, int seconds, boolean random) throws IOException, InterruptedException {
//random distribution of seeds
ArrayList<Integer> rand = new ArrayList<Integer>();
if (random == true) {
rand = randomDist(pits, seeds);
mainBoard = new Board(rand, pits);
mainBoard.printBoardPlayer(1);
}
else {
mainBoard = new Board(seeds,pits);
}
numSeeds = seeds;
numPits = pits;
this.gameType = gameType;
this.numSeconds = seconds;
listener = new ServerSocket(56789);
messageSem1 = new Semaphore(1);
messageSem2 = new Semaphore(1);
p1MoveSem = new Semaphore(1);
p2MoveSem = new Semaphore(1);
System.out.println("Initial board:");
mainBoard.printBoardPlayer(1);
this.time = seconds;
System.out.println("Starting Mancala");
//this sections determines what game type we want to run, PvP, PvAI, AIvAI
if(gameType.equals("PvP")) {
System.out.println("Waiting for connections...");
player1 = new User(listener.accept(), 1, time, mainBoard, messageSem1, p1MoveSem, p2MoveSem);
System.out.println("Player 1 connected");
player2 = new User(listener.accept(), 2, time, mainBoard, messageSem2, p2MoveSem, p1MoveSem);
System.out.println("Player 2 connected");
player1.setOtherPlayer(player2);
player2.setOtherPlayer(player1);
//start sending messages
player1.init(random, rand);
player2.init(random, rand);
player1.start();
player2.start();
}
else if (gameType.equals("PvAI")){
System.out.println("Waiting for connection...");
player1 = new User(listener.accept(), 1, time, mainBoard, messageSem1, p1MoveSem, p2MoveSem);
System.out.println("Player 1 connected");
//launch the AI program
AIThread a = new AIThread();
a.start();
player2 = new User(listener.accept(), 2, time, mainBoard, messageSem2, p2MoveSem, p1MoveSem);
player1.setOtherPlayer(player2);
player2.setOtherPlayer(player1);
//start sending messages
player1.init(random, rand);
player2.init(random, rand);
player1.start();
player2.start();
}
else if (gameType.equals("AIvAI")){
System.out.println("Waiting for other AI to connect...");
player1 = new User(listener.accept(), 1, time, mainBoard, messageSem1, p1MoveSem, p2MoveSem);
System.out.println("AI 1 connected");
//launch the AI program
AIThread a = new AIThread();
a.start();
player2 = new User(listener.accept(), 2, time, mainBoard, messageSem2, p2MoveSem, p1MoveSem);
System.out.println("Both AIs connected");
player1.setOtherPlayer(player2);
player2.setOtherPlayer(player1);
//start sending messages
player1.init(random, rand);
player2.init(random, rand);
player1.start();
player2.start();
}
else{
System.out.println("Incorrect game type");
}
}
private synchronized boolean isValidMove(Player currentlyPlaying, int move) throws IOException {
//make sure index is valid
if ((move < 0) || (move >= mainBoard.getNumPits())) {
return false;
}
//make sure player does not choose empty pit
else if (mainBoard.getSeedsInPit(currentlyPlaying.getPlayerType().ordinal() + 1, move) == 0) {
return false;
}
else {
//send continue message
currentlyPlaying.setMessage(Player.message.OK);
return true;
}
}
//returns whether there is a winner or not
boolean checkWinner() {
int counterPlayer1 = 0;
int counterPlayer2 = 0;
for(int i = 0; i < mainBoard.getNumPits(); i++){
int player1PocketValue = mainBoard.getSeedsInPit(player1.getPlayerType().ordinal() + 1, i);
int player2PocketValue = mainBoard.getSeedsInPit(player2.getPlayerType().ordinal() + 1, i);
if(player1PocketValue > 0) {
counterPlayer1++;
}
if(player2PocketValue > 0) {
counterPlayer2++;
}
}
if(counterPlayer1 == 0){
return true;
}
else if(counterPlayer2 == 0){
return true;
}
return false;
}
public Player.playerType returnWinner() throws IOException, InterruptedException {
Player.playerType noWinner = Player.playerType.NONE;
int pitValue;
for(int i = 0; i < mainBoard.getNumPits(); i++){
pitValue = mainBoard.getSeedsInPit(1, i);
mainBoard.addStoreByValue(1, pitValue);
pitValue = mainBoard.getSeedsInPit(2, i);
mainBoard.addStoreByValue(2, pitValue);
}
if(mainBoard.accessStore(1) > mainBoard.accessStore(2)){
return player1.getPlayerType();
}
else if(mainBoard.accessStore(1) < mainBoard.accessStore(2)){
return player2.getPlayerType();
}
else{
return noWinner;
}
}
//If we have two players in the game, this fuction takes as input one Player and outputs the other Player
private Player.playerType otherPlayerType(Player.playerType player) {
if (player == player1.getPlayerType()) {
return player2.getPlayerType();
}
else {
return player1.getPlayerType();
}
}
//TODO: send last move to loser before sending the loser message
public void playGame() throws IOException, InterruptedException {
int turn = 1;
int counter = 0;
String player2Move = "";
String player1Move = "";
Board prevBoard;
while (!checkWinner()) {
prevBoard = new Board(mainBoard);
counter++;
mainBoard.printBoardPlayer(player1.playerNum);
if(turn == 1) {
player1Move = player1.getMove();
ArrayList<Integer> player1Moves = new ArrayList<Integer>(parseForMoves(player1Move));
//make every legal move. If we encounter an illegal move, set message to illegal
for (int i = 0; i < player1Moves.size(); i++) {
if (mainBoard.isValidMove(1, player1Moves.get(i))) {
if ((player1.getMessage() == Player.message.OK) && !checkWinner()) {
player1.setMessage(Player.message.OK);
}
else {
break;
}
int makeReturn = mainBoard.makeMove(1, player1Moves.get(i));
//if not supposed to go again but there is another move, this move is illegal
if ((makeReturn == 0) && (i != player1Moves.size() - 1)) {
player1.setMessage(Player.message.ILLEGAL);
mainBoard = prevBoard;
break;
}
}
else {
player1.setMessage(Player.message.ILLEGAL);
mainBoard = prevBoard;
break;
}
}
turn = 2;
player1.setBoard(mainBoard);
player2.setBoard(mainBoard);
}
else if(turn == 2) {
player2Move = player2.getMove();
ArrayList<Integer> player2Moves = new ArrayList<Integer>(parseForMoves(player2Move));
//pie move
if (player2Move.startsWith("P") && counter == 2) {
System.out.println("Pie move:");
mainBoard.pieMove();
mainBoard.printBoardPlayer(turn);
player2.setMessage(Player.message.OK);
player1.setBoard(mainBoard);
player2.setBoard(mainBoard);
turn = 1;
continue;
}
//make every legal move. If we encounter an illegal move, set message to illegal and reset the board to what it was before
for (int i = 0; i < player2Moves.size(); i++) {
if (mainBoard.isValidMove(2, player2Moves.get(i))) {
if ((player2.getMessage() == Player.message.OK) && !checkWinner()) {
player2.setMessage(Player.message.OK);
}
else {
break;
}
int makeReturn = mainBoard.makeMove(2, player2Moves.get(i));
if ((makeReturn == 0) && (i != player2Moves.size() - 1)) {
player2.setMessage(Player.message.ILLEGAL);
mainBoard = prevBoard;
break;
}
}
else {
player2.setMessage(Player.message.ILLEGAL);
mainBoard = prevBoard;
break;
}
}
turn = 1;
player1.setBoard(mainBoard);
player2.setBoard(mainBoard);
}
if (player1.getMessage() == Player.message.ILLEGAL) {
player2.sendMove(Player.message.ILLEGAL.name());
player1.sendMove(Player.message.ILLEGAL.name());
//pause to let client prepare to recieve winner/loser
try {
Thread.sleep(1000);
}
catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println("Final board:");
mainBoard.printBoardPlayer(1);
System.out.println("Player 2 wins! Player 1 sent an invalid move.");
player1.sendMove(Player.message.LOSER.name());
player2.sendMove(Player.message.WINNER.name());
break;
}
else if (player2.getMessage() == Player.message.ILLEGAL) {
player2.sendMove(Player.message.ILLEGAL.name());
player1.sendMove(Player.message.ILLEGAL.name());
//pause to let client prepare to recieve winner/loser
try {
Thread.sleep(1000);
}
catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println("Final board:");
mainBoard.printBoardPlayer(1);
System.out.println("Player 1 wins! Player 2 sent an invalid move.");
player2.sendMove(Player.message.LOSER.name());
player1.sendMove(Player.message.WINNER.name());
break;
}
else if (player1.getMessage() == Player.message.TIME || player2.getMessage() == Player.message.TIME) {
break;
}
}
if(checkWinner() && (player1.getMessage() != Player.message.TIME) && (player2.getMessage() != Player.message.TIME)){
mainBoard.printBoardPlayer(1);
System.out.println("Final board:");
mainBoard.winningBoard();
mainBoard.printBoardPlayer(1);
//send the last move
//most recent player to go was player 2
if (turn == 1) {
player1.sendMove(player2Move);
}
//most recent player to go was player 1
else if (turn == 2) {
player2.sendMove(player1Move);
}
//pause to let client prepare to recieve Winner/loser message
try {
Thread.sleep(1000);
}
catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
Player.playerType winningPlayer = returnWinner();
if(winningPlayer == player1.getPlayerType()){
System.out.println("Player 1 has won!");
player1.setMessage(Player.message.WINNER);
player2.setMessage(Player.message.LOSER);
player1.sendMove(Player.message.WINNER.name());
player2.sendMove(Player.message.LOSER.name());
}
else if (winningPlayer == player2.getPlayerType()){
System.out.println("Player 2 has won!");
player1.setMessage(Player.message.LOSER);
player2.setMessage(Player.message.WINNER);
player1.sendMove(Player.message.LOSER.name());
player2.sendMove(Player.message.WINNER.name());
}
else {
System.out.println("TIE");
player1.setMessage(Player.message.TIE);
player2.setMessage(Player.message.TIE);
player1.sendMove(Player.message.TIE.name());
player2.sendMove(Player.message.TIE.name());
}
}
player2.setDone(true);
player1.setDone(true);
player1.join();
player2.join();
// mainBoard.printBoardPlayer(1);
// System.out.println("Final board:");
// mainBoard.winningBoard();
// mainBoard.printBoardPlayer(1);
}
public Player otherPlayer(Player p) {
if (p == player1) {
return player2;
}
else if (p == player2) {
return player1;
}
//error
else {
return null;
}
}
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;
}
//get a random distribution of seeds for one side of the board
public ArrayList<Integer> randomDist(int pits,int seeds) {
ArrayList<Integer> distribution = new ArrayList<Integer>();
Random rand = new Random();
int totalSeeds = pits*seeds;
int seedsLeft = totalSeeds;
for (int i = 0; i < pits - 1; i++) {
int thisPit = rand.nextInt(seedsLeft);
seedsLeft = seedsLeft - thisPit;
distribution.add(thisPit);
}
distribution.add(seedsLeft);
return distribution;
}
}