-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
338 lines (289 loc) · 13.6 KB
/
Main.java
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import java.net.*;
import java.io.*;
public class Main {
// 닉네임을 사용자에 맞게 변경해 주세요.
static final String NICKNAME = "SEOUL09_JEONGTAEYEONG";
// 일타싸피 프로그램을 로컬에서 실행할 경우 변경하지 않습니다.
static final String HOST = "127.0.0.1";
// 일타싸피 프로그램과 통신할 때 사용하는 코드값으로 변경하지 않습니다.
static final int PORT = 1447;
static final int CODE_SEND = 9901;
static final int CODE_REQUEST = 9902;
static final int SIGNAL_ORDER = 9908;
static final int SIGNAL_CLOSE = 9909;
// 게임 환경에 대한 상수입니다.
static final int TABLE_WIDTH = 254;
static final int TABLE_HEIGHT = 127;
static final int NUMBER_OF_BALLS = 6;
// static final int[][] HOLES = { { 0, 0 }, { 127, 0 }, { 254, 0 }, { 0, 127 }, { 127, 127 }, { 254, 127 } };
static final double R = 5.73 / 2;
public static void main(String[] args) {
Socket socket = null;
String recv_data = null;
byte[] bytes = new byte[1024];
float[][] balls = new float[NUMBER_OF_BALLS][2];
int order = 0;
try {
socket = new Socket();
System.out.println("Trying Connect: " + HOST + ":" + PORT);
socket.connect(new InetSocketAddress(HOST, PORT));
System.out.println("Connected: " + HOST + ":" + PORT);
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
String send_data = CODE_SEND + "/" + NICKNAME + "/";
bytes = send_data.getBytes("UTF-8");
os.write(bytes);
os.flush();
System.out.println("Ready to play!\n--------------------");
while (socket != null) {
// Receive Data
bytes = new byte[1024];
int count_byte = is.read(bytes);
recv_data = new String(bytes, 0, count_byte, "UTF-8");
System.out.println("Data Received: " + recv_data);
// Read Game Data
String[] split_data = recv_data.split("/");
int idx = 0;
try {
for (int i = 0; i < NUMBER_OF_BALLS; i++) {
for (int j = 0; j < 2; j++) {
balls[i][j] = Float.parseFloat(split_data[idx++]);
}
}
} catch (Exception e) {
bytes = (CODE_REQUEST + "/" + CODE_REQUEST).getBytes("UTF-8");
os.write(bytes);
os.flush();
System.out.println("Received Data has been currupted, Resend Requested.");
continue;
}
// Check Signal for Player Order or Close Connection
if (balls[0][0] == SIGNAL_ORDER) {
order = (int) balls[0][1];
System.out.println("\n* You will be the " + (order == 1 ? "first" : "second") + " player. *\n");
continue;
} else if (balls[0][0] == SIGNAL_CLOSE) {
break;
}
// Show Balls' Position
for (int i = 0; i < NUMBER_OF_BALLS; i++) {
System.out.println("Ball " + i + ": " + balls[i][0] + ", " + balls[i][1]);
}
double angle = 0.0f;
double power = 0.0f;
//////////////////////////////
// 이 위는 일타싸피와 통신하여 데이터를 주고 받기 위해 작성된 부분이므로 수정하면 안됩니다.
//
// 모든 수신값은 변수, 배열에서 확인할 수 있습니다.
// - order: 1인 경우 선공, 2인 경우 후공을 의미
// - balls[][]: 일타싸피 정보를 수신해서 각 공의 좌표를 배열로 저장
// 예) balls[0][0]: 흰 공의 X좌표
// balls[0][1]: 흰 공의 Y좌표
// balls[1][0]: 1번 공의 X좌표
// balls[4][0]: 4번 공의 X좌표
// balls[5][0]: 마지막 번호(8번) 공의 X좌표
// 여기서부터 코드를 작성하세요.
// 아래에 있는 것은 샘플로 작성된 코드이므로 자유롭게 변경할 수 있습니다.
for (int i = 0; i < NUMBER_OF_BALLS - 1; i++) {
if (balls[i][0] != -1 && balls[i][0] != -1) {
// whiteBall_x, whiteBall_y: 흰 공의 X, Y좌표를 나타내기 위해 사용한 변수
double whiteBall_x = balls[0][0];
double whiteBall_y = balls[0][1];
// targetBall_x, targetBall_y: 목적구의 X, Y좌표를 나타내기 위해 사용한 변수
double targetBall_x = balls[i][0];
double targetBall_y = balls[i][1];
// width, height: 목적구와 흰 공의 X좌표 간의 거리, Y좌표 간의 거리
double width = Math.abs(targetBall_x - whiteBall_x);
double height = Math.abs(targetBall_y - whiteBall_y);
// radian: width와 height를 두 변으로 하는 직각삼각형의 각도를 구한 결과
// - 1radian = 180 / PI (도)
// - 1도 = PI / 180 (radian)
// angle : 아크탄젠트로 얻은 각도 radian을 degree로 환산한 결과
double radian = height > 0 ? Math.atan(width / height) : 0;
angle = (180.0 / Math.PI) * radian;
// 목적구가 상하좌우로 일직선상에 위치했을 때 각도 입력
if (whiteBall_x == targetBall_x) {
if (whiteBall_y < targetBall_y) {
angle = 0;
} else {
angle = 180;
}
} else if (whiteBall_y == targetBall_y) {
if (whiteBall_x < targetBall_x) {
angle = 90;
} else {
angle = 270;
}
}
// 1사분면 홀의 위치 254, 127
// 목적구가 흰 공을 중심으로 1사분면에 위치했을 때 각도를 재계산
if (whiteBall_x < targetBall_x && whiteBall_y < targetBall_y) {
double theta = Math.atan2(127 - R - balls[i][1], 254 - R - balls[i][0]);
double target_x = balls[i][0] - (2 * R * Math.sin(theta));
double target_y = balls[i][1] - (2 * R * Math.cos(theta));
double nWidth = target_x - balls[0][0];
double nHeight = target_y - balls[0][1];
double theta2 = Math.atan2(nHeight, nWidth);
angle = 90 - (180.0 / Math.PI * theta2);
System.out.println(angle);
}
// 2사분면 홀의 위치 0, 127
// 목적구가 흰 공을 중심으로 2사분면에 위치했을 때 각도를 재계산
else if (whiteBall_x > targetBall_x && whiteBall_y < targetBall_y) {
// radian = Math.atan2(height, width);
// angle = (180.0 / Math.PI) * radian + 270;
double theta = Math.atan2(Math.abs(127 - R - balls[i][1]), Math.abs(R - balls[i][0]));
double target_x = balls[i][0] + (2 * R * Math.sin(theta));
double target_y = balls[i][1] - (2 * R * Math.cos(theta));
double nWidth = target_x + balls[0][0];
double nHeight = target_y - balls[0][1];
double theta2 = Math.atan2(nHeight, nWidth);
// System.out.println(target_x + " " + target_y);
// System.out.println("theta: " + theta + " theta2: " + theta2);
angle = 275 + (180.0 / Math.PI * theta2);
System.out.println(angle);
}
// 3사분면 홀의 위치 0, 0
// 목적구가 흰 공을 중심으로 3사분면에 위치했을 때 각도를 재계산
else if (whiteBall_x > targetBall_x && whiteBall_y > targetBall_y) {
double theta = Math.atan2(Math.abs(R - balls[i][1]), Math.abs(R - balls[i][0]));
double target_x = balls[i][0] + (2 * R * Math.sin(theta));
double target_y = balls[i][1] + (2 * R * Math.cos(theta));
double nWidth = target_x + balls[0][0];
double nHeight = target_y + balls[0][1];
double theta2 = Math.atan2(nHeight, nWidth);
angle = 180 + (180.0 / Math.PI * theta2);
System.out.println(angle);
}
// 4사분면 홀의 위치 254, 0
// 목적구가 흰 공을 중심으로 4사분면에 위치했을 때 각도를 재계산
else if (whiteBall_x < targetBall_x && whiteBall_y > targetBall_y) {
// radian = Math.atan2(height, width);
// angle = (180.0 / Math.PI) * radian + 90;
double theta = Math.atan2(Math.abs(R - balls[i][1]), 254 - R - balls[i][0]);
double target_x = balls[i][0] - (2 * R * Math.sin(theta));
double target_y = balls[i][1] + (2 * R * Math.cos(theta));
double nWidth = target_x - balls[0][0];
double nHeight = target_y + balls[0][1];
double theta2 = Math.atan2(nHeight, nWidth);
radian = Math.atan2(height, width);
angle = (180.0 / Math.PI * radian) + 90;
System.out.println(angle);
}
// distance: 두 점(좌표) 사이의 거리를 계산
double distance = Math.sqrt((width * width) + (height * height));
// power: 거리 distance에 따른 힘의 세기를 계산
power = distance * 0.88;
}
}
if (balls[1][0] == -1 && balls[2][0] == -1 && balls[3][0] == -1 && balls[4][0] == -1) {
double whiteBall_x = balls[0][0];
double whiteBall_y = balls[0][1];
// targetBall_x, targetBall_y: 목적구의 X, Y좌표를 나타내기 위해 사용한 변수
double targetBall_x = balls[NUMBER_OF_BALLS - 1][0];
double targetBall_y = balls[NUMBER_OF_BALLS - 1][1];
// width, height: 목적구와 흰 공의 X좌표 간의 거리, Y좌표 간의 거리
double width = Math.abs(targetBall_x - whiteBall_x);
double height = Math.abs(targetBall_y - whiteBall_y);
// radian: width와 height를 두 변으로 하는 직각삼각형의 각도를 구한 결과
// - 1radian = 180 / PI (도)
// - 1도 = PI / 180 (radian)
// angle : 아크탄젠트로 얻은 각도 radian을 degree로 환산한 결과
double radian = height > 0 ? Math.atan(width / height) : 0;
angle = (180.0 / Math.PI) * radian;
// 목적구가 상하좌우로 일직선상에 위치했을 때 각도 입력
if (whiteBall_x == targetBall_x) {
if (whiteBall_y < targetBall_y) {
angle = 0;
} else {
angle = 180;
}
} else if (whiteBall_y == targetBall_y) {
if (whiteBall_x < targetBall_x) {
angle = 90;
} else {
angle = 270;
}
}
// 1사분면 홀의 위치 254, 127
// 목적구가 흰 공을 중심으로 1사분면에 위치했을 때 각도를 재계산
if (whiteBall_x < targetBall_x && whiteBall_y < targetBall_y) {
double theta = Math.atan2(127 - R - balls[NUMBER_OF_BALLS - 1][1],
254 - R - balls[NUMBER_OF_BALLS - 1][0]);
double target_x = balls[NUMBER_OF_BALLS - 1][0] - (2 * R * Math.sin(theta));
double target_y = balls[NUMBER_OF_BALLS - 1][1] - (2 * R * Math.cos(theta));
double nWidth = Math.abs(target_x - balls[0][0]);
double nHeight = Math.abs(target_y - balls[0][1]);
double theta2 = Math.atan2(nHeight, nWidth);
angle = 90 - (180.0 / Math.PI * theta2);
System.out.println(angle);
}
// 2사분면 홀의 위치 0, 127
// 목적구가 흰 공을 중심으로 2사분면에 위치했을 때 각도를 재계산
else if (whiteBall_x > targetBall_x && whiteBall_y < targetBall_y) {
double theta = Math.atan2(Math.abs(127 - R - balls[NUMBER_OF_BALLS - 1][1]),
Math.abs(R - balls[NUMBER_OF_BALLS - 1][0]));
double target_x = balls[NUMBER_OF_BALLS - 1][0] + (2 * R * Math.sin(theta));
double target_y = balls[NUMBER_OF_BALLS - 1][1] - (2 * R * Math.cos(theta));
double nWidth = Math.abs(target_x - balls[0][0]);
double nHeight = Math.abs(target_y - balls[0][1]);
double theta2 = Math.atan2(nHeight, nWidth);
angle = 270 + (180.0 / Math.PI * theta2);
System.out.println(angle);
}
// 3사분면 홀의 위치 0, 0
// 목적구가 흰 공을 중심으로 3사분면에 위치했을 때 각도를 재계산
else if (whiteBall_x > targetBall_x && whiteBall_y > targetBall_y) {
double theta = Math.atan2(Math.abs(R - balls[NUMBER_OF_BALLS - 1][1]),
Math.abs(R - balls[NUMBER_OF_BALLS - 1][0]));
double target_x = balls[NUMBER_OF_BALLS - 1][0] + (2 * R * Math.sin(theta));
double target_y = balls[NUMBER_OF_BALLS - 1][1] + (2 * R * Math.cos(theta));
double nWidth = Math.abs(target_x - balls[0][0]);
double nHeight = Math.abs(target_y - balls[0][1]);
double theta2 = Math.atan2(nHeight, nWidth);
angle = 180 + (180.0 / Math.PI * theta2);
System.out.println(angle);
}
// 4사분면 홀의 위치 254, 0
// 목적구가 흰 공을 중심으로 4사분면에 위치했을 때 각도를 재계산
else if (whiteBall_x < targetBall_x && whiteBall_y > targetBall_y) {
double theta = Math.atan2(Math.abs(R - balls[NUMBER_OF_BALLS - 1][1]),
254 - R - balls[NUMBER_OF_BALLS - 1][0]);
double target_x = balls[NUMBER_OF_BALLS - 1][0] - (2 * R * Math.sin(theta));
double target_y = balls[NUMBER_OF_BALLS - 1][1] + (2 * R * Math.cos(theta));
double nWidth = Math.abs(target_x - balls[0][0]);
double nHeight = Math.abs(target_y - balls[0][1]);
double theta2 = Math.atan2(nHeight, nWidth);
angle = (180.0 / Math.PI * theta2) + 90;
System.out.println(angle);
}
// distance: 두 점(좌표) 사이의 거리를 계산
double distance = Math.sqrt((width * width) + (height * height));
// power: 거리 distance에 따른 힘의 세기를 계산
power = distance;
}
// 주어진 데이터(공의 좌표)를 활용하여 두 개의 값을 최종 결정하고 나면,
// 나머지 코드에서 일타싸피로 값을 보내 자동으로 플레이를 진행하게 합니다.
// - angle: 흰 공을 때려서 보낼 방향(각도)
// - power: 흰 공을 때릴 힘의 세기
//
// 이 때 주의할 점은 power는 100을 초과할 수 없으며,
// power = 0인 경우 힘이 제로(0)이므로 아무런 반응이 나타나지 않습니다.
//
// 아래는 일타싸피와 통신하는 나머지 부분이므로 수정하면 안됩니다.
//////////////////////////////
String merged_data = angle + "/" + power + "/";
bytes = merged_data.getBytes("UTF-8");
os.write(bytes);
os.flush();
System.out.println("Data Sent: " + merged_data);
}
os.close();
is.close();
socket.close();
System.out.println("Connection Closed.\n--------------------");
} catch (Exception e) {
e.printStackTrace();
}
}
}