2

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

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

4

1 回答 1

2

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

于 2017-04-11T22:54:42.940 回答