【发布时间】:2015-11-27 23:18:18
【问题描述】:
前段时间我问了一个关于如何让人工智能在我的 Agar.io 克隆中追逐食物的问题,我最终自己弄清楚了,但现在我不知道如何让它追逐最近的食物给它。我尝试通过创建两个数组,其中食物单元和其中的计算机之间的距离为 (x, y)(如 this fiddle 中所示,但 AI 仍然追求更远的距离。以下是我的代码的相关部分:
返回必要的值:
var x = [];
var y = [];
x.push(cell.x - computerX);
y.push(cell.y - computerY);
return [Math.min.apply(null, x), Math.min.apply(null, y)];
应用它们:
this.xxx = xy()[0];
this.yyy = xy()[1];
this.dist2 = Math.sqrt(this.xxx * this.xxx + this.yyy * this.yyy);
this.speedX = (this.xxx / this.dist2) * this.speed.x;
this.speedY = (this.yyy / this.dist2) * this.speed.y;
computerX += 28 * this.speedX / computerRadius;
computerY += 28 * this.speedY / computerRadius;
(注意:'xy' 是返回值的函数)
我如何让 AI 尝试吃离它最近的食物而不是任何细胞?
【问题讨论】:
-
投票否决。回答它的问题的相关部分不包含在问题中,并且“最小测试用例”不包括在内。您发布的代码出现“奇怪”:
return [Math.min.apply(null, x), Math.min.apply(null, y)];似乎每次都返回[cell.x - computerX, cell.y - computer Y],因为x和y数组中只有一个条目 -
对你有好处吗?如果你真的浏览了我的 jsFiddle,你会注意到上述代码位于 for 循环中。另外,相关部分都在那里。
标签: javascript html canvas artificial-intelligence