-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigTwoClient.java
More file actions
550 lines (480 loc) · 12.1 KB
/
BigTwoClient.java
File metadata and controls
550 lines (480 loc) · 12.1 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
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import javax.swing.JOptionPane;
/**
* The BigTwoClient class implements the CardGame interface and NetworkGame interface.
* It is used to model a Big Two card game that supports 4 players playing over the internet.
*
* @author Pranav Talwar
*
*/
public class BigTwoClient implements CardGame, NetworkGame{
/*
* An integer specifying the number of players.
*/
private int numOfPlayers;
/*
* A deck object for the BigTwo game.
*/
private Deck deck;
/*
* A list of all players in the game
*/
private ArrayList<CardGamePlayer> playerList;
/*
* A list of hands played on the table.
*/
private ArrayList<Hand> handsOnTable;
/*
* An integer specifying the playerID (i.e., index) of the local player.
*/
private int playerID;
/*
* A string specifying the name of the local player.
*/
private String playerName;
/*
* A string specifying the IP address of the game server.
*/
private String serverIP;
/*
* An integer specifying the TCP port of the game server.
*/
private int serverPort;
/*
* A socket connection to the game server.
*/
private Socket sock;
/*
* An ObjectOutputStream for sending messages to the server.
*/
private ObjectOutputStream oos;
/*
* An ObjectInputStream for receiving messages from the server.
*/
private ObjectInputStream ois;
/*
* An int type specifying the index of the active player.
*/
private int currentIdx;
/*
* A table object which is provides the necessary GUI.
*/
private BigTwoTable table;
/**
* A constructor for creating the BigTwo card game.
*/
public BigTwoClient()
{
playerList = new ArrayList<CardGamePlayer> ();
for(int i = 0; i < 4;i++)
{
CardGamePlayer player = new CardGamePlayer();
playerList.add(player);
}
numOfPlayers = playerList.size();
handsOnTable = new ArrayList<Hand>();
table = new BigTwoTable(this);
String name = (String) JOptionPane.showInputDialog("Enter your name: ");
if(name!="" || name!=null) {
setPlayerName(name);
}
else {
setPlayerName("Default_Name");
}
setServerIP("127.0.0.1");
setServerPort(2396);
makeConnection();
table.disable();
table.repaint();
currentIdx = -1;
}
@Override
public int getNumOfPlayers() {
return numOfPlayers;
}
@Override
public Deck getDeck()
{
return deck;
}
@Override
public ArrayList<CardGamePlayer> getPlayerList()
{
return playerList;
}
@Override
public ArrayList<Hand> getHandsOnTable()
{
return handsOnTable;
}
@Override
public int getCurrentIdx()
{
return currentIdx;
}
@Override
public void start(Deck Deck)
{
// removing all cards from the table
handsOnTable.clear();
//removing cards from players hands
for(int i=0;i<4;i++) {
playerList.get(i).getCardsInHand().removeAllCards();
}
// giving cards to players
int player = 0;
for(int i = 0; i <52; i++)
{
playerList.get(player%4).addCard(Deck.getCard(i));
player++;
}
// sorting cards present in player's hands
playerList.get(0).getCardsInHand().sort();
playerList.get(1).getCardsInHand().sort();
playerList.get(2).getCardsInHand().sort();
playerList.get(3).getCardsInHand().sort();
//finding the beginner player
Card beginningCard = new Card(0,2);
for(int i = 0; i < getNumOfPlayers();i++)
{
if(playerList.get(i).getCardsInHand().contains(beginningCard))
{
currentIdx= i;
break;
}
}
table.setActivePlayer(getPlayerID());
table.printMsg("All players are ready. Game starts.");
table.printMsg(getPlayerList().get(currentIdx).getName() + "'s turn");
table.repaint();
}
@Override
public void makeMove(int playerID, int [] cardIdx)
{
CardGameMessage move = new CardGameMessage(CardGameMessage.MOVE, playerID, cardIdx);
sendMessage(move);
}
@Override
public void checkMove(int playerID, int [] cardIdx)
{
CardList cardlist = new CardList();
boolean isValid = true;
Card beginningCard = new Card(0,2);
if(cardIdx!= null) {
cardlist = playerList.get(playerID).play(cardIdx);
Hand hand = composeHand(playerList.get(playerID), cardlist);
if(handsOnTable.isEmpty())
{
//checks if beginning move contains 3 of diamonds
if(hand.contains(beginningCard) && hand.isEmpty()==false && hand.isValid()==true)
isValid = true;
else
isValid = false;
}
else {
if(handsOnTable.get(handsOnTable.size() - 1).getPlayer() != playerList.get(playerID))
{
if(!hand.isEmpty()) {
isValid = hand.beats(handsOnTable.get(handsOnTable.size() - 1));
}
else {
isValid = false;
}
}
else {
if(!hand.isEmpty()) {
isValid = true;
}
else {
isValid= false;
}
}
}
if(isValid && hand.isValid()) {
//removing played cards from the players hands
for(int i=0;i<cardlist.size();i++)
{
playerList.get(playerID).getCardsInHand().removeCard(cardlist.getCard(i));
}
table.printMsg("{" + hand.getType() + "} " + hand);
handsOnTable.add(hand);
currentIdx = (currentIdx + 1) % 4;
table.setActivePlayer(currentIdx);
table.printMsg(getPlayerList().get(currentIdx).getName()+ "'s turn");
}
else
{
table.printMsg(cardlist +" <= Not a legal move!!!");
}
}
else {
// handles passing by a player
if(!handsOnTable.isEmpty() && handsOnTable.get(handsOnTable.size()-1).getPlayer() != playerList.get(playerID)) {
currentIdx = (currentIdx+1)%4;
table.setActivePlayer(currentIdx);
table.printMsg("{Pass}");
table.printMsg(getPlayerList().get(currentIdx).getName() + "'s turn");
isValid = true;
}
else {
table.printMsg("Not a legal move!!!");
isValid= false;
}
}
table.repaint();
// checking for end of game
if(endOfGame()) {
table.setActivePlayer(-1);
table.repaint();
String message = "Game ends.\n";
//check who wins and how many cards the other players have
for(int i = 0; i < playerList.size();i++)
{
if(playerList.get(i).getCardsInHand().size() == 0)
{
message+="Player " + i + " wins the game.\n";
}
else
{
message+="Player " + i + " has " + playerList.get(i).getCardsInHand().size() + " cards in hand.\n"; // list the number of cards left in the other players' hand
}
}
table.disable();
for (int i=0; i<4; ++i)
{
getPlayerList().get(i).removeAllCards();
}
JOptionPane.showMessageDialog(null, message);
CardGameMessage ready = new CardGameMessage(CardGameMessage.READY,-1,null);
sendMessage(ready);
}
}
@Override
public boolean endOfGame() {
for(int i=0;i<playerList.size();i++)
{
if(playerList.get(i).getNumOfCards() == 0) {
return true;
}
}
return false;
}
@Override
public int getPlayerID() {
return playerID;
}
@Override
public void setPlayerID(int playerID) {
this.playerID = playerID;
}
@Override
public String getPlayerName() {
return playerName;
}
@Override
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
@Override
public String getServerIP() {
return serverIP;
}
@Override
public void setServerIP(String serverIP) {
this.serverIP = serverIP;
}
@Override
public int getServerPort() {
return serverPort;
}
@Override
public void setServerPort(int serverPort) {
this.serverPort = serverPort;
}
/**
* A function which tells if the client is connected to the server or not.
*
* @return boolean true if client is connected to server, otherwise false
*/
public boolean isConnected() {
if(sock.isClosed()) {
return false;
}
return true;
}
@Override
public void makeConnection() {
try {
sock = new Socket(getServerIP(), getServerPort());
oos = new ObjectOutputStream(sock.getOutputStream());
Runnable job = new ServerHandler();
Thread thread = new Thread(job);
thread.start();
CardGameMessage joinGame = new CardGameMessage(CardGameMessage.JOIN, -1, this.getPlayerName());
sendMessage(joinGame);
CardGameMessage readyGame = new CardGameMessage(CardGameMessage.READY, -1, null);
sendMessage(readyGame);
table.repaint();
}
catch(Exception e) {
e.printStackTrace();
}
}
@Override
public void parseMessage(GameMessage message) {
if(message.getType()==CardGameMessage.FULL) {
table.printMsg("The server is full and cannot be joined!");
try {
sock.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
else if(message.getType()==CardGameMessage.JOIN) {
getPlayerList().get(message.getPlayerID()).setName((String)message.getData());
table.repaint();
table.printMsg("Player " + playerList.get(message.getPlayerID()).getName() + " joined the game!");
}
else if(message.getType()==CardGameMessage.MOVE) {
checkMove(message.getPlayerID(),(int[])message.getData());
table.repaint();
}
else if(message.getType()==CardGameMessage.MSG) {
table.printChatMsg((String)message.getData());
}
else if(message.getType()==CardGameMessage.PLAYER_LIST) {
setPlayerID(message.getPlayerID());
table.setActivePlayer(message.getPlayerID());
for(int i=0;i<getNumOfPlayers();i++)
{
if(((String[])message.getData())[i]!=null) {
getPlayerList().get(i).setName(((String[])message.getData())[i]);
}
}
}
else if(message.getType()==CardGameMessage.QUIT) {
table.printMsg("Player " + message.getPlayerID() + " " + playerList.get(message.getPlayerID()).getName() + " left the game.");
getPlayerList().get(message.getPlayerID()).setName("");
if(!endOfGame()) {
table.disable();
CardGameMessage msg = new CardGameMessage(CardGameMessage.READY, -1, null);
sendMessage(msg);
for(int i=0;i<4;i++) {
getPlayerList().get(i).removeAllCards();
}
table.repaint();
}
table.repaint();
}
else if(message.getType()==CardGameMessage.READY) {
table.printMsg("Player " + message.getPlayerID() + " is ready.");
}
else if(message.getType()==CardGameMessage.START) {
deck = (BigTwoDeck) message.getData();
start(deck);
table.enable();
table.repaint();
}
}
@Override
public void sendMessage(GameMessage message) {
try {
oos.writeObject(message);
}
catch(Exception e) {
e.printStackTrace();
}
}
/**
* An inner class that implements the Runnable interface.
*
* @author Pranav Talwar
*
*/
class ServerHandler implements Runnable{
@Override
public void run() {
CardGameMessage message = null;
try
{
ois = new ObjectInputStream(sock.getInputStream());
while(!sock.isClosed()) {
if((message = (CardGameMessage) ois.readObject()) != null)
{
parseMessage(message);
}
}
ois.close();
}
catch (Exception e)
{
e.printStackTrace();
}
table.repaint();
}
}
/**
* A method to return a valid hand from all the list of cards played by the player.
*
* @param player A CardGamePlayer object which contains the list of players in the game.
* @param cards A CardList object which contains list of cards played by the active player.
*
* @return the type of hand
*/
public static Hand composeHand(CardGamePlayer player, CardList cards)
{
StraightFlush straightflush = new StraightFlush(player,cards);
if(straightflush.isValid())
{
return straightflush;
}
Quad quad = new Quad(player,cards);
if(quad.isValid())
{
return quad;
}
FullHouse fullhouse = new FullHouse(player,cards);
if(fullhouse.isValid())
{
return fullhouse;
}
Flush flush = new Flush(player,cards);
if(flush.isValid())
{
return flush;
}
Straight straight = new Straight(player,cards);
if(straight.isValid())
{
return straight;
}
Triple triple = new Triple(player,cards);
if(triple.isValid())
{
return triple;
}
Pair pair = new Pair(player,cards);
if(pair.isValid())
{
return pair;
}
Single single = new Single(player,cards);
if(single.isValid())
{
return single;
}
return new Hand(player, cards);
}
/**
* A method for creating an instance of BigTwoClient.
*
* @param args unused
*/
public static void main(String [] args)
{
BigTwoClient game = new BigTwoClient();
}
}