为什么我的两个结构数组没有相互作用?
Why are my two struct-arrays not interacting with each other?
我一直试图使它,所以一个数组的结构体停止移动时,他们是在一个不同的数组的结构体。然而,到目前为止,第一个数组(又名僵尸)似乎只与第二个结构体(又名洞)之一交互。
运行时,程序识别出僵尸在所有洞的顶部。但是,只有当它们位于两个孔中的一个而不是三个孔的顶部时,才会阻止它们移动(通过将结构体alive成员设置为false)。
我做了5次测试,它们都表明,当它们经过hole[0]
时,没有一个"僵尸"会停止移动(它们应该停止移动)。在hole[1]
中,只有zombie[0]
会停止移动,而在hole[2]
中,三个僵尸都将停止移动。
同样,由于某些原因,即使'alive'成员的默认设置为True,除非方法中有else
语句,否则除了一个僵尸之外,所有僵尸都会消失。
我不知道为什么,但我怀疑这与for循环中有一个for循环有关。无论是否给出准确的答案,任何帮助都将是感激的:)
主要方法:struct Player{
int x;
int y;
int number;
bool alive;
};
Player humanStart(Player Human);
Player zombieStart(Player zombie[], int difficulty, int mapSize, int level);
Player humanMove(Player human);
void zombieMove(Player zombie[]);
Player holePosition(Player hole[]);
Player humanCollision(Player zombie[], Player hole[], Player human, bool collided, int level);
void zombieCollision(Player zombie[], Player hole[]);
int main (){
int mapSize, difficulty, horde = 0, level = 0;
Player human = { 0, 0, 1, true};
Player zombie[27] = {0, 0, 1, true};
Player hole[27] = {0, 0, 1, false};
bool gameover = false;
bool collided = false;
srand((unsigned)time(NULL));
cout << "Please choose the size of the map. <10-32>" << endl;
cin >> mapSize;
if (mapSize > 32 || mapSize < 10){
cout << "Please enter a valid size between 10 and 32" << endl;
cin >> mapSize;
}
cout << "Please choose the difficulty level. <1-3>" << endl;
cin >> difficulty;
if (mapSize < 20)
horde = 1;
else if (mapSize >= 20 && mapSize < 25)
horde = 2;
else if (mapSize >= 25 && mapSize < 33)
horde = 3;
level = (difficulty * 3) * horde;
for(int i = 0; i < 27; i++){
zombie[i].number = level;
hole[i].number = level;
}
HNDCONSOLE_H clrscr();
HNDCONSOLE_H createMap();
human = humanStart(human);
zombieStart(zombie, difficulty, mapSize, level);
holePosition(hole);
while(!gameover){
for(int i = 0; i < hole[i].number; i++){
HNDCONSOLE_H gotoxy(hole[i].x, hole[i].y);
cout << "O";
}
zombieCollision(zombie, hole);
human = humanMove(human);
zombieMove(zombie);
human = humanCollision(zombie, hole, human, collided, level);
if(!human.alive)
gameover = true;
}
system("pause");
}
判断僵尸是否与一个洞相撞;
void zombieCollision(Player zombie[27], Player hole[27]){
for(int i = 0; i < hole[i].number; i++){
for(int z = 0; z < zombie[z].number; z++){
if ((zombie[z].x == hole[i].x) && (zombie[z].y == hole[i].y)){
zombie[z].alive = false;
HNDCONSOLE_H gotoxy(20+z,20+z);
cout << "ZOMBIE " << z << " DIED IN HOLE " << i << endl;
}
else
zombie[i].alive = true;
}
}
}
阻止僵尸移动;
void zombieMove(Player zombie[27]){
int choice = 0;
for(int i = 0; i < zombie[i].number; i++){
if(zombie[i].alive){
rand();
choice = 1 + (rand() % 4);
if (choice == 1 && zombie[i].y > 1){
zombie[i].y = zombie[i].y - 1;
}
else if (choice == 2 && zombie[i].y < 10){
zombie[i].y = zombie[i].y + 1;
}
else if (choice == 3 && zombie[i].x > 1){
zombie[i].x = zombie[i].x - 1;
}
else if (choice == 4 && zombie[i].x < 13){
zombie[i].x = zombie[i].x + 1;
}
HNDCONSOLE_H gotoxy(zombie[i].x, zombie[i].y);
cout << "Z";
}
else if(!zombie[i].alive){
}
}
//return zombie[27];
}
int zombieCollision,问题在这一行
zombie[i].alive = true;
对于来说,变量I正在遍历这些洞,所以它不应该被用作指向僵尸的索引。此外,即使你把它改成zombie[z],它仍然不会起作用,因为它会让僵尸活过来,如果它没有越过最后一个洞。
您的程序没有这一行也可以工作,假设僵尸开始时被标记为活着。没有必要在循环中将alive设置回true
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '