質問編集履歴

3

内容の充実化

2021/07/01 02:16

投稿

akaevaka
akaevaka

スコア8

test CHANGED
File without changes
test CHANGED
@@ -221,3 +221,325 @@
221
221
  0
222
222
 
223
223
  ```
224
+
225
+
226
+
227
+ ###Responser.java
228
+
229
+ ```Java
230
+
231
+ import java.net.*;
232
+
233
+ import java.util.*;
234
+
235
+ import java.util.Map.Entry;
236
+
237
+ import java.io.*;
238
+
239
+
240
+
241
+ //loadlyrycslist->loadQstrings
242
+
243
+ //lyricslist -> Qstring
244
+
245
+ //lylicsFilePat -> QuestionFilePath
246
+
247
+
248
+
249
+ //EASY = 1;
250
+
251
+ //HARD = 2;
252
+
253
+
254
+
255
+ public class Responser{
256
+
257
+ private static ArrayList<String> Qstring = new ArrayList<>(); //question
258
+
259
+ private static ArrayList<Long> timeList = new ArrayList<>(); //waiting list
260
+
261
+
262
+
263
+ public int loadDifficulty() throws IOException{
264
+
265
+ ServerSocket s = new ServerSocket(8080); // make socket
266
+
267
+ Socket socket = s.accept(); // wait setting demand of connection
268
+
269
+ BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
270
+
271
+ s.close();
272
+
273
+ return Integer.parseInt(in.readLine()); //accept difficulty
274
+
275
+ }
276
+
277
+
278
+
279
+ //load Qstringsdata from arraylist to file
280
+
281
+ public void loadQstrings(String QuestionFilePath) throws FileNotFoundException {
282
+
283
+ System.out.println(QuestionFilePath);
284
+
285
+ Responser.Qstring.add("");
286
+
287
+ Scanner s = new Scanner(new File(QuestionFilePath));
288
+
289
+ Responser.Qstring.clear();
290
+
291
+ while (s.hasNext()){
292
+
293
+ System.out.println("add");
294
+
295
+ Responser.Qstring.add(s.nextLine());
296
+
297
+ }
298
+
299
+ s.close();
300
+
301
+ }
302
+
303
+
304
+
305
+ //load timelist from arraylist to file
306
+
307
+ public void loadTimeList(String timeFilePath) throws FileNotFoundException {
308
+
309
+ Responser.timeList.add(0L);
310
+
311
+ Scanner s = new Scanner(new File(timeFilePath));
312
+
313
+ Responser.timeList.clear();
314
+
315
+ while (s.hasNext()){
316
+
317
+ Responser.timeList.add(s.nextLong());
318
+
319
+ }
320
+
321
+ s.close();
322
+
323
+ }
324
+
325
+
326
+
327
+ //send gamedata in response to client requests
328
+
329
+ public void sendGameData() throws IOException {
330
+
331
+ try {
332
+
333
+ ServerSocket s = new ServerSocket(8080); // make socket
334
+
335
+ Socket socket = s.accept(); //wait setting demand of connection
336
+
337
+
338
+
339
+ try {
340
+
341
+ ObjectOutputStream objectOutput = new ObjectOutputStream(socket.getOutputStream());
342
+
343
+ //send question
344
+
345
+ objectOutput.writeObject(Responser.Qstring);
346
+
347
+
348
+
349
+ System.out.println(Responser.Qstring.size());
350
+
351
+ for(int i = 0; i < Responser.Qstring.size(); i++){
352
+
353
+ System.out.println(Responser.Qstring.get(i));
354
+
355
+ }
356
+
357
+
358
+
359
+ //send timelist
360
+
361
+ objectOutput.writeObject(Responser.timeList);
362
+
363
+ } catch (IOException e) {
364
+
365
+ e.printStackTrace();
366
+
367
+ }
368
+
369
+ s.close();
370
+
371
+ } catch (Exception e) {
372
+
373
+ e.printStackTrace();
374
+
375
+ }
376
+
377
+
378
+
379
+ }
380
+
381
+
382
+
383
+
384
+
385
+ //accept score and register it to ranking
386
+
387
+ public void updateRanking() throws IOException {
388
+
389
+ ServerSocket s = new ServerSocket(8080); // make socket
390
+
391
+ Socket socket = s.accept(); // wait setting demand of connection
392
+
393
+ BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
394
+
395
+ String p_name = in.readLine(); //accept name
396
+
397
+ String p_score = in.readLine(); //accept score
398
+
399
+ int p_mode = Integer.parseInt(in.readLine()); //accept mode
400
+
401
+
402
+
403
+ //System.out.println(p_mode);
404
+
405
+
406
+
407
+ String rankingFilePath;
408
+
409
+ if(p_mode == 1) rankingFilePath = "data/ranking/easyranking.txt";
410
+
411
+ else rankingFilePath = "data/ranking/hardranking.txt";
412
+
413
+
414
+
415
+ //key=ID, value=score
416
+
417
+ Map<Integer,Integer> scores = new HashMap<Integer,Integer>();
418
+
419
+ ArrayList<String> playerNameList = new ArrayList<String>();
420
+
421
+
422
+
423
+ Scanner s2 = new Scanner(new File(rankingFilePath));
424
+
425
+ int count = 1;
426
+
427
+
428
+
429
+ int id = -1, score = -1;
430
+
431
+ String name = "";
432
+
433
+ while (s2.hasNext()){
434
+
435
+ if(count % 3 == 1){
436
+
437
+ id = Integer.parseInt(s2.next());
438
+
439
+ }else if(count % 3 == 2){
440
+
441
+ score = Integer.parseInt(s2.next());
442
+
443
+ }else{
444
+
445
+ name = s2.next();
446
+
447
+ scores.put(id,score);
448
+
449
+ playerNameList.add(name);
450
+
451
+ }
452
+
453
+ count++;
454
+
455
+ }
456
+
457
+
458
+
459
+ try{
460
+
461
+ int nextId = playerNameList.size()+1;
462
+
463
+ File file = new File(rankingFilePath);
464
+
465
+ FileWriter filewriter = new FileWriter(file, true);
466
+
467
+ filewriter.write(nextId + " " + p_score + " " + p_name + "\n");
468
+
469
+ filewriter.close();
470
+
471
+ scores.put(nextId, Integer.parseInt(p_score));
472
+
473
+ playerNameList.add(p_name);
474
+
475
+ }catch(IOException e){
476
+
477
+ System.out.println(e);
478
+
479
+ }
480
+
481
+
482
+
483
+ // make entry list of scorelist
484
+
485
+ List<Entry<Integer, Integer>> list_entries = new ArrayList<Entry<Integer, Integer>>(scores.entrySet());
486
+
487
+
488
+
489
+ System.out.println("Sort start");
490
+
491
+ //sort scorelist
492
+
493
+ Collections.sort(list_entries, new Comparator<Entry<Integer, Integer>>() {
494
+
495
+ public int compare(Entry<Integer, Integer> obj1, Entry<Integer, Integer> obj2) {
496
+
497
+ return obj2.getValue().compareTo(obj1.getValue());
498
+
499
+ }
500
+
501
+ });
502
+
503
+
504
+
505
+
506
+
507
+ PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); //setting of sending buffer
508
+
509
+
510
+
511
+ //send ranking data to client
512
+
513
+ String[] ordinalNumber = {" 1st", " 2nd", " 3rd", " 4th", " 5th"};
514
+
515
+ out.println("--- Rank | Player | Score ---");
516
+
517
+ for(int i=0; i<5; i++){
518
+
519
+ out.println(ordinalNumber[i] + " " + playerNameList.get(list_entries.get(i).getKey()-1) + "\t " + list_entries.get(i).getValue());
520
+
521
+ //System.out.println(ordinalNumber[i] + ", " + playerNameList.get(list_entries.get(i).getKey()-1) + ", " + list_entries.get(i).getValue());
522
+
523
+ }
524
+
525
+ try{
526
+
527
+ Thread.sleep(1000);
528
+
529
+ }catch (Exception e){
530
+
531
+ e.printStackTrace();
532
+
533
+ }
534
+
535
+ s.close();
536
+
537
+ socket.close();
538
+
539
+ }
540
+
541
+
542
+
543
+ }
544
+
545
+ ```

2

内容の充実化

2021/07/01 02:15

投稿

akaevaka
akaevaka

スコア8

test CHANGED
File without changes
test CHANGED
@@ -157,3 +157,67 @@
157
157
  わんだーえっぐぷらいおりてぃ
158
158
 
159
159
  ```
