This page contains code snippets published daily for the 2020 spring home challenge.

Robocode

The robots we use for the challenge are based on the JuniorRobot,
    public class Day2 extends JuniorRobot
All methods and variables available in the JuniorRobot are documented on https://robocode.sourceforge.io/docs/robocode/robocode/JuniorRobot.html
please use that page as reference.

How to set the color of a robot?

Robocode use the setColors method to set colors of the JuniorRobot we are using for the challenge. Put this into the run() method, so the colors are set when the Robot is created:

// Some predefined colors: blue, yellow, black, white, red, 
// pink, brown, grey, orange…
// Sets these colors (robot parts): body, gun, radar, bullet, scan_arc
setColors(orange, blue, white, yellow, black);

How do I get to turn my Robot to the robot I scanned?

Very similar to how we did the Day3 robot, please check this document for reference as well.

To turn your tank towards a scanned enemy you must modify the the onScannedRobot() method and use the turnTo(degrees) method. There is a variable available to you called scannedAngle which is set to the direction (in degrees) that an enemy is scanned at. The scannedAngle variable is set right before the onScannedRobot() method is called, but it is reset as soon as your gun or your tank starts to turn. The reason this is important is that you want to turn your tank towards the enemy, and then make sure your gun is pointed towards the enemy after your tank is rotated. Otherwise your robot is pointed towards the enemy, but as the gun rotated with the body the gun is no longer pointing towards the scanned robot.
So you will have to create a local variable to store the scannedAngle so that you can turn both your tank and the gun towards the enemy. Here is what your onScannedRobot() method should look like:

public void onScannedRobot() {
    // store the angle the enemy is scanned
    int iScanAngle = scannedAngle; 
    turnTo(iScanAngle); // turn the body
    turnGunTo(iScanAngle); // turn the gun
}

How do I make my Robot sit on a wall and sweep its radar left and right 180 degrees?

We will show you how to do this on the North (i.e. the top wall), but you can edit it to be any wall. Firstly, we need to understand directions. The top of the map is at a heading of 0 degrees, right is 90 degrees, down is 180 degrees and left is 270 degrees. So, the tank has to move up (heading of 0 degrees). Then turn to face left (270 degrees). Then sweep its radar between 90 degrees (right) and 270 degrees (left). Easy! Here is the whole class file with comments explaining what is happening:

package HomeChallenge; 
import robocode.*; 
  
public class WallCrawler extends JuniorRobot {                
    // tells us to move forwards or backwards               
    private boolean bGoForward = true; 
    // tells us to sweep our gun left or right               
    private boolean bSweepLeft = true; 
    // tells us if we are on a wall               
    private boolean bOnWall = false; 
                                
    public void run() {                              
        // if the tank is on the wall, then do the sweeping                              
        if (bOnWall) {                                           
            if (bSweepLeft) {                                                           
                // sweep the gun left (from 270 degrees to 90 degrees)                                                      
                turnGunLeft(2);
                // if gun is all the way to the right, 
                // then sweep the other way                                                             
                if (gunHeading <= 90) 
                    bSweepLeft = false;  
            } 
            // sweep the gun right (from 90 degrees to 270 degrees)
            else { 
                turnGunRight(2);
                // if gun is all the way to the right
                // then sweep the other way                                                            
                if (gunHeading >= 270) 
                    bSweepLeft = true;                                           
            } 
            // move along the wall
            if (bGoForward) {
                 ahead(10);
             }
             else {
                 back(10);
             }                            
        }                             
                                     
        else { // the tank is not at a wall                                          
            // turn north 
            turnTo(0);   
            // and move - with 1000 we will hit the top wall
            // adjust the number if the battlefield is larger                                        
            ahead(1000);                               
        }              
     }                 

     public void onScannedRobot() {                              
        // Big Bullets
        fire(3);              
     }                 

     public void onHitByBullet() { 
        // Stationary Target - turn to where we were hit from                             
        turnGunTo(hitByBulletAngle);                              
        // Big Bullets                              
        fire(3);               
     } 
                                   
     public void onHitWall() {                             
        if (bOnWall) {
            // we are on the wall - change direction                                           
            bGoForward = !bGoForward;                            
        }                                                      
        else { // we have just hit the North wall                                          
            bOnWall = true;
            // Turn to face left                                           
            turnTo(270);  
            // Turn the gun to face left                                         
            turnGunTo(270);                              
        }               
    }             
}

How can you find the closest wall and drive to that wall ..

Extend the WallCrawler by going to the closest wall. How can you find the closest wall and drive to that wall ..

...
// the initial direction of the robot on a wall
// Index
// 0 == top - drive left - 270 deg
// 1 == left - drive down- 180 deg
// 2 == bottom - drive right - 90 deg
// 3 == right - drive up - 0 deg
private int[] iInitDir = { 270, 180, 90, 0 };
private int iWhatWall = -1;
....
// the first time we enter the run method
public void run()
{
// not initialized - first time
if (iWhatWall == -1)
{
int iVertDist, iHorzDist, iWhereNext;
// find the closest wall
// remember coordinate system 0,0 is bottom left
// robotX , robotY - this robots position
// fieldWidth, fieldHeight - battlefield size if (robotY < (fieldHeight/2)) // bottom or top half? iVertDist = robotY; // distance from bottom else iVertDist = fieldHeight-robotY; // distance from top
if (robotX < (fieldWidth/2)) // left or right half? iHorzDist = robotX; // distance from left else iHorzDist= fieldWidth-robotX; // distance from right if (iVertDist < iHorzDist) // vertical { if (robotY < (fieldHeight/2)) iWhereNext = 2; // go to bottom else iWhereNext = 0; // go to top } else { // horizontal if (robotX < (fieldWidth/2)) iWhereNext = 1; // go to left else iWhereNext = 3; // go to right } iWhatWall = (iWhereNext+3)%4; // direction to the wall turnTo(iInitDir[iWhatWall]); // initial direction bearGunTo(270); // 90 deg on left side // and move - max distance to wall ..
ahead(Math.max(iVertDist , iHorzDist)); } else ....