-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
252 lines (200 loc) · 9.73 KB
/
Main.java
File metadata and controls
252 lines (200 loc) · 9.73 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Hashtable;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//creating a server socket
// try (ServerSocket serverSocket = new ServerSocket(5000)) {
//
// while (true) {
//
// //calling a client thread everytime a client tries to connect
// new ToClient(serverSocket.accept()).start();
// }
//
// } catch (IOException e) {
// System.out.println("server error : " + e.getMessage());
// }
Hashtable<String, Integer> ht1 = new Hashtable<>();
ht1.put("2*2", 4);
ht1.put("2*10", 20);
ht1.put("2+2", 4);
ht1.put("5*2", 10);
ht1.put("10+20", 30);
ht1.put("20-10", 10);
ht1.put("2*5", 10);
ht1.put("13-3", 10);
ht1.put("90/10", 9);
ht1.put("2*9", 18);
int size = ht1.size();
Scanner scanner = new Scanner(System.in);
System.out.println("Pls enter the number of players : ");
int players = scanner.nextInt();
scanner.nextLine();
//creating the class to keep the score
System.out.println("creating the Score class");
Score score = new Score(players);
Socket[] clientSocket = new Socket[players];
BufferedReader[] inputFromClient = new BufferedReader[players];
PrintWriter[] outputToClient = new PrintWriter[players];
try (ServerSocket serverSocket = new ServerSocket(5000)) {
Thread[] t = new Thread[players];
System.out.println("no of players is " + players);
//accepting the number of players(clients)
// we put the clients to sleep in a thread, if they send a message they are told to wait
System.out.println("Thread[] t of [players made] starting with the for loop to create the server socket");
for (int i = 0; i < players; i++) {
System.out.println("creating the client socket for " + i);
clientSocket[i] = serverSocket.accept();
System.out.println("client socket created now going to call the thread to make client sleep");
inputFromClient[i] = new BufferedReader(new InputStreamReader(clientSocket[i].getInputStream()));
outputToClient[i] = new PrintWriter(clientSocket[i].getOutputStream(), true);
int finalI = i;
System.out.println("finalI = " + finalI);
t[i] = new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
System.out.println("in the thread to make client sleep client number is " + finalI);
System.out.println("the IP and OP buffer things have been ade now waiting for a message from client");
String fromClient = inputFromClient[finalI].readLine();
System.out.println("from client : "+fromClient);
outputToClient[finalI].println(players + ";-1;pls wait for all to connect;" + score.getScore());
}
} catch (IOException e) {
System.out.println("Error in wait thread");
}
}
});
t[i].start();
}
System.out.println("All clients here now we will interrupt them all");
//once all clients connect we will stop all the threads
for (int i = 0; i < players; i++) {
System.out.println("interrupting client no" + i);
t[i].stop();
}
//here we are putting the thread to sleep again till its turn comes
for(int i=0; i<players; i++){
System.out.println("putting clients to sleep till it their turn");
int finalI = i;
t[i]= new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("in the thread to put client to sleep client no "+finalI);
while (true) {
String fromClient = inputFromClient[finalI].readLine();
System.out.println("from client : "+fromClient);
outputToClient[finalI].println(players+";-1;pls wait for your turn;"+score.getScore());
System.out.println("client "+finalI+" is waiting still and has been sent a wait message");
}
}catch (IOException e){
System.out.println("Error in wait thread");
}
}
});
t[i].start();
}
System.out.println("All clients sleeping and waiting for their turns");
System.out.println("Ready going in while");
//now the game starts
while (true) {
System.out.println("in while true");
for (int i = 0; i < players; i++) {
System.out.println(" player no " + i + "is playing now");
//stopping the player whose turn it is and asking the question
t[i].stop();
// System.out.println("player has been interrupted " + i);
String[] s = ht1.keySet().toArray(new String[0]);
String question = s[new Random().nextInt(s.length)];
//sending the question to all clients
//question has the number of the client who is supposed to answer
System.out.println("sending message to all clients ");
System.out.println("message is " + question);
for (int j = 0; j < players; j++) {
outputToClient[j].println(players + ";-1;Question for player no " + i + " is : " + question + ";" + score.getScore());
System.out.println("message sent to client " + j);
}
System.out.println("all clients got messages");
String fromClient = inputFromClient[i].readLine();
System.out.println("Message from client : "+fromClient);
// String fromClient = inputFromClient.readLine();
// inputFromClient.close();
System.out.println("reply is " + fromClient);
//sending the clients reply to all clients
System.out.println("sending the reply to all clients");
for (int j = 0; j < players; j++) {
outputToClient[j].println(players + ";" + i + ";" + fromClient + ";" + score.getScore());
}
int answer = ht1.get(question);
//correct answer then add a point
System.out.println("checking answer");
if (answer == Integer.parseInt(fromClient)) {
System.out.println("correct");
score.updateScore(i);
for (int j = 0; j < players; j++) {
outputToClient[j].println(players + ";-1;Player no "+i+" is correct;" + score.getScore());
}
} else {
System.out.println("wrong");
for (int j = 0; j < players; j++) {
outputToClient[j].println(players + ";-1;Player no "+i+" is wrong;" + score.getScore());
}
}
//again putting the thread to sleep till it is its turn
int finalI = i;
t[i]= new Thread(new Runnable() {
@Override
public void run() {
try {
while(true){
String fromClient = inputFromClient[finalI].readLine();
System.out.println("from client : "+fromClient);
outputToClient[finalI].println(players+";-1;pls wait for your turn;"+score.getScore());
}
}catch (IOException e){
System.out.println("Error in wait thread");
}
}
});
t[i].start();
}
}
} catch (IOException e) {
System.out.println("Error in main");
e.printStackTrace();
}
}
static class Score {
int[] score;
int players;
public Score(int players) {
this.players = players;
this.score = new int[players];
System.out.println("in the score constructor");
for (int i = 0; i < players; i++) {
System.out.println("0 assigned to player no " + i);
this.score[i] = 0;
System.out.println("0 assigned to player no " + i);
}
}
public void updateScore(int player) {
score[player]++;
}
public String getScore() {
String s = "";
for (int i = 0; i < players; i++) {
s = s + score[i] + ";";
}
return s;
}
}
}