-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBigTwoClient.java
260 lines (216 loc) · 6.92 KB
/
BigTwoClient.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
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
/**
* This class is used for modeling client for the Big Two card game.
*
* @author Chan Yuk Yin (UID: 3035786574)
*/
public class BigTwoClient implements NetworkGame{
//public contructor
public BigTwoClient(BigTwo game, BigTwoGUI gui){
this.game = game;
this.gui = gui;
this.playerID = game.getNumOfPlayers();
this.playerName = (String) JOptionPane.showInputDialog("Name: ");
if (this.playerName == null){
this.playerName = "Player " + playerID;
}
connect();
}
private BigTwo game;
private BigTwoGUI gui;
private Socket sock;
private ObjectOutputStream oos;
private int playerID;
private String playerName;
private String serverIP;
private int serverPort;
/**
* Getter of PlayerID
* @return int
*/
@Override
public int getPlayerID() {
return this.playerID;
}
/**
* Setter of playerID
* @param playerID
*/
@Override
public void setPlayerID(int playerID) {
this.playerID = playerID;
}
/**
* Getter of PlayerName
* @return String
*/
@Override
public String getPlayerName() {
return this.playerName;
}
/**
* Setter of PlayerName
* @param playerName
*/
@Override
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
/**
* Getter of ServerIP
* @return String
*/
@Override
public String getServerIP() {
return this.serverIP;
}
/**
* Setter of ServerIP
* @param serverIP
*/
@Override
public void setServerIP(String serverIP) {
this.serverIP = serverIP;
}
/**
* Getter of ServerPort
* @return int
*/
@Override
public int getServerPort() {
return this.serverPort;
}
/**
* Setter of ServerPort
* @param serverPort
*/
@Override
public void setServerPort(int serverPort) {
this.serverPort = serverPort;
}
/**
* Connect sock to the BigTwoServer
*/
@Override
public void connect() {
setServerIP("127.0.0.1");
setServerPort(2396);
try {
sock = new Socket(serverIP, serverPort);
oos = new ObjectOutputStream(sock.getOutputStream());
Runnable threadJob = new ServerHandler();
Thread myThread = new Thread(threadJob);
myThread.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Parse msg from BigTwoServer
* @param message
*/
@Override
public void parseMessage(GameMessage message) {
switch (message.getType()) {
// update the players information on this client
case CardGameMessage.PLAYER_LIST:
setPlayerID(message.getPlayerID());
gui.setActivePlayer(message.getPlayerID());
String[] names = (String[]) message.getData();
for (int i = 0; i < names.length ; i++){
if (names[i]!=null){
game.getPlayerList().get(i).setName(names[i]);
}
}
sendMessage(new CardGameMessage(CardGameMessage.JOIN, this.playerID, getPlayerName()));
break;
// update new player
case CardGameMessage.JOIN:
gui.printMsg("Welcome! Our new player : "+ (String) message.getData());
game.getPlayerList().get(message.getPlayerID()).setName((String) message.getData());
if (((String)message.getData()).equals(playerName)){
sendMessage(new CardGameMessage(CardGameMessage.READY, -1, null));
}
break;
// If the game is Full
case CardGameMessage.FULL:
gui.printMsg("The game is full :/ Cannont join the game now.");
break;
// If Someone Quit
case CardGameMessage.QUIT:
gui.printMsg("Player "+message.getPlayerID()+" has left :/");
game.getPlayerList().get(message.getPlayerID()).setName("");
if (!game.endOfGame()){
gui.disable();
sendMessage(new CardGameMessage(CardGameMessage.READY, -1, null));
}
break;
// If someone is ready
case CardGameMessage.READY:
gui.printMsg(game.getPlayerList().get(message.getPlayerID()).getName()+" is READY!!");
break;
// start if the game can start
case CardGameMessage.START:
gui.reset();
game.getHandsOnTable().clear();
game.start((Deck) message.getData());
gui.enable();
break;
// Make move
case CardGameMessage.MOVE:
game.checkMove(message.getPlayerID(), (int[]) message.getData());
// Print message for ending the game
if (game.endOfGame()){
String msg = "Game ends\n";
for (int i = 0; i < 4; i++){
if (game.getPlayerList().get(i).getNumOfCards() != 0){
msg += "Player "+i+" has "+game.getPlayerList().get(i).getNumOfCards()+" cards in hand.\n";
}
else{
msg += "Player "+i+" wins the game.\n";
}
}
JOptionPane.showMessageDialog(this.gui.getFrame(), msg);
sendMessage(new CardGameMessage(CardGameMessage.READY, -1, null));
}
break;
// texting
case CardGameMessage.MSG:
gui.printChat((String) message.getData());
break;
}
gui.repaint();
}
/**
* Send Msg to BigTwoServer
* @param message
*/
@Override
public synchronized void sendMessage(GameMessage message) {
try {
oos.writeObject(message);
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Inner class that implements the run method
* a runnable for multi-threading
*/
class ServerHandler implements Runnable{
public void run() {
CardGameMessage msg = null;
try{
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
while ((msg = (CardGameMessage) ois.readObject())!=null){
parseMessage(msg);
}
}catch(Exception ex){
ex.printStackTrace();
}
gui.repaint();
}
}
}