用于Java游戏的随机动作AI控制器

时间:2016-03-01 22:12:36

标签: java artificial-intelligence

我试图为我的敌舰执行基本游戏AI,执行随机动作(即转弯和射击,然后向前移动,然后可能转身射击等)。我做了一个基本的人工智能,只需旋转和射击。

这是RotateAndShoot AI:

public class RotateAndShoot implements Controller {
Action action = new Action();

@Override
public Action action() {
    action.shoot = true;
    action.thrust = 1; //1=on 0=off
    action.turn = -1; //-1 = left 0 = no turn 1 = right
    return action;
}
}

这是Controller类,如果这有助于解释:

public interface Controller {
public Action action();
}

这些使用一个名为Action的类,它只提供一些分配给动作的变量(例如public int thrust,如果转为on状态,则向前移动船只)。我怎样才能实现一种只需要做一堆随机动作的AI形式?

1 个答案:

答案 0 :(得分:3)

您可以使用Math.random()或Random。

以下是随机解决方案:

@Override
public Action action() {
    Random rand = new Random();
    action.shoot = rand.nextBoolean();
    action.thrust = rand.nextInt(2);
    action.turn = rand.nextInt(3) - 1;
    return action;
}