Robocode - 如何计算战斗结束时赢得的回合数?

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

我想我可以通过从回合数中减去死亡人数来计算获胜的回合数,但我的计数器没有增加:

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

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

日志中没有任何编译错误或任何其他错误。当我在 onBattleEnded 事件中将变量输出到日志时,输出(100 回合后)是:

roundCount=1
deathCount=0

完整代码如下:

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 版本是 1.9.2.6

因此每一轮都会创建一个新实例。将字段设为静态使其成为 class 变量,每个实例也共享该变量。您可以找到更多信息 here.