-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestPrepServer.java
More file actions
263 lines (189 loc) · 5.46 KB
/
TestPrepServer.java
File metadata and controls
263 lines (189 loc) · 5.46 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
253
254
255
256
257
258
259
260
261
262
263
import java.io.*;
import java.net.*;
import java.util.*;
/*
* Ava Zhang
* Period 6
*
* TestPrepServer - server
*
* Maintains the file of all test questions. Runs on port 4242 and accepts clients forever.
* Whenever a client connects, the server will spawn a new thread to interact with them.
* Maintains the highest score + person with the highest score.
*/
public class TestPrepServer {
private AllQuestions test;
private String highestPlayer;
private int numWinners;
private int highestScore;
public TestPrepServer(){
test = new AllQuestions("questions.txt");
if(test.size() == 0){
System.out.println("There are no Questions in this text");
System.exit(-1);
}
highestScore = Integer.MIN_VALUE;
highestPlayer = "";
//creates and launches the server socket
try {
System.out.println("Server: ");
ServerSocket server = new ServerSocket(4242);
System.out.println(server.getLocalPort());
System.out.println(InetAddress.getLocalHost().getHostAddress());
//spawns new Client Handler threads forever
while(true) {
Thread newPlayer = new Thread(new ClientHandler(server.accept()));
newPlayer.start();
}
}catch(IOException e) {
e.printStackTrace();
}
}
/*
* ClientHandler - inner class
*
* handles the client that connects to the server
* provides answer selections and questions from the test
* informs the client whether they chose correctly or not
*/
private class ClientHandler implements Runnable{
private Scanner in;
private PrintWriter out;
//creates a thread that handles a client
public ClientHandler(Socket s){
try {
in = new Scanner(s.getInputStream());
out = new PrintWriter(s.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
System.exit(2);
}
}
//facilitates the test prep "game"
//sends questions, receives answers, sends back results
public void run(){
int numCorrect = 0;
String name = in.nextLine();
//sends out the size of the test
out.println(test.size());
out.flush();
for(int i = 0; i < test.size(); i++) {
//sends out the question + answer choices
out.println(test.get(i).question);
for(String ans: test.get(i).answers)
out.println(ans);
out.flush();
//takes in the user's choice
String choice = in.nextLine();
String message = "Nope.";
if(Integer.parseInt(choice) == test.get(i).correctAns) {
message = "Correct!";
numCorrect++;
}
//sends out the pop-up message and the current test number
out.println(message);
out.println(numCorrect);
out.println(i+1);
out.flush();
}
String message;
//makes sure that a player isn't set as the highest player if
//this thread is put to sleep while another thread has a player that scored higher
synchronized(highestPlayer) {
if(numCorrect > highestScore) {
highestScore = numCorrect;
highestPlayer = name + " has ";
numWinners = 1;
}
else if(numCorrect == highestScore) {
numWinners++;
highestPlayer = numWinners + " players have ";
}
message = highestPlayer+ "the high score with " +highestScore+ " correct.";
}
out.println(message);
out.flush();
}
}
/*
* AllQuestions - inner ArrayList of Questions
* Takes in the file "questions.txt" to create a test. Assumes that the file is in the correct format
*/
public class AllQuestions extends ArrayList<Question> {
public AllQuestions(String fName){
Scanner fileIn = null;
//looking for the file
try{
fileIn = new Scanner(new File(fName));
}catch(FileNotFoundException e){
System.out.println("Can't find file");
System.exit(-1);
}
//reading in the file to create the object
while(fileIn.hasNextLine()){
String q = fileIn.nextLine();
ArrayList<String> a = new ArrayList<String>();
for(int i = 0; i < 4; i++)
a.add(fileIn.nextLine());
add(new Question(q, a, fileIn.nextInt()));
if(fileIn.hasNextLine())
fileIn.nextLine();
}
shuffle();
}
//shuffles the order of questions [0, 50] times
private void shuffle(){
int numRepeats = (int)(Math.random()*51);
for(int i = 0; i < numRepeats; i++){
int ranIndex = (int)(Math.random()*size());
add(remove(ranIndex));
}
}
}
/*
* Question - inner class
* Contains a test question, 4 possible answer choices and an int representing the index of the correct answer
*/
public class Question {
private String question;
private ArrayList<String> answers;
private int correctAns;
public Question(String q, ArrayList<String> a, int c){
if(c < 0 || c >= a.size()){
throw new IllegalArgumentException("Out of bounds answer");
}
question = q;
correctAns = c;
answers = new ArrayList<String>();
for(String ans: a)
answers.add(ans);
shuffle();
}
//shuffles 4 possible answers a random [0,50] number of times
private void shuffle(){
int numRepeats = (int)(Math.random()*51);
for(int i = 0; i <= numRepeats; i++){
int ranIndex = (int)(Math.random()*4);
if(correctAns == ranIndex)
correctAns = 3;
else if(ranIndex < correctAns)
correctAns--;
answers.add(answers.get(ranIndex));
answers.remove(ranIndex);
}
}
public String getQuestion(){
return question;
}
public ArrayList<String> getAnswerChoices(){
return answers;
}
public int getCorrectAns(){
return correctAns;
}
}
//starts the server
public static void main(String[] args){
new TestPrepServer();
}
}