Robo Code anyone?
#1
Robo Code anyone?
Why wont this code compile ??
Code:
package wjt; import robocode.*; import java.awt.Color; import java.io.*; /** * MyLeader - a sample team robot based on MyFirstLeader by Mathew Nelson * * Looks around for enemies, and orders teammates to fire */ public class MyLeader extends TeamRobot { String mates[]; int rank = 1; /** * run: Leader's default behavior */ public void run() { // After trying out your robot, try uncommenting the import at the top, // and the next line: setColors(Color.green,Color.green,Color.green); try { // Send RobotColors object to our entire team broadcastMessage(new RobotColors(Color.green,Color.white,Color.blue)); mates = getTeammates(); for (int x = 0; x < mates.length; x++) { sendMessage (mates[x], new Integer(x+2)); } } catch (IOException e) { } // Normal behavior while(true) { setTurnRadarRight(Double.POSITIVE_INFINITY); execute(); } } /** * onScannedRobot: What to do when you see another robot */ public void onScannedRobot(ScannedRobotEvent e) { // Don't fire on teammates if (isTeammate(e.getName())) { return; } // Calculate enemy bearing double enemyBearing = this.getHeading() + e.getBearing(); // Calculate enemy's position double enemyX = getX() + e.getDistance() * Math.sin(Math.toRadians(enemyBearing)); double enemyY = getY() + e.getDistance() * Math.cos(Math.toRadians(enemyBearing)); // System.out.println("Enemy position = " + enemyX + " , " + enemyY + ".\n"); try { // Send enemy position to teammates broadcastMessage(new Point(enemyX,enemyY)); } catch (IOException ex) { System.out.println("Unable to send order: " + ex); } } /** * onMessageReceived: What to do when our leader sends a message */ public void onMessageReceived(MessageEvent e) { // Fire at a point if (e.getMessage() instanceof Point) { Point p = (Point)e.getMessage(); // Calculate x and y to target double dx = p.getX() - this.getX(); double dy = p.getY() - this.getY(); // Calculate angle to target double theta = Math.toDegrees(Math.atan2(dx,dy)); // Turn to target setTurnRight(normalRelativeAngle(theta - getGunHeading())); double dist = Math.sqrt ((dx*dx) + (dy*dy)); setAhead (dist); } // Set our colors else if (e.getMessage() instanceof RobotColors) { RobotColors c = (RobotColors)e.getMessage(); setColors(c.getBodyColor(),c.getGunColor(),c.getRadarColor()); } } /** * normalRelativeAngle: returns angle such that -180<angle<=180 */ public double normalRelativeAngle(double angle) { if (angle > -180 && angle <= 180) return angle; double fixedAngle = angle; while (fixedAngle <= -180) fixedAngle += 360; while (fixedAngle > 180) fixedAngle -= 360; return fixedAngle; } }
Thread
Thread Starter
Forum
Replies
Last Post
Mattybr5@MB Developments
Full Cars Breaking For Spares
28
28 December 2015 11:07 PM
Mattybr5@MB Developments
Full Cars Breaking For Spares
12
18 November 2015 07:03 AM
Sam Witwicky
Engine Management and ECU Remapping
17
13 November 2015 10:49 AM