Making decisions in Robocode methods


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