-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMains.java
More file actions
173 lines (144 loc) · 6.1 KB
/
Mains.java
File metadata and controls
173 lines (144 loc) · 6.1 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
170
171
172
import java.net.*;
import java.io.*;
public class Mains {
// 닉네임을 사용자에 맞게 변경해 주세요.
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좌표
// 여기서부터 코드를 작성하세요.
// 아래에 있는 것은 샘플로 작성된 코드이므로 자유롭게 변경할 수 있습니다.
double dx = 0;
double dy = 0;
if(balls[1][0]!= 0 && balls[1][1] != 0) {
power = 130;
dx = balls[1][0] - balls[0][0];
dy = balls[1][1] - balls[0][1];
double 각도 = Math.atan2(dy,dx);
각도 = 각도 * 180 / Math.PI;
angle = 90-각도;
}else if(balls[2][0] != 0 && balls[2][1] != 0) {
power = 115;
dx = balls[2][0] - balls[0][0];
dy = balls[2][1] - balls[0][1];
double 각도 = Math.atan2(dy,dx);
각도 = 각도 * 180 / Math.PI;
angle = 90-각도;
}else if(balls[3][0] != 0 && balls[3][1] != 0) {
power = 105;
dx = balls[3][0] - balls[0][0];
dy = balls[3][1] - balls[0][1];
double 각도 = Math.atan2(dy,dx);
각도 = 각도 * 180 / Math.PI;
angle = 90-각도;
}else if(balls[4][0] != 0 && balls[4][1] != 0) {
power = 115;
dx = balls[4][0] - balls[0][0];
dy = balls[4][1] - balls[0][1];
double 각도 = Math.atan2(dy,dx);
각도 = 각도 * 180 / Math.PI;
angle = 90-각도;
}
// double length = Math.sqrt(Math.pow(dx, 2)+Math.pow(dy, 2));
// if(length < 60) power = 80;
// 주어진 데이터(공의 좌표)를 활용하여 두 개의 값을 최종 결정하고 나면,
// 나머지 코드에서 일타싸피로 값을 보내 자동으로 플레이를 진행하게 합니다.
// - 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();
}
}
}