package hello;
public class Soccer { Field field; Ball ball; Player p, q, r; boolean timeout, goal;
int clock;
public static void main(String a[]) {
new Soccer();
}
Soccer() {
field = new Field(128, 96); // 640/5, 480/5
ball = new Ball(field);
p = new Player("손","P",field,-50,10);
p = new Player("기","Q",field,50,-10);
start();
}
void start() {
System.out.println(" * START *");
timeout = false;
run();
}
void stop() {
timeout = true;
}
class Field {
int x0, x1, y0, y1, w, h;
Field(int wide, int high) {
w = wide; h = high;
x1 = w/2; y1 = h/2;
x0 = -x1; y0 = -y1;
}
int getLeft() { return x0; }
int getRight() { return x1; }
int getTop() { return y0; }
int getBottom() { return y1; }
}
class Ball {
Field f;
int x, y;
double vx, vy;
Ball(Field f) { x = 0; vx = 0; y = 0; this.f = f; }
boolean move() {
x = x + (int)vx; y = y + (int)vy;
System.out.println("공(" + x + ", " + y + ").");
vx = vx * 0.8; vy = vy * 0.8; // 마찰로 감속*비율
return (x > f.getRight()-1 || x < f.getLeft()+1); //골인?
}
void fly(double kx, double ky) {vx = vx+kx; vy += ky; }
int getX() { return x; }
int getY() { return y; }
}
class Player {
Field f;
int x, y;
double dx, dy, speed;
String name, team;
Player(String 이름, String tm, Field f, int x0, int y0) {
name = 이름; x = x0; y = y0;
team = tm; this.f = f;
}
int move(Ball b) {
/* dx = ? dy = ? 공을 향해 dash */
x = x + (int)dx; y = y + (int)dy;
int dist = (int) distance(b);
System.out.println(name +" " + dist);
return dist;
}
}
double distance(Ball b) {
double x2x = x – b.getX();
double y2y = y – b.getY();
return Math.sqrt(x2x*x2x + y2y*y2y);
}
void kick(Ball b) {
double kx = dx*2 + randM(10) – 5;
double ky = dy*2 + randM(8) – 5;
b.fly(kx, ky);
speed = speed / 2;
}
double randM(int M) {
return Math.random() * M;
}
int move(Ball b) {
dash(b); /* dx = ? … */
...
}
void dash (Ball b) {
double dist = distance(b) + 1;
speed = speed*0.8 + randM(4); // 질주 속도 조절
dx = (b.getX() – x)/dist * speed;
dy = (b.getY() – y)/dist * speed;
}
void run() {
clock = 0;
while( !timeout ) {
clock++; System.out.print ("["+clock+"]");
int dist = p.move(ball); // 선수들 공 쪽 달려간다
int distq = q.move(ball);
r = p;
if (distq < dist) { r = q; dist = distq; } //가까운 선수
if (dist < 5) {
r.kick(ball); // 5ft 이내면 공을 찬다
System.out.print(" -> " + r.name + " kicks -> ");
}goal = ball.move();
if (clock >45) stop();
}
System.out.println(" * TIME OUT *");
}
나 존나 초보야 빨간색 해놓은게 버근데 좀 알려주라;;
쪽팔리네;;;
(speed 정의가 안되어있는것은 기분 탓인가..)
콘 개웃기네 짖짜 ㅋㅋㅋ