teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

内容の充実化

2021/07/01 02:16

投稿

akaevaka
akaevaka

スコア8

title CHANGED
File without changes
body CHANGED
@@ -109,4 +109,165 @@
109
109
  ```
110
110
  hard.txt
111
111
  0
112
+ ```
113
+
114
+ ###Responser.java
115
+ ```Java
116
+ import java.net.*;
117
+ import java.util.*;
118
+ import java.util.Map.Entry;
119
+ import java.io.*;
120
+
121
+ //loadlyrycslist->loadQstrings
122
+ //lyricslist -> Qstring
123
+ //lylicsFilePat -> QuestionFilePath
124
+
125
+ //EASY = 1;
126
+ //HARD = 2;
127
+
128
+ public class Responser{
129
+ private static ArrayList<String> Qstring = new ArrayList<>(); //question
130
+ private static ArrayList<Long> timeList = new ArrayList<>(); //waiting list
131
+
132
+ public int loadDifficulty() throws IOException{
133
+ ServerSocket s = new ServerSocket(8080); // make socket
134
+ Socket socket = s.accept(); // wait setting demand of connection
135
+ BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
136
+ s.close();
137
+ return Integer.parseInt(in.readLine()); //accept difficulty
138
+ }
139
+
140
+ //load Qstringsdata from arraylist to file
141
+ public void loadQstrings(String QuestionFilePath) throws FileNotFoundException {
142
+ System.out.println(QuestionFilePath);
143
+ Responser.Qstring.add("");
144
+ Scanner s = new Scanner(new File(QuestionFilePath));
145
+ Responser.Qstring.clear();
146
+ while (s.hasNext()){
147
+ System.out.println("add");
148
+ Responser.Qstring.add(s.nextLine());
149
+ }
150
+ s.close();
151
+ }
152
+
153
+ //load timelist from arraylist to file
154
+ public void loadTimeList(String timeFilePath) throws FileNotFoundException {
155
+ Responser.timeList.add(0L);
156
+ Scanner s = new Scanner(new File(timeFilePath));
157
+ Responser.timeList.clear();
158
+ while (s.hasNext()){
159
+ Responser.timeList.add(s.nextLong());
160
+ }
161
+ s.close();
162
+ }
163
+
164
+ //send gamedata in response to client requests
165
+ public void sendGameData() throws IOException {
166
+ try {
167
+ ServerSocket s = new ServerSocket(8080); // make socket
168
+ Socket socket = s.accept(); //wait setting demand of connection
169
+
170
+ try {
171
+ ObjectOutputStream objectOutput = new ObjectOutputStream(socket.getOutputStream());
172
+ //send question
173
+ objectOutput.writeObject(Responser.Qstring);
174
+
175
+ System.out.println(Responser.Qstring.size());
176
+ for(int i = 0; i < Responser.Qstring.size(); i++){
177
+ System.out.println(Responser.Qstring.get(i));
178
+ }
179
+
180
+ //send timelist
181
+ objectOutput.writeObject(Responser.timeList);
182
+ } catch (IOException e) {
183
+ e.printStackTrace();
184
+ }
185
+ s.close();
186
+ } catch (Exception e) {
187
+ e.printStackTrace();
188
+ }
189
+
190
+ }
191
+
192
+
193
+ //accept score and register it to ranking
194
+ public void updateRanking() throws IOException {
195
+ ServerSocket s = new ServerSocket(8080); // make socket
196
+ Socket socket = s.accept(); // wait setting demand of connection
197
+ BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
198
+ String p_name = in.readLine(); //accept name
199
+ String p_score = in.readLine(); //accept score
200
+ int p_mode = Integer.parseInt(in.readLine()); //accept mode
201
+
202
+ //System.out.println(p_mode);
203
+
204
+ String rankingFilePath;
205
+ if(p_mode == 1) rankingFilePath = "data/ranking/easyranking.txt";
206
+ else rankingFilePath = "data/ranking/hardranking.txt";
207
+
208
+ //key=ID, value=score
209
+ Map<Integer,Integer> scores = new HashMap<Integer,Integer>();
210
+ ArrayList<String> playerNameList = new ArrayList<String>();
211
+
212
+ Scanner s2 = new Scanner(new File(rankingFilePath));
213
+ int count = 1;
214
+
215
+ int id = -1, score = -1;
216
+ String name = "";
217
+ while (s2.hasNext()){
218
+ if(count % 3 == 1){
219
+ id = Integer.parseInt(s2.next());
220
+ }else if(count % 3 == 2){
221
+ score = Integer.parseInt(s2.next());
222
+ }else{
223
+ name = s2.next();
224
+ scores.put(id,score);
225
+ playerNameList.add(name);
226
+ }
227
+ count++;
228
+ }
229
+
230
+ try{
231
+ int nextId = playerNameList.size()+1;
232
+ File file = new File(rankingFilePath);
233
+ FileWriter filewriter = new FileWriter(file, true);
234
+ filewriter.write(nextId + " " + p_score + " " + p_name + "\n");
235
+ filewriter.close();
236
+ scores.put(nextId, Integer.parseInt(p_score));
237
+ playerNameList.add(p_name);
238
+ }catch(IOException e){
239
+ System.out.println(e);
240
+ }
241
+
242
+ // make entry list of scorelist
243
+ List<Entry<Integer, Integer>> list_entries = new ArrayList<Entry<Integer, Integer>>(scores.entrySet());
244
+
245
+ System.out.println("Sort start");
246
+ //sort scorelist
247
+ Collections.sort(list_entries, new Comparator<Entry<Integer, Integer>>() {
248
+ public int compare(Entry<Integer, Integer> obj1, Entry<Integer, Integer> obj2) {
249
+ return obj2.getValue().compareTo(obj1.getValue());
250
+ }
251
+ });
252
+
253
+
254
+ PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); //setting of sending buffer
255
+
256
+ //send ranking data to client
257
+ String[] ordinalNumber = {" 1st", " 2nd", " 3rd", " 4th", " 5th"};
258
+ out.println("--- Rank | Player | Score ---");
259
+ for(int i=0; i<5; i++){
260
+ out.println(ordinalNumber[i] + " " + playerNameList.get(list_entries.get(i).getKey()-1) + "\t " + list_entries.get(i).getValue());
261
+ //System.out.println(ordinalNumber[i] + ", " + playerNameList.get(list_entries.get(i).getKey()-1) + ", " + list_entries.get(i).getValue());
262
+ }
263
+ try{
264
+ Thread.sleep(1000);
265
+ }catch (Exception e){
266
+ e.printStackTrace();
267
+ }
268
+ s.close();
269
+ socket.close();
270
+ }
271
+
272
+ }
112
273
  ```

