为什么我的两个struct-arrays之间没有相互影响?

时间:2014-03-13 21:58:54

标签: c++ arrays struct

我一直试图让它成为一个结构数组,当它们位于不同结构数组的顶部时停止移动。然而到目前为止,第一个阵列(AKA Zombies)似乎只与第二个结构(AKA Holes)之一相互作用。

运行时,程序会识别出僵尸位于所有洞的顶部。但是,如果它们位于两个孔之一而不是全部三个孔之上,则只会阻止它们移动(通过将struct alive成员设置为false)。

我做了5次测试,他们都表明,当他们超过hole[0]时,没有一个'僵尸'会停止移动(正如他们应该的那样)。只有zombie[0]将停止在hole[1]中移动,并且所有三个僵尸将停止在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");
}

确定Zombie是否与洞相撞;

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];

}

1 个答案:

答案 0 :(得分:0)

int zombieCollision,问题出在这一行

zombie[i].alive = true;

首先,变量i在孔上迭代,所以它不应该用作僵尸的索引。 此外,即使你改变它来说zombie [z],它仍然不会起作用,因为如果它不在 last 洞之上,它将使僵尸活跃起来。

假设僵尸被标记为活着,你的程序将在没有这一行的情况下工作。没有必要在循环中将alive设置为true。