2016-03-01 30 views
0

我试图为执行随机动作(即转动和射击,然后向前移动,然后可能转身和射击等)的敌方船只实施基本游戏AI。我做了一个简单的旋转和射击的基本AI。Java游戏的随机动作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; 
} 
} 

这里是控制器类,如果这有助于解释:

public interface Controller { 
public Action action(); 
} 

这些使用一种称为Action类这只是提供了被分配到操作一些变量(如公共推力,如果转向开启状态,则将船向前移动)。我该如何去实现一种只执行一堆随机操作的AI形式?

回答

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; 
}