-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestClient.java
295 lines (255 loc) · 6.04 KB
/
TestClient.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
package connect4_main;
import java.io.*;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import processing.core.PApplet;
public class TestClient extends PApplet
{
public static final String SERVER_HOSTNAME = "localhost";
public static final int SERVER_PORT = 12010;
public static Handler handler;
public static StateMessage stateMsg = null;
public void settings()
{
size(500,500);
}
public void draw()
{
background(0);
stateMsg = handler.getLatestStateMsg();
if(stateMsg!=null)
{
int x=100,y=130;
int tx= 100 + stateMsg.slotWidth/2,ty = 120;
fill(255,255,255);
text("Left and Right arrow keys to move",160,440);
text("Enter to drop coin",200,460);
triangle(tx+stateMsg.pointer*45,ty,tx-10+stateMsg.pointer*45,ty-10,tx+10 + stateMsg.pointer*45,ty-10);
for(int i=0;i<stateMsg.dimensionX;i++)
{
for(int j=0;j<stateMsg.dimensionY;j++)
{
if(stateMsg.matrix[i][j]==0)
{
fill(255,255,255);
rect(x,y,stateMsg.slotWidth,stateMsg.slotHeight);
}
else if(stateMsg.matrix[i][j]==1)
{
fill(255,0,0);
rect(x,y,stateMsg.slotWidth,stateMsg.slotHeight);
}
else if(stateMsg.matrix[i][j]==2)
{
fill(0,0,255);
rect(x,y,stateMsg.slotWidth,stateMsg.slotHeight);
}
x= x + 10 + stateMsg.slotWidth;
}
x=100;
y=y+45;
}
if(stateMsg.winCondition)
{
fill(255,255,255);
textSize(35);
text("Player "+stateMsg.winner+" wins!", 130,70);
textSize(10);
text("Press N to start a new game",180,90);
}
else
{
fill(255,255,255);
text("Player "+stateMsg.turn+" 's turn",200,20);
}
}
else
{
System.out.println("null");
}
}
public static void main(String args[])
{
ObjectInputStream in = null;
ObjectOutputStream out = null;
try
{
Socket socket = new Socket(SERVER_HOSTNAME, SERVER_PORT);
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
System.out.println("Connected to server "+ SERVER_HOSTNAME + ":" + SERVER_PORT);
}
catch (IOException ioe)
{
System.err.println("Can not establish connection to " +
SERVER_HOSTNAME + ":" + SERVER_PORT);
ioe.printStackTrace();
System.exit(-1);
}
Sender sender = new Sender(out);
handler = new Handler(sender);
handler.start();
sender.setDaemon(true);
sender.start();
Receiver receiver = new Receiver(in,handler);
receiver.setDaemon(true);
receiver.start();
PApplet.main("connect4_main.TestClient");
}
@Override
public void keyPressed()
{
if(key==CODED)
{
switch(keyCode)
{
case LEFT:
handler.sendOutboundMessage("left");
break;
case RIGHT:
handler.sendOutboundMessage("right");
break;
}
}
else if(key==ENTER)
{
handler.sendOutboundMessage("enter");
}
else if(key=='n')
{
handler.sendOutboundMessage("n");
}
}
}
class Handler extends Thread
{
int player;
StateMessage stateMsg;
ClientToServerMessage clientMessage = new ClientToServerMessage();
Sender senderThread;
ConcurrentHashMap<Integer,Object> hashmap = new ConcurrentHashMap<Integer, Object>();//incoming StateMessages
public Handler(Sender senderThread)
{
this.senderThread=senderThread;
}
public synchronized StateMessage getLatestStateMsg()
{
//Get Local State
if (stateMsg!=null)
{
return stateMsg;
}
else
{
return null;
}
}
//GameLoop updates
public synchronized void sendOutboundMessage(String key)
{
clientMessage.keyPressInfo(player,key);
senderThread.addObjectToQueue(clientMessage);
}
public synchronized void newObjectReceived(Object anObject)
{
//Update Local State
if(anObject instanceof StateMessage)
{
stateMsg = (StateMessage) anObject;
}
//Update Character if InitMessage
else if(anObject instanceof InitMessage)
{
InitMessage temp = (InitMessage) anObject;
player = temp.playerId;
System.out.println("Player"+player);
}
}
}
class Sender extends Thread
{
private ObjectOutputStream oos;
Vector<Object> objectsToBeSent=new Vector<Object>();
public Sender(ObjectOutputStream oos)
{
this.oos=oos;
}
public void sendObjectToServer(Object object)
{
try
{
oos.writeObject(object);
oos.reset();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public synchronized void addObjectToQueue(Object anObject)
{
if(anObject!=null)
{
objectsToBeSent.add(anObject);
notify();
}
}
private synchronized Object getNextObjectFromQueue() throws InterruptedException
{
while(objectsToBeSent.size()==0)
wait();
Object anObject = objectsToBeSent.get(0);
objectsToBeSent.removeElementAt(0);
return anObject;
}
public void run()
{
try
{
while (!isInterrupted())
{
Object anObject = getNextObjectFromQueue();
sendObjectToServer(anObject);
}
}
catch (Exception e)
{
}
}
}
class Receiver extends Thread
{
public ObjectInputStream ois;
Handler handler;
Receiver(ObjectInputStream ois, Handler handler)
{
this.ois=ois;
this.handler=handler;
}
@Override
public void run()
{
try
{
while (!isInterrupted())
{
try
{
Object anObject = ois.readObject();
if(anObject == null)
break;
handler.newObjectReceived(anObject);
}
catch (SocketTimeoutException | ClassNotFoundException ste)
{
}
}
}
catch (IOException ioex)
{
}
}
}