-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigTwoClient.java
More file actions
243 lines (216 loc) · 7.47 KB
/
BigTwoClient.java
File metadata and controls
243 lines (216 loc) · 7.47 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
import javax.swing.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
public class BigTwoClient implements NetworkGame{
private BigTwo game;
private BigTwoGUI gui;
private Socket sock;
private ObjectOutputStream oos;
private ObjectInputStream ois;
private int playerID;
private String playerName;
private String serverIP;
private int serverPort = 2396;
/**
* constructor
* @param game game reference
* @param gui GUI
*/
public BigTwoClient(BigTwo game, BigTwoGUI gui){
this.game = game;
this.gui = gui;
gui.disable();
gui.repaint();
}
/**
* to get playerID
* @return playerID
*/
public int getPlayerID(){
return this.playerID;
}
/**
* set the playerID
* @param playerID the ID to set
* the playerID (index) of the local player.
*/
public void setPlayerID(int playerID){
this.playerID = playerID;
}
/**
* get the player name
* @return return playername
*/
public String getPlayerName(){
return this.playerName;
}
/**
* set the player name
* @param playerName the name of the local player
*/
public void setPlayerName(String playerName){
this.playerName = playerName;
}
/**
* get the server IP
* @return
*/
public String getServerIP(){
return this.serverIP;
}
/**
* set the IP
* @param serverIP
* the IP address of the server
*/
public void setServerIP(String serverIP){
this.serverIP = serverIP;
}
/**
* return "2396"
* @return
*/
public int getServerPort(){
return this.serverPort;
}
/**
* the port is 2396!
* @param serverPort
* the TCP port of the server
*/
@Override
public void setServerPort(int serverPort) {
System.out.println("Server port is 2396!!!");
this.serverPort = 2396;
}
/**
* connect to the server
*/
public void connect(){
this.setPlayerName(JOptionPane.showInputDialog("Name:", "Player name"));
this.setServerIP("127.0.0.1");
try{
this.sock = new Socket(this.serverIP, this.serverPort);
this.oos = new ObjectOutputStream(this.sock.getOutputStream());
System.out.println(playerName+" connected!");
ServerHandler sh = new ServerHandler();
Thread thread = new Thread(sh);
thread.start();
//MESSAGE: 1
sendMessage(new CardGameMessage(CardGameMessage.JOIN, -1, this.playerName));
//MESSAGE: 4
sendMessage(new CardGameMessage(CardGameMessage.READY, -1, null));
}catch(IOException ex){
ex.printStackTrace();
}
}
/**
* identify the message and do corresponding things
* @param message
* the specified message received from the server
*/
public synchronized void parseMessage(GameMessage message){
if (message.getType() == 0) {
if (message.getData() != null)
{
String[] playerNames = (String[]) message.getData();
for(int i = 0; i < 4; i++)
{
this.game.getPlayerList().get(i).setName(playerNames[i]);
}
}
gui.repaint();
gui.setActivePlayer(playerID);
}
else if (message.getType() == 1)
{
//this.gui.printMsg("message type 1 received\n");
this.game.setNumOfPlayers(Math.max(this.game.getNumOfPlayers(), message.getPlayerID() + 1));
if (message.getData() != null)
{
this.game.getPlayerList().get(message.getPlayerID()).setName((String)message.getData());
if (game.getMyId() == -1){
this.game.setMyId(message.getPlayerID());
}
//this.gui.printMsg("current numofplayers:"+this.game.getNumOfPlayers()+", to set:"+Math.max(this.game.getNumOfPlayers(), message.getPlayerID() + 1)+", myID: "+this.game.getMyId());
}
}
else if (message.getType() == 2)
{
gui.printMsg("This game has 4 players already!!!.");
}
else if (message.getType() == 3)
{
this.game.getPlayerList().get(message.getPlayerID()).setName("");
gui.disable();
if (!this.game.endOfGame()){
gui.printMsg(message.getData() + " has left the game.");
gui.printMsg("Waiting for new players to join......");
this.sendMessage(new CardGameMessage(4, -1, null));
}
}
else if (message.getType() == 4) {
gui.printMsg(this.game.getPlayerList().get(message.getPlayerID()).getName() + " is ready.\n");
}
else if (message.getType() == 5) {
this.game.start((BigTwoDeck)message.getData());
}
else if (message.getType() == 6) {
this.game.checkMove(message.getPlayerID(), (int[]) message.getData());
gui.disable();
if (this.game.getMyId() == this.game.getCurrentPlayerIdx()) {
gui.enable();
gui.printMsg("Your turn\n");
} else {
gui.printMsg(this.game.getPlayerList().get(this.game.getCurrentPlayerIdx()).getName() + "'s turn.\n");
}
}
else if (message.getType() == 7) {
gui.chatAppend(message);
}
else {
System.out.println("message not valid");
}
}
/**
* send the message to server
* @param message
* the specified message to be sent the server
*/
public void sendMessage(GameMessage message){
try
{
oos.writeObject(message);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
private class ServerHandler implements Runnable{
public ServerHandler(){
try {
ois = new ObjectInputStream(sock.getInputStream());
}catch (IOException e){
System.out.println("Exception in ServerHandler's constructor!!");
e.printStackTrace();
}
}
@Override
public void run() {
try{
while (true){
CardGameMessage messageIn = (CardGameMessage)ois.readObject();
if (messageIn != null){
parseMessage(messageIn);
}
}
} catch (IOException | ClassNotFoundException e) {
System.out.println("Exception in run method of ServerHandler!!");
throw new RuntimeException(e);
}
}
}
}