160
+
161
+
162
+
163
+
164
+
165
+ ###追記
166
+
167
+ パスでの指定が駄目なのかと考え.ディレクトリ構造を次のようにし,以下のようにコードを変更したのですが改善しませんでした.
168
+
169
+
170
+
171
+ ![イメージ説明](3fdf7331cdb4fd4b3ae9c4229b386013.png)
172
+
173
+
174
+
175
+ 呼び出し
176
+
177
+ ```java
178
+
179
+ responser.loadQstrings("hard.txt");
180
+
181
+ ```
182
+
183
+
184
+
185
+ 読み込み
186
+
187
+ ```Java
188
+
189
+ public void loadQstrings(String QuestionFilePath) throws FileNotFoundException {
190
+
191
+ System.out.println(QuestionFilePath);
192
+
193
+ Responser.Qstring.add("");
194
+
195
+ Scanner s = new Scanner(new File(QuestionFilePath));
196
+
197
+ Responser.Qstring.clear();
198
+
199
+ while (s.hasNext()){
200
+
201
+ System.out.println("add");
202
+
203
+ Responser.Qstring.add(s.nextLine());
204
+
205
+ }
206
+
207
+ s.close();
208
+
209
+ }
210
+
211
+ ```
212
+
213
+
214
+
215
+ 結果
216
+
217
+ ```
218
+
219
+ hard.txt
220
+
221
+ 0
222
+
223
+ ```

1

内容の充実化

2021/06/30 12:13

投稿

akaevaka
akaevaka

スコア8

test CHANGED
File without changes
test CHANGED
@@ -42,7 +42,7 @@
42
42
 
43
43
  ```Java
44
44
 
45
- responser.loadQstrings("data/questions/easy.txt");
45
+ responser.loadQstrings("data/questions/hard.txt");
46
46
 
47
47
  ```
48
48
 
@@ -129,3 +129,31 @@
129
129
  0
130
130
 
131
131
  ```
132
+
133
+
134
+
135
+ ###hard.txt
136
+
137
+ ```ここに言語を入力
138
+
139
+ まおうがくいんのふてきごうしゃ
140
+
141
+ とあるまじゅつのいんでっくす
142
+
143
+ いたいのはいやなのでぼうぎょりょくにきょくふりしたいとおもいます
144
+
145
+ せいしゅうぶたやろうはばにーがーるせんぱいのゆめをみない
146
+
147
+ そーどあーとおんらいんおるたなてぃぶがんげいるおんらいん
148
+
149
+ にじがさきがくえんすくーるあいどるどうこうかい
150
+
151
+ まほうかこうこうのれっとうせい
152
+
153
+ やはりおれのせいしゅんらぶこめはまちがっている
154
+
155
+ ゆうきゆうなはゆうしゃである
156
+
157
+ わんだーえっぐぷらいおりてぃ
158
+
159
+ ```