每当我尝试在简单的roguelike游戏中移动到新房间时,程序就会崩溃

时间:2016-03-30 03:18:33

标签: c linked-list

所以我很确定它是正确的,但显然它不起作用,是的。我基本上难过了。一切都很好,直到我试着去一个新的房间。所以我基本上被困在第一个房间,如果我试图离开它,程序就会死掉。

struct rooms
{
    int isVisited; //This is to make sure that if we go back to a room we've been to it won't add to the room counter   
    int wPickup;
    int aPickup;
    int mFight;
    int tPickup; //To make sure that the room's items or monsters haven't been fought or picked up.
    struct rooms *wNext;
    struct rooms *eNext;
    struct rooms *sNext;
    struct rooms *nNext;
};

这是我的房间结构和房间的链接列表。 所以我只是将代码的一部分放到一个新的房间,因为无论我走到哪个出口,它都会崩溃。

struct rooms *currentR, *newR, *mainR;
mainR = malloc(sizeof(room));
currentR = mainR;
mainR->eNext = NULL;
mainR->wNext = NULL;
mainR->sNext = NULL;
mainR->nNext = NULL;
currentR->isVisited = 1;
currentR->aPickup = 0;
currentR->mFight = 0;
currentR->tPickup = 0;
currentR->tPickup = 0;

第一个房间初始化。

一切都下地狱。

if (playerX == center && playerY == size)
    {
        if (currentR->eNext!=NULL)
            currentR = currentR->eNext;
        else
        {
            newR = malloc(sizeof(room));
            newR->eNext = NULL;
            newR->wNext = NULL;
            newR->sNext = NULL;
            newR->nNext = NULL;
            newR->wNext = currentR;
            currentR = currentR->eNext;
            currentR->aPickup = 0;
            currentR->mFight = 0;
            currentR->tPickup = 0;
            currentR->tPickup = 0;
            currentR->isVisited = 0; //Reinitialize all values for the new room
        }
        if(currentR->isVisited == 0)
        {
            explored++;
            currentR->isVisited = 1;
        }
    }

0 个答案:

没有答案