为什么我的两个结构数组没有相互作用?

Why are my two struct-arrays not interacting with each other?

本文关键字:数组 相互作用 结构 两个 我的 为什么      更新时间:2023-10-16

我一直试图使它,所以一个数组的结构体停止移动时,他们是在一个不同的数组的结构体。然而,到目前为止,第一个数组(又名僵尸)似乎只与第二个结构体(又名洞)之一交互。

运行时,程序识别出僵尸在所有洞的顶部。但是,只有当它们位于两个孔中的一个而不是三个孔的顶部时,才会阻止它们移动(通过将结构体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