【问题标题】:Reassigning position within an array重新分配数组中的位置
【发布时间】:2020-01-21 19:34:53
【问题描述】:

我一直在开发一款地牢爬行游戏,但在我的二维数组中移动玩家坐标时遇到了问题。目前,在选择移动时,移动将传递到函数中,但位置不会重新分配。我已经尝试过通过我的参考和价值。

目前编写的程序让玩家移动,但我认为它实际上并没有被传递到 updateMove 函数中。我尝试使用函数 cout

我已经添加了我的主要功能和相关功能:

int main()
{
    // initialize random #
    srand(static_cast<int>(time(NULL)));

    do  // play again loop
    {
        // define variables
        bool win, lose;
        char playerMove;
        char dungeon[MAX_ROW][MAX_COL];
        int cash;

        displayInstructions(); // display instructions
        initializeDungeon(dungeon); // initialize dungeon

        do
        {
            setTraps(dungeon); // set traps
            displayDungeon(dungeon); // display dungeon
            playerMove = getMove(playerMove);

            // check for valid move
            // reassign player to new position
            updateMove(playerMove, dungeon);


            //update map now that move is legit
            if (!win && !lose)
            {
                updateDungeon();
            }

        } while (!win && !lose);

    } while (repeat());
}

void updateMove(char & playerMove, char dungeon[MAX_ROW][MAX_COL])
{
    int px, py;

    int dx = 0, dy = 0;
    if(playerMove == UP && px != 0)
    {
        dx--;
    }
    else if (playerMove == DOWN && px != 7)
    {
        dx++;
    }
    else if (playerMove == LEFT && py != 0)
    {
        dy--;
    }
    else if (playerMove == RIGHT && py != 8)
    {
        dy++;
    }

    if(dx != 0 || dy != 0)
    {
    dungeon[px][py] = SPACE; // I updated this to be an 'empty' space based on my variables
    px += dx;
    py += dy;
    dungeon[px][py] = PLAY;
    }
} ````

【问题讨论】:

  • 你的问题是什么??
  • 没有通过这个函数重新分配位置。我想知道我是否遗漏了什么?据我所知,此举已传递到函数中,但似乎对它没有任何作用
  • 您将新位置设置为 PLAY(无论是什么),但您从未更改 '@' 的旧位置。
  • @MadisonFronabarger 请编辑您的问题以添加说明。不要只是发表评论。
  • 分而治之。备份你的代码,然后破解你的方式来制作一个具有相同不当行为的非常小的程序。当您的程序只是您要查找的错误时,很容易找到错误。您可能仍然不知道为什么它是一个错误,但您发现了它并且处于提出针对性问题的非常好的位置。使用minimal reproducible example 获取灵感。

标签: c++ arrays function


【解决方案1】:

需要遍历整个地图来找到玩家是相当浪费和缓慢的。正如您在此处看到的那样,它还使编码变得更加困难。

您应该将玩家的位置存储在某处,以便一目了然,并可以参考它来清除旧位置。

类似这样的:

int px, py;

void updateMove(char playerMove, char dungeon[MAX_ROW][MAX_COL])
{
    int dx = 0, dy = 0;
    if(playerMove == 'u' && px != 0)
    {
        dx--;
    }
    else if (playerMove == 'd' && px != 8)
    {
        dx++;
    }
    else if (playerMove == 'l' && py != 0)
    {
        dy--;
    }
    else if (playerMove == 'r' && py != 9)
    {
        dy++;
    }

    if(dx != 0 || dy != 0) {
        dungeon[px][py] = EMPTY;
        px += dx;
        py += dy;
        dungeon[px][py] = PLAY;
    }
}

【讨论】:

  • 一种改进,但不能解决提问者正在努力解决的问题。
  • 它确实从地图上删除了玩家的旧位置@user4581301
  • 枪之子。错过了。请允许我对我的评论做一个小小的修改。现在这可能会解决问题,但很难确定。
  • 尝试您的建议后,我仍然遇到同样的问题。所以我猜这个问题出在我程序的其他地方。非常感谢您提高此功能的效率!
  • @MadisonFronabarger 我认为展示您如何称呼updateMove 可能会使问题浮出水面。我建议使用其他上下文来编辑您的问题。
猜你喜欢
  • 2017-09-02
  • 2020-08-15
  • 1970-01-01
  • 1970-01-01
  • 2018-11-09
  • 1970-01-01
  • 1970-01-01
  • 2020-07-07
  • 1970-01-01
相关资源
最近更新 更多