2

内容の充実化

2021/07/01 02:15

投稿

akaevaka
akaevaka

スコア8

title CHANGED
File without changes
body CHANGED
@@ -77,4 +77,36 @@
77
77
  やはりおれのせいしゅんらぶこめはまちがっている
78
78
  ゆうきゆうなはゆうしゃである
79
79
  わんだーえっぐぷらいおりてぃ
80
+ ```
81
+
82
+
83
+ ###追記
84
+ パスでの指定が駄目なのかと考え.ディレクトリ構造を次のようにし,以下のようにコードを変更したのですが改善しませんでした.
85
+
86
+ ![イメージ説明](3fdf7331cdb4fd4b3ae9c4229b386013.png)
87
+
88
+ 呼び出し
89
+ ```java
90
+ responser.loadQstrings("hard.txt");
91
+ ```
92
+
93
+ 読み込み
94
+ ```Java
95
+ public void loadQstrings(String QuestionFilePath) throws FileNotFoundException {
96
+ System.out.println(QuestionFilePath);
97
+ Responser.Qstring.add("");
98
+ Scanner s = new Scanner(new File(QuestionFilePath));
99
+ Responser.Qstring.clear();
100
+ while (s.hasNext()){
101
+ System.out.println("add");
102
+ Responser.Qstring.add(s.nextLine());
103
+ }
104
+ s.close();
105
+ }
106
+ ```
107
+
108
+ 結果
109
+ ```
110
+ hard.txt
111
+ 0
80
112
  ```

1

内容の充実化

2021/06/30 12:13

投稿

akaevaka
akaevaka

スコア8

title CHANGED
File without changes
body CHANGED
@@ -20,7 +20,7 @@
20
20
 
21
21
  ###関数の呼び出し
22
22
  ```Java
23
- responser.loadQstrings("data/questions/easy.txt");
23
+ responser.loadQstrings("data/questions/hard.txt");
24
24
  ```
25
25
 
26
26
  ###ディレクトリ
@@ -63,4 +63,18 @@
63
63
  ```ここに言語を入力
64
64
  data/questions/hard.txt
65
65
  0
66
+ ```
67
+
68
+ ###hard.txt
69
+ ```ここに言語を入力
70
+ まおうがくいんのふてきごうしゃ
71
+ とあるまじゅつのいんでっくす
72
+ いたいのはいやなのでぼうぎょりょくにきょくふりしたいとおもいます
73
+ せいしゅうぶたやろうはばにーがーるせんぱいのゆめをみない
74
+ そーどあーとおんらいんおるたなてぃぶがんげいるおんらいん
75
+ にじがさきがくえんすくーるあいどるどうこうかい
76
+ まほうかこうこうのれっとうせい
77
+ やはりおれのせいしゅんらぶこめはまちがっている
78
+ ゆうきゆうなはゆうしゃである
79
+ わんだーえっぐぷらいおりてぃ
66
80
  ```