/***********************************/ /* Player3.java */ /* subclass of Player2.java */ /***********************************/ import java.util.*; import java.lang.*; class Player3 extends Player2 { boolean DEBUG = true; int p_ball ; // pointer to BALL element. if negative, invisibe. int p_player[] = new int[21]; // pointer to Player elements, int p_p_player ; // pointer to p_player[]. int p_goal[] = new int[2]; // pointer to {left, right} goal. int p_p_goal ; // pointer to p_goal[]. if 3, don't know which one. int p_flag[] = new int[52]; // pointer to Flag elements, int p_p_flag ; // pointer to p_flag[]. int p_line[] = new int[4]; // pointer to {left, right, top, bottom} line. int p_p_line ; // pointer to p_line[]. if 4, don't know which one. double my_x, my_y; // my calculated position. double ny_x, ny_y; // my calculated position. public void run() { while(m_time < 10) { can_shoot(); send("(turn 50)"); } } protected boolean can_shoot() { message = receive(); parse(message); if (classify_objects()) { System.out.println("Now I can calculate my position."); where_am_i(); } else { System.out.println("Now I must move to calculate my position."); } return true; } protected boolean classify_objects() { // all pointer set `-1` as initial values. p_ball = -1; p_p_player = -1; p_p_goal = -1; p_p_flag = -1; p_p_line = -1; int j; for(j = 0; j < m_objects.size(); j++) { ObjectInfo object = (ObjectInfo)m_objects.elementAt(j); if(object.m_type.compareTo("ball") == 0) p_ball = j; if(object.m_type.compareTo("player") == 0) p_player[++p_p_player] = j; if(object.m_type.compareTo("goal") == 0) // at this point, it is uncertain Left or Right. p_goal[++p_p_goal] = j; if(object.m_type.compareTo("flag") == 0) p_flag[++p_p_flag] = j; if(object.m_type.compareTo("line") == 0) // at this point, it is uncertain Left, Right, Top, Down. p_goal[++p_p_line] = j; } // Now you notice that it is better to unit this classifucation // at Player1.java. if (p_p_flag >= 2) return true; else return false; } protected void where_am_i() { /** Now, calculate my position using the first TWO FLAGS. Problem to select the most suitable pair of flag is left as homework. */ FlagInfo flag_i = (FlagInfo)m_objects.elementAt(p_flag[0]); FlagInfo flag_j = (FlagInfo)m_objects.elementAt(p_flag[1]); double r0 = Math.sqrt((flag_i.x - flag_j.x)*(flag_i.x - flag_j.x) + (flag_i.y - flag_j.y)*(flag_i.y - flag_j.y )); double r1 = flag_i.m_distance; double r2 = flag_j.m_distance; double a, b, c; c = Math.abs(flag_i.m_direction - flag_j.m_direction ) * Math.PI / 180; if ( r1 > r2) { a = Math.asin(r2 * Math.sin(c) / r0); b = Math.PI - a - c; } else { b = Math.asin(r1 * Math.sin(c) / r0); a = Math.PI - b - c; } if(flag_j.m_direction > flag_i.m_direction) b = - b; my_x = flag_j.x + ((flag_i.x - flag_j.x)*Math.cos(b) - (flag_i.y - flag_j.y)*Math.sin(b))*r2/r0; my_y = flag_j.y + ((flag_i.y - flag_j.y)*Math.cos(b) + (flag_i.x - flag_j.x)*Math.sin(b))*r2/r0; if(DEBUG){ System.out.println("where_am_i (x, y, r0) = " + my_x + ", " + my_y + ", " + r0); System.out.println(" angles (j-i, a, b) = " + (flag_j.m_direction - flag_i.m_direction) + ", " + (a*180/Math.PI) + ", " + (b*180/Math.PI)); flag_i.print(); flag_j.print(); } } }