-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
268 lines (250 loc) · 5.89 KB
/
Server.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
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.Executors;
/**
* Server class to make a server for the clients to join
*
* @author Saharsh
*
*/
public class Server {
private Set<PrintWriter> players = new HashSet<>();
String res;
int move, wins, nplay, mvc;
int cross[][];
private ServerSocket serverSocket;
/**
* Server constructor to initiate the serverSocket variables
*
* @param serverSocket
* @throws IOException
*/
Server(ServerSocket serverSocket) throws IOException{
move= 1;
wins = 0;
nplay = 0;
mvc = 0;
cross = new int[3][3];
for(int i=0; i<3; i++) {
for(int j=0;j<3; j++) {
cross[i][j] = 0;
}
}
this.serverSocket = serverSocket;
res = "";
}
/**
* start() to accept the clients and get the output
*/
public void start() {
var pool = Executors.newFixedThreadPool(200);
int clientCount = 0;
while (true) {
try {
Socket socket1 = serverSocket.accept();
System.out.println("Connected to client " + ++clientCount);
PrintWriter out1 = new PrintWriter(socket1.getOutputStream(), true);
out1.println("1");
Socket socket2 = serverSocket.accept();
System.out.println("Connected to client " + ++clientCount);
PrintWriter out2 = new PrintWriter(socket2.getOutputStream(), true);
out2.println("2");
Scanner in1 = new Scanner(socket1.getInputStream());
Scanner in2 = new Scanner(socket2.getInputStream());
while(!in1.hasNextLine()) {}
nplay++;
String temp = in1.nextLine();
while(!in2.hasNextLine()) {}
nplay++;
temp = in2.nextLine();
players.add(out1);
players.add(out2);
out1.println("2 players.");
pool.execute(new Handler(socket1, in1, out1));
out2.println("2 players.");
pool.execute(new Handler(socket2, in2, out2));
}
catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* moves() takes string in the format player, row, column and then checks if a move is valid. It also sends it to clients.
*
* @param str
*/
public void moves(String str) {
if(nplay==2) {
String aa = String.valueOf(str.charAt(0));
String bb = String.valueOf(str.charAt(1));
String cc = String.valueOf(str.charAt(2));
int wyp = Integer.parseInt(aa);
int rw = Integer.parseInt(bb);
int cl = Integer.parseInt(cc);
if(wyp == move&& cross[rw][cl]==0) {
cross[rw][cl] = wyp;
mvc++;
switchPlay();
}
String st = "";
for(int i=0; i<=2;i++) {
for(int j=0; j<=2; j++) {
st += Integer.toString(cross[i][j]);
}
}
st+= Integer.toString(move);
checkForwins();
st += Integer.toString(wins);
res = st;
}
}
/**
* switchPlay() to change the turns of the players after one move.
*/
public void switchPlay() {
if(move== 1) {
move= 2;
} else if(move== 2) {
move= 1;
}
}
/**
* check Rows, Columns and Diagonals to determine the winner.
*/
public void checkRowColDiag() {
if(cross[0][1]==cross[0][2] && cross[0][1]==cross[0][0]) {
if(cross[0][0]==2) {
wins = 2;
} else if(cross[0][0]==1) {
wins = 1;
}
}
else if(cross[2][2]==cross[2][1] && cross[2][1]==cross[2][0]) {
if(cross[2][0]==2) {
wins = 2;
} else if(cross[2][0]==1) {
wins = 1;
}
}
else if(cross[1][2]==cross[1][1] && cross[1][1]==cross[1][0]) {
if(cross[1][0]==2) {
wins = 2;
} else if(cross[1][0]==1) {
wins = 1;
}
}
else if(cross[0][0]==cross[1][0] && cross[1][0]==cross[2][0]) {
if(cross[0][0]==2) {
wins = 2;
} else if(cross[0][0]==1) {
wins = 1;
}
}
else if(cross[2][1]==cross[1][1] && cross[1][1]==cross[0][1]) {
if(cross[0][1]==2) {
wins = 2;
} else if(cross[0][1]==1) {
wins = 1;
}
}
else if(cross[2][2]==cross[1][2] && cross[1][2]==cross[0][2]) {
if(cross[0][2]==2) {
wins = 2;
} else if(cross[0][2]==1) {
wins = 1;
}
}
else if(cross[1][1]==cross[2][2] && cross[1][1]==cross[0][0]) {
if(cross[1][1]==2) {
wins = 2;
} else if(cross[1][1]==1) {
wins = 1;
}
}
else if(cross[1][1]==cross[0][2] && cross[1][1]==cross[2][0]) {
if(cross[1][1]==2) {
wins = 2;
} else if(cross[1][1]==1) {
wins = 1;
}
}
}
/**
* Checkforwins() determines the winner
*/
public void checkForwins() {
checkRowColDiag();
if(wins==0 && mvc==9) {
wins = 3;
}
}
/**
* reset() to bring the values back to default
*/
public void reset() {
for(int i=0; i<=2; i++) {
for(int j=0;j<=2; j++) {
cross[i][j] = 0;
}
}
nplay = 0;
mvc = 0;
move= 1;
wins = 0;
res = "";
}
/**
* Handler that implements the runnable to handle the input and output of the clients
*
* @author Saharsh
*
*/
public class Handler implements Runnable {
private Socket soc;
private Scanner inp;
private PrintWriter outp;
/**
* Handler constructor to update the socket, input and output
*
* @param soc
* @param inp
* @param outp
*/
public Handler(Socket soc, Scanner inp, PrintWriter outp) {
this.soc = soc;
this.outp = outp;
this.inp = inp;
}
/**
* run() overridden according to TicTacToe
*/
public void run() {
System.out.println("Connected: " + soc);
try {
while(inp.hasNextLine()) {
nplay = 2;
String command = inp.nextLine();
moves(command);
for(PrintWriter player : players) {
player.println(res);
}
}
}
catch(Exception e) {
System.out.println(e.getMessage());
}
finally {
if(outp != null) {
players.remove(outp);
nplay--;
for(PrintWriter player : players) {
player.println("Game Ends. One of the players left.");
}
reset();
}
}
}
}
}