免责声明 - 我尝试通过 google/stackoverflow 找到答案,但我不知道如何定义问题(我不知道正确的术语)
我有很多小的 AI 片段,如下所示。每个敌人类型都有一个._ai
片段(如下所示),其中一个函数next()
由主游戏循环中的有限状态机调用(仅供参考:该next
函数不会在每次更新迭代时被调用,只有当敌人从队列)。
问题:我如何测试每个案例(考虑到一些敌人的 AI 片段可能更复杂,可能会出现千分之一的案例)并确保代码有效?
在下面的示例中,如果我blabla/1
在 count++ 下添加了该行,则该错误可能很长时间都不会出现,因为 Javascript 解释器在遇到该特定路径之前不会捕获该错误。在编译语言中,添加诸如blabla/1
会在编译时捕获的垃圾。
// AI Snippet
this._ai = (function(commands){
var count = 0;
return {
next: function(onDone, goodies, baddies) {
// If the internal counter reaches
// 2, launch a super attack and
// reset the count
if(count >= 2) {
commands.super(onDone);
count = 0;
}
else {
// If not performing the super attack
// there is a 50% chance of calling
// the `attack` command
if(chance(50)) {
var target = goodies[0];
commands.attack(onDone, target);
}
// Or a 50% chance of calling the
// `charge` command
else {
commands.charge(onDone);
count++;
}
}
}
};
})(this._commands);
我可以装配随机生成器来返回一个值表,0-n
并针对每个数字运行next
1000 次。我只是不觉得那会具体告诉我每条路径都没有错误。