Menu

Making a tactical retreat

Help
2009-08-31
2012-09-15
  • Mark Petersen

    Mark Petersen - 2009-08-31

    this is my 3. time i make a threadin this forum and i have been glad for the help i received so far.

    But this is a bit of a bigger topic. my last 2 was concerning the compiling of the custom robot.

    This is some programming questions i have been unable to figure out myself.

    How do i found out how much energy is left in "my" robot and how can i escape around whit out using up all the remaining energy.

    This is the code so far;

    package mp;
    import robocode.*;
    //import java.awt.Color;

    /**

    • Darkyere - a robot by (your name here)
      */
      public class Darkyere extends AdvancedRobot
      {
      double gunTurnAmt;
      int Count = 0;
      String trackName;
      Boolean haveTarget;
      Boolean lowOnEnergy;

      /**

      • run: Darkyere's default behavior
        */
        public void run() {
        // After trying out your robot, try uncommenting the import at the top,
        // and the next line:
        //setColors(Color.red,Color.blue,Color.green);
        trackName = null; // Initialize to not tracking anyone
        setAdjustGunForRobotTurn(false);
        haveTarget= false;
        lowOnEnergy = false;

        while(true) {
        // Replace the next 4 lines with any behavior you would like

        if (lowOnEnergy == true){
            setBack(100);
            // Tell the game we will want to turn right 90
            setTurnRight(90);
            // At this point, we have indicated to the game that *when we do something*,
            // we will want to move ahead and turn right.  That's what "set" means.
            // It is important to realize we have not done anything yet!
            // In order to actually move, we'll want to call a method that
            // takes real time, such as waitFor.
            // waitFor actually starts the action -- we start moving and turning.
            // It will not return until we have finished turning.
            waitFor(new TurnCompleteCondition(this));
            // Note:  We are still moving ahead now, but the turn is complete.
            // Now we'll turn the other way...
            setTurnLeft(180);
            // ... and wait for the turn to finish ...
            waitFor(new TurnCompleteCondition(this));
            // ... then the other way ...
            setTurnRight(180);
            // .. and wait for that turn to finish.
            waitFor(new TurnCompleteCondition(this));
            // then back to the top to do it all again
        }else{
        
            Count ++;
        
            if (Count > 5) {
                trackName = null;
                haveTarget = false;}
        
            if (haveTarget == false ) {turnGunRight(360);}
        }
        

        }
        }

      /**

      • onScannedRobot: What to do when you see another robot
        */
        public void onScannedRobot(ScannedRobotEvent e) {
        if (trackName != null && !e.getName().equals(trackName)) {return;}

        if (trackName == null){trackName = e.getName();}

        Count = 0;

        if (e.getEnergy() <= 30){
        lowOnEnergy = true;
        }else{
        if (e.getEnergy() >= 50) {lowOnEnergy = false;}

        if (trackName == e.getName()){
        
            haveTarget = true;
        
            if (e.getDistance() &lt;= 200 &amp;&amp; e.getDistance() &gt;= 100) {
                fire(2);
            } else if (e.getDistance() &lt;= 100 &amp;&amp; e.getDistance() &gt;= 0){
                fire(3);
            } else {
                fire(1);
            }
        
            if (e.getDistance() &gt;= 150) {
        
                if (e.getBearing() &gt;= -180) {
                    turnLeft(-e.getBearing());
        
                } else if (e.getBearing() &lt;= 180){
                    turnRight(e.getBearing());
                }
        
                ahead(e.getDistance() -140);
        
                return;
            }
        
            if (e.getDistance() &lt;= 200 &amp;&amp; e.getDistance() &gt;= 100) {
                fire(2);
            } else if (e.getDistance() &lt;= 100 &amp;&amp; e.getDistance() &gt;= 0){
                fire(3);
            } else {
                fire(1);
            }
        
            if (e.getDistance() &lt; 25) {
                if (e.getBearing() &gt; -90 &amp;&amp; e.getBearing() &lt;= 90) {
                    back(40);
                } else {
                    ahead(40);
                }
        
            }
        

        }
        }
        scan();

    }

    /**
    
     * onHitByBullet: What to do when you're hit by a bullet
     */
    public void onHitByBullet(HitByBulletEvent e) {
            }
    
    public void onHitWall(HitWallEvent e) {
      if (e.getBearing() &gt;= -180) {
        turnLeft(e.getBearing());
        ahead(100);
        } else if (e.getBearing() &lt;= 180){
        turnRight(-e.getBearing());
        ahead(100);
        }
    
    }
    public void onHitRobot(HitRobotEvent e) {
       if (e.getBearing() &gt; -90 &amp;&amp; e.getBearing() &lt;= 90) {
           back(100);
       } else {
           ahead(100);
       }
        trackName = e.getName();
    

    }

    }

    Hope its not to much to ask, its a bit of some code to go through.

     
    • Nat Pavasant

      Nat Pavasant - 2009-08-31

      The code you use to track your energy is correct. But about the other part you want, I think you need to figure them out yourselves. There are many techniques invented over the eight years of Robocode. You might want to join the RoboWiki (http://robowiki.net/) for further information on this.

      The energy is lost when you hit a wall, hit by bullet, fire a bullet or hit other robot. There are quite many strategies on this. Please join the RoboWIki and enjoyed becoming a Robocoder!

       
    • Mark Petersen

      Mark Petersen - 2009-08-31

      i thought the code to track my energy would be correct but actually it tracks my current targets energy, so when my robot has all most killed it's target it back's up and move wildly around.

      I have tried reading through the robocode API but i cant find a way to track my energy only shows me way's to see the targets energy.

      im currently only online to check any replies to the thread, so i am gonna wait joining RoboWiki for now. But i will take your advise and most definitely look around and becoming a member probably as soon as tomorrow.

      im gonna go to bed and have a look at it all tomorrow. ty for your help so far and best regards to you and all the people reading this post.

      Have a good night,
      Mark

       
      • Nat Pavasant

        Nat Pavasant - 2009-08-31

        Sorry, I misread your code. It should call getEnergy(), not e.getEnergy();

         
    • Nat Pavasant

      Nat Pavasant - 2009-08-31

      A bit more, you should look for a API at AdvancedRobot class ,not ScannedRobotEvent class.

       

Log in to post a comment.

MongoDB Logo MongoDB