C++tron AI陷入了自己的困境
C++ tron AI stuck in its own trail
我在为tron游戏组装AI时遇到了一些麻烦。人工智能应该以某种方式移动,以避开地图的边界和自己的轨迹。问题是,每次人工智能移动时,都会有一条轨迹出现在它的正后方,所以这会导致人工智能根本不移动,因为它会发出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;
}
}
}
以下是我的写作方式:
// 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;
}
我删除了大量类似的代码,并在检查后移动了踪迹创建,但在实际移动之前。
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;
}
}
注意,我检测下一个位置是否会发生碰撞,而不是检测你是否在上面。
相关文章:
- 没有为自己的结构调用列表推回方法
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- 在c++中为我自己的基于指针的数组分配内存的正确方法
- C++从对象自己的类中删除对象
- 使用 std::optional,而不是自己的结构
- 子轴围绕父轴而不是他自己的轴旋转
- 这个C++编译器优化(在自身的实例上调用对象自己的构造函数)的名称是什么,它是如何工作的?
- C++ 如何为自己的迭代器类从迭代器转换为const_iterator?
- 重载 + 自己的类和 std::string 的运算符
- 类无法访问自己的私有静态 constexpr 方法 - Clang bug?
- 是否可以在不填充自己的参数的情况下将模板函数作为参数传递?
- 如何访问模板参数自己的模板参数?
- 将矩阵乘以我自己的输入的向量
- 您应该在什么时候创建自己的异常类型
- 派生类是从基类继承 v 指针并仅使用它,还是也有自己的 v 指针?
- string1 == string2 和你自己的 for 循环比较有什么区别?
- 如何正确包含我自己的标头?
- 自己的自定义向量类. 内存重新分配
- 如何使用我构建的库,而不会从源代码出错,但不为我自己的项目编译?
- C++tron AI陷入了自己的困境