Search code examples
javarobocode

Robocode - how to calculate rounds won at end of a battle?


I thought I might be able to calculate the rounds won value by subtracting deaths from number of rounds, but my counters are not being incremented:

public void onRoundEnded(RoundEndedEvent event) 
{
    roundCount++;
}

public void onDeath(DeathEvent event)
{
    deathCount++;
}

Not getting any compile errors or any other errors in the log. When I output the variables to the log in the onBattleEnded event, the output (after 100 rounds) is:

roundCount=1
deathCount=0

Full code below:

public class AB extends AdvancedRobot
{
    private int deathCount;
    private int roundCount;

    public void run() 
    {
        while(true) 
        {
            ahead(100);
            turnGunRight(360);
            back(100);
            turnGunRight(360);
        }
    }

    public void onScannedRobot(ScannedRobotEvent e) 
    {
        fire(1);
    }

    public void onHitByBullet(HitByBulletEvent e) 
    {
        back(10);
    }

    public void onHitWall(HitWallEvent e) 
    {
        back(20);
    }

    public void onRoundEnded(RoundEndedEvent event) 
    {
        roundCount++;
    }

    public void onDeath(DeathEvent event)
    {
        deathCount++;
    }

    public void onBattleEnded(BattleEndedEvent event) 
    {   
        System.out.println("roundCount=" + roundCount);
        System.out.println("deathCount=" + deathCount);
    }
}

Robocode version used is 1.9.2.6


Solution

  • So a new instance is created for each round. Making the fields static makes it a class variable, which is shared by each instance as well. You can find more information here.