Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

Robo Code anyone?

Thread Tools
 
Search this Thread
 
Old 04 April 2007, 09:15 PM
  #1  
pimmo2000
Scooby Regular
Thread Starter
iTrader: (6)
 
pimmo2000's Avatar
 
Join Date: Sep 2004
Location: On a small Island near France
Posts: 14,660
Received 4 Likes on 4 Posts
Default 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;
 }
}
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
Darrell@Scoobyworx
Trader Announcements
26
30 January 2024 01:27 PM
Abx
Subaru
22
09 January 2016 05:42 PM
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



Quick Reply: Robo Code anyone?



All times are GMT +1. The time now is 08:27 PM.