-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
169 lines (149 loc) · 5.64 KB
/
Client.java
File metadata and controls
169 lines (149 loc) · 5.64 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
package practice1;
import java.util.Arrays;
import java.util.stream.Stream;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
public class practice {
private static final String SERVER_IP = "127.0.0.1"; //IP주소
public static void main(String[] args) {
Socket socket = null;
socket = new Socket(); //소켓 객체 생성
String SERVER_PORT = "";
Scanner scanner =new Scanner(System.in);
OutputStream os = null;
OutputStreamWriter osw = null;
BufferedWriter bw = null;
InputStream is =null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
System.out.print("port number: ");
SERVER_PORT = scanner.nextLine(); //포트 번호
//서버와 연결
socket.connect(new InetSocketAddress(SERVER_IP, Integer.parseInt(SERVER_PORT)));
//서버로 전송을 위한 OutputStream
os = socket.getOutputStream();
osw = new OutputStreamWriter(os);
bw = new BufferedWriter(osw);
//서버로부터 데이터를 받을 InputStream
is = socket.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
int num; //random 숫자 생성
int min;
int max;
int[] sortedAnswer;
int[] answerList;
Scanner in = new Scanner(System.in);
System.out.println("choice number of digits : ( 2,3,4 )");
int vNum = in.nextInt();
if( vNum == 2 ){
min = 10;
max = 100;
num = (int)((Math.random() * (max - min)) + min);
sortedAnswer = Stream.of(String.valueOf(num).split("")).mapToInt(Integer::parseInt).toArray(); //정답을 순서대로 나타내는 리스트
answerList = Stream.of(String.valueOf(num).split("")).mapToInt(Integer::parseInt).toArray(); //정답을 그대로 나타내는 리스트
Arrays.sort(sortedAnswer);
int count = play(num,sortedAnswer,answerList);
bw.write(count);
bw.newLine();
bw.flush();
}
else if( vNum == 3 ){
min = 100;
max = 1000;
num = (int)((Math.random() * (max - min)) + min);
sortedAnswer = Stream.of(String.valueOf(num).split("")).mapToInt(Integer::parseInt).toArray();
answerList = Stream.of(String.valueOf(num).split("")).mapToInt(Integer::parseInt).toArray();
Arrays.sort(sortedAnswer);
int count = play(num,sortedAnswer,answerList);
bw.write(count);
bw.newLine();
bw.flush();
}
else if( vNum == 4 ){
min = 1000;
max = 10000;
num = (int)((Math.random() * (max - min)) + min);
sortedAnswer = Stream.of(String.valueOf(num).split("")).mapToInt(Integer::parseInt).toArray();
answerList = Stream.of(String.valueOf(num).split("")).mapToInt(Integer::parseInt).toArray();
Arrays.sort(sortedAnswer);
int count = play(num,sortedAnswer,answerList);
bw.write(count);
bw.newLine();
bw.flush();
}
else {
System.out.println(" error! ");
}
String answerResult;
answerResult = br.readLine();
System.out.println("\n");
if("A".equals(answerResult)) {
System.out.println("좋은 실력이네요");
}
else if("B".equals(answerResult)) {
System.out.println("다음에 더 잘 할 수 있어요");
}
}
catch(IOException e){
e.printStackTrace();
}
finally {
try {
if(socket != null && !socket.isClosed()) {
socket.close();
}
}catch(IOException e){
e.printStackTrace();
}
scanner.close();
}
}
public static int play(int num, int[] sortedAnswer, int[] answerList) {
int count = 0;
int answer = 0;
int s=0,b=0;
int[] myAnswer;
int length = sortedAnswer.length;
while(true) {
Scanner in = new Scanner(System.in);
System.out.println("answer list :" + Arrays.toString(sortedAnswer));
System.out.println(" guess the answer! ");
answer = in.nextInt();
myAnswer = Stream.of(String.valueOf(answer).split("")).mapToInt(Integer::parseInt).toArray();
if(answer != num) {
count ++;
for(int i = 0; i<length;i++) {
if(myAnswer[i] == answerList[i]) {
s++;
}
else {
b++;
}
}
System.out.println(" Wrong! ");
System.out.println("number of attempts :"+ count);
System.out.println("Correct: "+s + " Miss : "+ b);
System.out.println("try again!");
s=0;b=0;
}
else {
count ++;
System.out.println(" Congratulations! You Win! ");
System.out.println(" Answer is " +num);
System.out.println("number of attempts :"+ count);
break;
}
}
return count;
}
}