User Tools

Site Tools


cs175:cs-175_sp13_robocode_select_fire_with_variables

Making decisions in Robocode methods


  • A Robocode robot can fire its cannon using variable power. The higher the power used in firing, the greater the energy drain. Also, a “heavier” cannon shot travels more slowly, while a “lighter” cannon shot travels more quickly. It would make sense to use a larger cannon fire energy value when the enemy tank is at close range and to use a smaller cannon fire energy value when the enemy tank is further away.

Change fire power based on opposing robot distance

In robocode, create AdjustFireBot1 and try the following.

   /**
    * onScannedRobot: What to do when you see another robot
    */
   public void onScannedRobot(ScannedRobotEvent e) {

      // 1 - Get distance to scanned robot e:
      double distance = e.getDistance();
      
      // 2 - See what range of distances the distance from 1 falls in,
      //     and set power appropriately:
      double power = 0.0; // This the fire power variable.

         // Pseudocode; translate to Java:
         //
         //   If distance is less than 100, set power to 3 (max).
         //   
         //   If distance is between 100 and 300, set power to 2.
         //   
         //   If distance is between 300 and 500, set power to 1.
         //   
         //   If distance is greater than 500, set power to 0.1 (min).
      
      
      // 3 - Fire the cannon with range-appropriate power:
      fire( power );
      System.out.println("power: " + power); // See this output in robot's console during battle

   }

Link to robot source code, if-only version

Link to robot source code, if-else version


cs175/cs-175_sp13_robocode_select_fire_with_variables.txt · Last modified: 2014/10/13 14:33 by jchung

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki