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

标签 java robocode

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

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

最佳答案

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

关于java - Robocode - 如何计算战斗结束时赢得的回合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43356973/

相关文章:

java - 如何使用 JOOQ 刷新数据库连接

java - 设置 JPanel 的大小不依赖于 JFrame 的大小

java - 如何通过getText()从html内的多个子节点中提取动态文本

java - 机器人代码如何工作?

java - 如何解决 'could not find or load main class robocode.robocode'错误?

python - 是否有类似 Python 游戏或挑战的 RoboCode?

java - Robocode(java): how can I make my robot change color depending on the result of getEnergy()?

c# - 在 Java 中使用 C# 代码

java - 我不断收到错误。不兼容的类型 : ArrayList<StudentDetails> cannot be converted to ArrayList<AndroidTargets>