邻接矩阵设置链接不正确

Adjacency Matrix Setting Links Incorrectly

我正在尝试为基于文本的“3-D 迷宫”(实现为 7x7 矩阵)实现邻接矩阵。出于某种原因,我当前的代码使玩家始终可以选择向下移动,这是不应该发生的。我的测试行表明文件输入正确,并且 pIdx、idx 和 dCode 参数以正确的值进入第二个函数。

问题是否出在第二个函数的最后一行?

    {    

   //This function reads in the directions from a file. If it reads in -1, there should not be a
    //link to that direction.

    // Read North 
    getNextLine(line, 128);
    link = atoi(line); 
    setLink(i, link, 'N');

    // Read South 
    getNextLine(line, 128);
    link = atoi(line);   
    setLink(i, link, 'S');

    // Read East
    getNextLine(line, 128);
    link = atoi(line);    
    setLink(i, link, 'E');

    // Read West
    getNextLine(line, 128);
    link = atoi(line);    
    setLink(i, link, 'W');

    // Read Up
    getNextLine(line, 128);
    link = atoi(line);    
    setLink(i, link, 'U');

    // Read Down
    getNextLine(line, 128);
    link = atoi(line);    
    setLink(i, link, 'D');
}

这个函数就是问题所在:

 void Map::setLink(int pIdx, int linkIdx, char dCode)
{
  if ((pIdx<0)||(pIdx>6))
    {
        cout<<"pIdx invalid"<<endl;
        exit(-1);
    }
else if ((linkIdx<-1)||(linkIdx>6))
    {
        cout<<"LinkIdx invalid"<<endl;
        exit(-1);
    }
else if ((dCode!='N')&& (dCode!='S')&& (dCode!='E')&& (dCode!='W')&&(dCode!='U')&&(dCode!='D'))
    {
        cout<<"dCode must equal 'N', 'S','E', 'W', 'U', or 'D'."<<endl;
        exit(-1);
    }

m_cAdjMatrix[pIdx][linkIdx]=dCode;
}

文件输入:
-1 3个 1个 -1 -1 -1 -1 4个 2个 0 -1 -1 -1 -1 -1 1个 14 -1 0 6个 -1 -1 -1 -1 1个 -1 5个 -1 16 -1 -1 8个 3个 -1 -1 -1 3个 9 7 -1 -1 -1

linkIdx为-1时,setLink末尾的赋值将访问数组(m_cAdjMatrix[pIdx][-1])的边界之外,导致未定义的行为。

如果 linkIdx 是 -1,你不想做那个作业。