我有一个小麻烦,为我的电子计算机游戏设置一个人工智能。人工智能应该以一种方式来避免地图的边界和它自己的踪迹。问题是,每次AI移动时,都会有一条轨迹出现在AI后面,因此这会导致AI完全不移动,因为它启动了if语句"if跟踪,不要移动“,所以我对在这种情况下应该做什么感到有点困惑。
void AIBike(){
srand(time(0)); // use time to seed random number
int AI; // random number will be stored in this variable
AI = rand()%4 + 1; // Selects a random number 1 - 4.
Map[AIy][AIx]= trail; // trail is char = '*'
if (AI == 1){
if(Map[AIy][AIx]!='x' && Map[AIy][AIx]!=trail){
AIx = AIx - 1;
}
}
else if (AI == 2){
if(Map[AIy][AIx]!='x' && Map[AIy][AIx]!=trail){
AIx = AIx + 1;
}
}
else if(AI == 3){
if(Map[AIy][AIx]!='x' && Map[AIy][AIx]!=trail){
AIy = AIy + 1;
}
}
else if(AI == 4){
if(Map[AIy][AIx]!='x' && Map[AIy][AIx]!=trail){
AIy = AIy - 1;
}
}
}
发布于 2013-11-17 20:12:22
下面是我写它的方式:
// I prefer arrays of constants instead of the copy-paste technology
const int dx[] = { 1, 0, -1, 0 };
const int dy[] = { 0, 1, 0, -1 };
int newAIx = AIx + dx[AI - 1];
int newAIy = AIy + dy[AI - 1];
if (/* newAIx and newAIy are inside the field and */ Map[newAIy][newAIx] != 'x' && Map[newAIy][newAIx] != trail) {
Map[AIy][AIx] = trail;
AIx = newAIx;
AIy = newAIy;
}
我删除了大量类似的代码,并在检查之后,但在实际移动之前,移动了跟踪创建。
发布于 2013-11-17 20:12:30
Map[AIy][AIx]= trail;
和Map[AIy][AIx]!=trail
似乎有冲突..。要检测碰撞,您需要做的就是举例说明
else if(AI == 3){
if(Map[AIy][AIx]!='x' && Map[AIy+1][AIx]!=trail){
AIy = AIy + 1;
}
}
请注意,我检测到下一个位置是否会发生碰撞,而不是检测您是否在上面。
https://stackoverflow.com/questions/20035494
复制相似问题