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

时间:2017-04-11 22:04:15

标签: java robocode

我以为我可以通过从轮数中减去死亡数来计算轮次赢额值,但我的计数器没有递增:

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

1 个答案:

答案 0 :(得分:2)

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