-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCardGame.java
More file actions
439 lines (374 loc) · 12.5 KB
/
CardGame.java
File metadata and controls
439 lines (374 loc) · 12.5 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
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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Scanner;
import java.util.concurrent.CyclicBarrier;
public class CardGame {
private int numOfDecks;
private int numOfPlayers;
private int numOfDeals;
private CardCollection pile;
private CyclicBarrier barrier;
private GameLogger logger;
private ArrayList<PlayerThread> players;
private PlayerThread firstPlayer;
private PlayerThread winner = null;
private Suit selectedSuit;
private Thread[] playerThreads;
private Boolean isTied;
private final int DECK_LIMIT = 4;
// ______________PUBLIC______________
/*
* Constructor that initializes game state and sets the game parameters by reading them from
* the start file passed as the argument.
*/
public CardGame(String startFile) throws FileNotFoundException {
setGameParameters(startFile);
}
/*
* Runs the whole game.
*/
public void run() throws InterruptedException {
logger.logHeading("GAME PARAMETERS");
logger.addNewLine();
logger.log("--Number of decks: " + numOfDecks);
logger.log("--Number of players: " + numOfPlayers);
// Request Player objects to obtain their player's name over their corresponding
// sockets and set it.
try {
setPlayerNames();
logger.log("\n--PLAYER NAMES:");
for(PlayerThread player : players) {
System.out.println("Connected with client of " + player.getPlayerName());
logger.log("----" + player.getPlayerName());
}
}
catch (ClassNotFoundException | IOException e) {
System.err.println("Error setting player names!");
e.printStackTrace();
}
// Determine the first player. Also log the card distribution that leads to the winner.
determineFirstPlayer();
// Request the first player to select a suit.
try {
selectWinningSuit();
logger.log("--SELECTED SUIT: " + selectedSuit + "\n");
}
catch (ClassNotFoundException | IOException e) {
System.err.println("Error during suit selection from first player!");
e.printStackTrace();
}
setPlayerTurns();
logger.logHeading("ORDER OF TURNS");
logger.addNewLine();
for(PlayerThread player : players) {
logger.log("----" + player.getTurn() + " -> " + player.getPlayerName());
}
logger.addNewLine();
setPlayerParameters();
try {
// Send selected suit to clients
sendInfoToClients();
}
catch(IOException e) {
System.err.println("Error sending information to clients!");
}
try {
dealCards();
}
catch (IOException e) {
System.err.println("Error dealing cards!");
e.printStackTrace();
}
logger.logHeading("INITIAL HANDS");
logger.addNewLine();
try {
logPlayerHands();
}
catch (ClassNotFoundException | IOException e) {
System.err.println("Error logging initial player hands");
e.printStackTrace();
}
// Player threads coordinate their turns between themselves. The game only spawns
// the player threads, passes them some information and waits for them to complete.
// This is a requirement of the assignment which states that player threads must work
// independently.
spawnPlayerThreads();
// Wait for player threads to complete both their turns
for (int i = 0; i < players.size(); ++i) {
playerThreads[i].join();
}
logger.logHeading("FINAL HANDS");
logger.addNewLine();
try {
logPlayerHands();
}
catch (ClassNotFoundException | IOException e) {
System.err.println("Error logging final player hands!");
e.printStackTrace();
}
try {
determineWinner();
}
catch (ClassNotFoundException | IOException e) {
System.err.println("Error determining winner of the game!");
e.printStackTrace();
}
closeClientConnections();
logger.close();
}
/*
* Returns the name of the winner.
*/
public String getWinnerName() {
if (winner == null)
return "Game has not finished yet!";
else
return winner.getPlayerName();
}
/*
* Returns the number of decks being used for the game.
*/
public int getNumberOfDecks() {
return numOfDecks;
}
/*
* Returns the number of players playing the game.
*/
public int getNumberOfPlayers() {
return numOfPlayers;
}
/*
* Add a new player and its store its socket which is passed by the server.
* Create Object streams using the socket and pass them to the Player object
* so that player threads can use these streams to communication with their
* corresponding client. The player object is not sent the socket because
* it is not possible to open and close streams multiple times.
*/
public void addPlayer(Socket playerSocket) {
ObjectOutputStream out = null;
ObjectInputStream in = null;
try {
out = new ObjectOutputStream(playerSocket.getOutputStream());
in = new ObjectInputStream(playerSocket.getInputStream());
}
catch (IOException e) {
System.err.println("Failed to open object stream with client!");
e.printStackTrace();
}
players.add(new PlayerThread(out, in));
}
// ______________PRIVATE______________
/*
* Informs player threads to terminate their connections with their clients.
*/
private void closeClientConnections() {
for(PlayerThread player : players) {
try {
if (isTied && winner == null) {
player.closeConnection(GameProtocol.GAME_TIED);
}
else if (player == winner) {
player.closeConnection(GameProtocol.YOU_WIN);
}
else {
player.closeConnection(GameProtocol.YOU_LOSE);
}
}
catch (IOException e) {
System.err.println("Failed to close client connection of " + player.getPlayerName());
e.printStackTrace();
}
}
}
/*
* Gets the player's name using the socket associated with that player.
*/
private void setPlayerNames() throws ClassNotFoundException, IOException {
for(PlayerThread player : players) {
player.setPlayerName();
}
}
/*
* Parses and sets the game parameters from the String returned by getGameParameters.
*/
private void setGameParameters(String filename) throws FileNotFoundException {
String gameInputs = getGameParameters(filename);
// Tokenise the string containing game parameters.
String[] tokens = gameInputs.split(",");
// Trimming white space from all tokens
Arrays.parallelSetAll(tokens, i -> tokens[i].trim());
this.numOfDecks = Integer.parseInt(tokens[0]);
this.numOfPlayers = Integer.parseInt(tokens[1]);
this.numOfDeals = (numOfDecks * Deck.DECK_SIZE) / numOfPlayers;
if (tokens.length < 2)
throw new InvalidGameParameterException("Invalid number of game parameters in the start file!");
if (numOfDecks < 1 || numOfDecks > 4)
throw new InvalidGameParameterException("Invalid number of decks in the start file!");
if (numOfPlayers < 2 || numOfPlayers > 4)
throw new InvalidGameParameterException("Invalid number of players in the start file!");
players = new ArrayList<>(numOfPlayers);
// DECK_LIMIT indicates the maximum number of decks that can be in the pile
pile = new CardCollection(numOfDecks, DECK_LIMIT);
pile.shuffle();
barrier = new CyclicBarrier(numOfPlayers);
logger = new GameLogger("logfile.txt");
}
/*
* Reads the game parameters from the given file and returns them as a String. These game
* parameters include the number of players and the number of decks being used for the game.
*/
private String getGameParameters(String filename) throws FileNotFoundException {
File startFile = new File(filename);
Scanner scanner = new Scanner(startFile);
// Ignore first line of file which only contains headings
scanner.nextLine();
String gameInputs = scanner.nextLine();
scanner.close();
return gameInputs;
}
/*
* Determines who the first player will be by randomly dealing cards to each player until a Jack is drawn.
* The player that draws the Jack is set as the first player
*/
private void determineFirstPlayer() {
logger.log("\n--DETERMINIG FIRST PLAYER:");
for (int i = 0; i < numOfDeals; ++i) {
for (int j = 0; j < numOfPlayers; ++j) {
Card drawnCard = pile.drawRandomCard();
logger.log("----" + drawnCard.getName() + " dealt to " + players.get(j).getPlayerName());
if (drawnCard.getCardType() == CardType.JACK) {
firstPlayer = players.get(j);
logger.log("\n--FIRST PLAYER: " + firstPlayer.getPlayerName());
return;
}
}
}
}
/*
* First player selects and sets the a winning suit
*/
private void selectWinningSuit() throws ClassNotFoundException, IOException {
selectedSuit = firstPlayer.getSelectedSuitFromClient();
}
/*
* Places first player at the start of the players ArrayList. Also assigns each player a turn.
*/
private void setPlayerTurns() {
// Setting firstPlayer at the start of the players ArrayList. The order of the
// ArrayList defines the sequence of turns of each player in the game.
int index = players.indexOf(firstPlayer);
players.remove(index);
players.add(0, firstPlayer);
// Set the turns of each player
for (int i = 0; i < numOfPlayers; ++i) {
players.get(i).setTurn(i + 1);
}
}
/*
* Pass Player class some important parameters so that the player threads can play the game independently.
*/
private void setPlayerParameters() {
PlayerThread.setLogger(logger);
PlayerThread.setPile(pile);
PlayerThread.setBarrier(barrier);
}
/*
* Passes some necessary information to the clients such as the selected suit of this game.
*/
private void sendInfoToClients() throws IOException {
if (selectedSuit == null)
throw new IllegalStateException("Suit has not been selected for the game yet");
for(PlayerThread player : players)
player.sendSelectedSuit(selectedSuit);
}
private void dealCards() throws IOException {
// Reset pile before dealing
pile.clear();
pile.addDecks(numOfDecks);
pile.shuffle();
// Deal cards one by one to all players
for (int i = 0; i < numOfDeals; ++i) {
for (int j = 0; j < numOfPlayers; ++j) {
Card drawnCard = pile.drawRandomCard();
players.get(j).pickupCard(drawnCard);
}
}
}
/*
* Log all the cards in the current hand of all players.
*/
private void logPlayerHands() throws ClassNotFoundException, IOException {
for (PlayerThread player : players) {
logger.log("--" + player.getPlayerName() + "'s hand " + "(" + player.getHandSize() + ")"+ ":");
logger.logCards(player.getHand());
logger.addNewLine();
}
}
/*
* Create player threads that will play the game independently.
*/
private void spawnPlayerThreads() {
playerThreads = new Thread[numOfPlayers];
for (int i = 0; i < players.size(); ++i) {
Thread playerThread = new Thread(players.get(i));
playerThread.start();
playerThreads[i] = playerThread;
}
}
/*
* Determine the winner of the game by calculating the scores of each player. Also checks whether
* the game has been tied in case no winner is found.
*/
private void determineWinner() throws ClassNotFoundException, IOException {
int maxScore = 0;
isTied = false;
logger.logHeading("PLAYER SCORES");
logger.addNewLine();
for(PlayerThread player : players) {
int score = calculateHandScore(player.getHand());
if (score > maxScore) {
maxScore = score;
winner = player;
}
else if (score == maxScore) {
isTied = true;
}
logger.log("----" + player.getPlayerName() + "'s score is " + score);
}
if (!isTied) {
logger.log("\n--WINNER: " + winner.getPlayerName());
System.out.println("\nWinner of the game is " + winner.getPlayerName());
}
else {
System.out.println("\nGame is tied");
logger.log("\n--GAME IS TIED");
}
}
/*
* Returns the score of a card.
*/
private int getCardScore(Card card) {
if (card.getSuit() != selectedSuit)
return card.getCardType().getCardValue();
else
return (2 * card.getCardType().getCardValue());
}
/*
* Returns the total score of a players hand.
*/
private int calculateHandScore(CardCollection hand) {
int totalScore = 0;
Iterator<Card> iter = hand.iterator();
while(iter.hasNext()) {
totalScore += getCardScore(iter.next());
}
return totalScore;
}
}