【发布时间】:2016-10-13 14:14:57
【问题描述】:
有一个游戏,我必须对 AI 进行编程并与其他程序员竞争。我正在玩那个游戏。它包括一场比赛,你的 pod 必须按顺序通过一些检查站。
你有一些输入参数可以在你的算法中使用:
nextCheckpoint (x,y) -> nX,nY
lastCheckpoint (x,y) -> lnX,lnY
currentposition (x,y) -> x,y
代码的输出是方向和推力。
int pod::errorX(){
int R[2];
int X1;
float b;
int DeltaX;
//Vector of the line between Checkpoints
R[0] = nX-lnX;
R[1] = nY-lnY;
//calculation of the distance in the X axis between the ship and the line
b =(R[0]*(lnX-x) + R[1]*(lnY-y))/((-pow(R[0],2))-(pow(R[1],2)));
X1 = lnY + b*R[0];
DeltaX = x-X1;
cerr<<"Dx: "<<DeltaX<<endl;
return DeltaX;
}
我也有等效的 pod::errorY()
终于:
DirectionX = nX-errorX();
DirectionY = nY-errorY();
这是我正在做的一些照片:
the theory of what should happen
我想这样做,因为大多数时候惯性使我的船绕检查站运行,这会消耗大量时间并使我失去比赛。
好吧,现在让我们面对这个问题,它有时有效,而其他的甚至不接近。
当输入很小时。 喜欢
nX= 2;
nY= 2;
lnX=0;
lnY=0;
x=0;
y=0;
输出是:
directionX = 3
directionY = 1
但是当它们更大时:
nX= 6553;
nY= 7817;
lnX=13044;
lnY=1908;
x=13921;
y=3530;
输出:
DirectionX=6940;
DirectionY=1907;
我不知道会发生什么。也许因为 b 是十进制的?有没有我没见过的错误?
也许这个代码更容易测试,因为你不需要游戏:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
int lnX = 13044;
int lnY = 7817;
int nX = 13044;
int nY = 1908;
int x = 13921;
int y = 3530;
int R[2]={};
//Vector de la recta entre Checkpoints
R[0] = nX-lnX;
R[1] = nY-lnY;
//puro algebra, para calcular el desfase de X
int X1,DeltaX;
long double b;
b =(R[0]*(lnX-x) + R[1]*(lnY-y))/((-pow(R[0],2))-(pow(R[1],2)));
X1 = lnY + b*R[0];
DeltaX = x-X1;
//desfase de y;
int Y1,DeltaY;
Y1 = lnY+b*R[1];
DeltaY = y-Y1;
//dirección
int DirectionX;
int DirectionY;
DirectionX = nX-DeltaX;
DirectionY = nY-DeltaY;
cout<<X1<<" "<<Y1<<endl;
cout<<DeltaX<<" "<<DeltaY<<endl;
cout<<b<<" "<<DirectionX<< " "<<DirectionY<<endl;
【问题讨论】:
-
不客气,但老实说,在阅读了一半之后,我跳过了其余部分,在最后看到了一个问题。不幸的是我没有找到任何;)。不太清楚你的问题是什么
-
看起来你可以在应该进行浮点数学运算时进行整数数学运算,然后在必要时截断或舍入。
标签: c++ algorithm artificial-intelligence computational-geometry