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

質問編集履歴

2

内容の充実化

2021/06/29 06:07

投稿

akaevaka
akaevaka

スコア8

title CHANGED
File without changes
body CHANGED
@@ -15,37 +15,6 @@
15
15
  import java.io.*;
16
16
 
17
17
  public class Requester extends Worker {
18
-
19
- public void requestGameData(GameData gameData){
20
- try {
21
- InetAddress addr = InetAddress.getByName("localhost"); // change for IPAddress
22
- Socket socket = new Socket(addr, 8080); // set port No.
23
- ObjectInputStream objectInput = new ObjectInputStream(socket.getInputStream());
24
- //set questions
25
- Object object = objectInput.readObject();
26
- gameData.setQstrings((ArrayList<String>) object);
27
- //set waiting time
28
- Object object2 = objectInput.readObject();
29
- gameData.setTimeList((ArrayList<Long>) object2);
30
- socket.close();
31
- }catch (Exception e){
32
- e.printStackTrace();
33
- }
34
- }
35
-
36
- public void sendDifficulty(Player player){
37
- try{
38
- String mode = String.valueOf(player.getMode());
39
- InetAddress addr = InetAddress.getByName("localhost"); // change for IP address
40
- Socket socket = new Socket(addr, 8080); //set port No.
41
- PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
42
- out.println(mode); //send mode
43
- socket.close();
44
- } catch (Exception e) {
45
- e.printStackTrace();
46
- }
47
- }
48
-
49
18
  //send score to server, show ranking
50
19
  public void sendScore(Player player){
51
20
  try {
@@ -107,65 +76,10 @@
107
76
  import java.util.Map.Entry;
108
77
  import java.io.*;
109
78
 
110
- //EASY = 1;
111
- //HARD = 2;
112
79
 
113
80
  public class Responser{
114
81
  private static ArrayList<String> Qstring = new ArrayList<>(); //question
115
82
  private static ArrayList<Long> timeList = new ArrayList<>(); //waiting list
116
-
117
- public int loadDifficulty() throws IOException{
118
- ServerSocket s = new ServerSocket(8080); // make socket
119
- Socket socket = s.accept(); // wait setting demand of connection
120
- BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
121
- s.close();
122
- return Integer.parseInt(in.readLine()); //accept difficulty
123
- }
124
-
125
- //load Qstringsdata from arraylist to file
126
- public void loadQstrings(String QuestionFilePath) throws FileNotFoundException {
127
- Responser.Qstring.add("");
128
- Scanner s = new Scanner(new File(QuestionFilePath));
129
- Responser.Qstring.clear();
130
- while (s.hasNext()){
131
- Responser.Qstring.add(s.nextLine());
132
- }
133
- s.close();
134
- }
135
-
136
- //load timelist from arraylist to file
137
- public void loadTimeList(String timeFilePath) throws FileNotFoundException {
138
- Responser.timeList.add(0L);
139
- Scanner s = new Scanner(new File(timeFilePath));
140
- Responser.timeList.clear();
141
- while (s.hasNext()){
142
- Responser.timeList.add(s.nextLong());
143
- }
144
- s.close();
145
- }
146
-
147
- //send gamedata in response to client requests
148
- public void sendGameData() throws IOException {
149
- try {
150
- ServerSocket s = new ServerSocket(8080); // make socket
151
- Socket socket = s.accept(); //wait setting demand of connection
152
-
153
- try {
154
- ObjectOutputStream objectOutput = new ObjectOutputStream(socket.getOutputStream());
155
- //send question
156
- objectOutput.writeObject(Responser.Qstring);
157
- //send timelist
158
- objectOutput.writeObject(Responser.timeList);
159
- } catch (IOException e) {
160
- e.printStackTrace();
161
- }
162
- s.close();
163
- } catch (Exception e) {
164
- e.printStackTrace();
165
- }
166
-
167
- }
168
-
169
83
 
170
84
  //accept score and register it to ranking
171
85
  public void updateRanking() throws IOException {
@@ -237,4 +151,96 @@
237
151
  }
238
152
 
239
153
  }
240
- ```
154
+ ```
155
+
156
+
157
+ ###ClientMain
158
+ ```Java
159
+ import java.io.*;
160
+ import java.util.*;
161
+
162
+ public class ClientMain {
163
+ public static void main(String[] args) throws InterruptedException{
164
+ Player player = new Player("", 0, 0); //(name, score, mdoe)
165
+ GameData gameData = new GameData(player);
166
+ ClientSystem clientsystem = new ClientSystem();
167
+ Requester requester = new Requester();
168
+
169
+ clientsystem.opening(); //op
170
+
171
+ clientsystem.say("Choose difficulty from easy:1 or hard:2.");
172
+ clientsystem.say("Input the difficulty number.");
173
+ Scanner scan = new Scanner(System.in);
174
+ int difficulty = scan.nextInt();
175
+
176
+ player.setMode(difficulty);
177
+ requester.sendDifficulty(player);
178
+
179
+ clientsystem.say("send difficulty");
180
+ clientsystem.say("gamedata download...");
181
+ Thread.sleep(1000);
182
+
183
+ requester.requestGameData(gameData); //download gamedata from server
184
+ clientsystem.say("Load Successes!");
185
+ Thread.sleep(2000);
186
+
187
+ clientsystem.startGame(gameData); //gamestart
188
+ System.out.println();
189
+ clientsystem.say("Finish");
190
+
191
+ clientsystem.showScore(player); //show score
192
+
193
+
194
+ clientsystem.say("Please input your name.");
195
+
196
+ String playerName = scan.nextLine();
197
+ scan.close();
198
+ player.setName(playerName);
199
+
200
+ requester.sendScore(player);
201
+
202
+ clientsystem.say("See you!");
203
+ }
204
+ }
205
+ ```
206
+
207
+
208
+
209
+ ###実行本文
210
+ ```ここに言語を入力
211
+ [Welcome to this TYPING GAME.]
212
+ [When this system says something, uses [] like this message]
213
+ [--rule--]
214
+ [Type the message enclosed in {}.]
215
+ [The next is a example.]
216
+ Q.{ もんだいぶん }
217
+ [if you misstype and enter without modifying, subtract your score.]
218
+ [There are two difficulty levels]
219
+ [Choose difficulty from easy:1 or hard:2.]
220
+ [Input the difficulty number.]
221
+ 1
222
+ [send difficulty]
223
+ [gamedata download...]
224
+ [Load Successes!]
225
+ [Are you ready?]
226
+ [START!]
227
+ 0
228
+
229
+ [Finish]
230
+ [Score: 0]
231
+ [Please input your name.]
232
+ --- Rank | Player | Score ---
233
+ java.net.SocketException: Connection reset
234
+ すでに載せたので文字数の都合上割愛
235
+ [See you!]
236
+ PS C:\Users\Client>
237
+ ```
238
+
239
+ ###原因の推定
240
+ 名前の入力をまたずに
241
+ --- Rank |
242
+ というものが出力されてしまっているので,
243
+ 名前の入力をしようとしたときにupdateRankingがもう始まってしまっているのが原因だと考えられます.
244
+ 本来ならこのupdateはsendScoreを待たなければいけないのに先に終わらせてしまっているからこのような例がでたと考えております.
245
+ updateRankinggがしっかりと待機するようにするにはどうすればよいですか
246
+ 文字数の都合上,必要ないと判断した関数は省略しました

1

内容の充実化

2021/06/29 06:07

投稿

akaevaka
akaevaka

スコア8

title CHANGED
File without changes
body CHANGED
@@ -94,4 +94,147 @@
94
94
  at java.base/java.io.BufferedReader.readLine(BufferedReader.java:396)
95
95
  at Requester.sendScore(Requester.java:55)
96
96
  at ClientMain.main(ClientMain.java:57)
97
+ ```
98
+
99
+
100
+ ###追記
101
+ Server側
102
+ updateRankingでスコアを受け取っています.
103
+
104
+ ```Java
105
+ import java.net.*;
106
+ import java.util.*;
107
+ import java.util.Map.Entry;
108
+ import java.io.*;
109
+
110
+ //EASY = 1;
111
+ //HARD = 2;
112
+
113
+ public class Responser{
114
+ private static ArrayList<String> Qstring = new ArrayList<>(); //question
115
+ private static ArrayList<Long> timeList = new ArrayList<>(); //waiting list
116
+
117
+ public int loadDifficulty() throws IOException{
118
+ ServerSocket s = new ServerSocket(8080); // make socket
119
+ Socket socket = s.accept(); // wait setting demand of connection
120
+ BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
121
+ s.close();
122
+ return Integer.parseInt(in.readLine()); //accept difficulty
123
+ }
124
+
125
+ //load Qstringsdata from arraylist to file
126
+ public void loadQstrings(String QuestionFilePath) throws FileNotFoundException {
127
+ Responser.Qstring.add("");
128
+ Scanner s = new Scanner(new File(QuestionFilePath));
129
+ Responser.Qstring.clear();
130
+ while (s.hasNext()){
131
+ Responser.Qstring.add(s.nextLine());
132
+ }
133
+ s.close();
134
+ }
135
+
136
+ //load timelist from arraylist to file
137
+ public void loadTimeList(String timeFilePath) throws FileNotFoundException {
138
+ Responser.timeList.add(0L);
139
+ Scanner s = new Scanner(new File(timeFilePath));
140
+ Responser.timeList.clear();
141
+ while (s.hasNext()){
142
+ Responser.timeList.add(s.nextLong());
143
+ }
144
+ s.close();
145
+ }
146
+
147
+ //send gamedata in response to client requests
148
+ public void sendGameData() throws IOException {
149
+ try {
150
+ ServerSocket s = new ServerSocket(8080); // make socket
151
+ Socket socket = s.accept(); //wait setting demand of connection
152
+
153
+ try {
154
+ ObjectOutputStream objectOutput = new ObjectOutputStream(socket.getOutputStream());
155
+ //send question
156
+ objectOutput.writeObject(Responser.Qstring);
157
+ //send timelist
158
+ objectOutput.writeObject(Responser.timeList);
159
+ } catch (IOException e) {
160
+ e.printStackTrace();
161
+ }
162
+ s.close();
163
+ } catch (Exception e) {
164
+ e.printStackTrace();
165
+ }
166
+
167
+ }
168
+
169
+
170
+ //accept score and register it to ranking
171
+ public void updateRanking() throws IOException {
172
+ ServerSocket s = new ServerSocket(8080); // make socket
173
+ Socket socket = s.accept(); // wait setting demand of connection
174
+ BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
175
+ String p_name = in.readLine(); //accept name
176
+ String p_score = in.readLine(); //accept score
177
+ int p_mode = Integer.parseInt(in.readLine()); //accept mode
178
+
179
+ String rankingFilePath;
180
+ if(p_mode == 1) rankingFilePath = "data/ranking/easyranking.txt";
181
+ else rankingFilePath = "data/ranking/hardranking.txt";
182
+
183
+ //key=ID, value=score
184
+ Map<Integer,Integer> scores = new HashMap<Integer,Integer>();
185
+ ArrayList<String> playerNameList = new ArrayList<String>();
186
+
187
+ Scanner s2 = new Scanner(new File(rankingFilePath));
188
+ int count = 1;
189
+
190
+ int id = -1, score = -1;
191
+ String name = "";
192
+ while (s2.hasNext()){
193
+ if(count % 3 == 1){
194
+ id = Integer.parseInt(s2.next());
195
+ }else if(count % 3 == 2){
196
+ score = Integer.parseInt(s2.next());
197
+ }else{
198
+ name = s2.next();
199
+ scores.put(id,score);
200
+ playerNameList.add(name);
201
+ }
202
+ count++;
203
+ }
204
+
205
+ try{
206
+ int nextId = playerNameList.size()+1;
207
+ File file = new File(rankingFilePath);
208
+ FileWriter filewriter = new FileWriter(file, true);
209
+ filewriter.write(nextId + " " + p_score + " " + p_name + "\n");
210
+ filewriter.close();
211
+ scores.put(nextId, Integer.parseInt(p_score));
212
+ playerNameList.add(p_name);
213
+ }catch(IOException e){
214
+ System.out.println(e);
215
+ }
216
+
217
+ // make entry list of scorelist
218
+ List<Entry<Integer, Integer>> list_entries = new ArrayList<Entry<Integer, Integer>>(scores.entrySet());
219
+
220
+ //sort scorelist
221
+ Collections.sort(list_entries, new Comparator<Entry<Integer, Integer>>() {
222
+ public int compare(Entry<Integer, Integer> obj1, Entry<Integer, Integer> obj2) {
223
+ return obj2.getValue().compareTo(obj1.getValue());
224
+ }
225
+ });
226
+
227
+
228
+ PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); //setting of sending buffer
229
+
230
+ //send ranking data to client
231
+ String[] ordinalNumber = {"1st", "2nd", "3rd", "4th", "5th"};
232
+ out.println("--- Rank | Player | Score ---");
233
+ for(int i=0; i<5; i++){
234
+ out.println(ordinalNumber[i] + ", " + playerNameList.get(list_entries.get(i).getKey()-1) + ", " + list_entries.get(i).getValue());
235
+ }
236
+ s.close();
237
+ }
238
+
239
+ }
97
240
  ```