开关盒 - 意外行为

Switch case - unexpected behavior

我正在做一个项目,我试图使代码更高效,但现在我的 switch case 出现了一个我似乎无法解决的意外问题。

代码在这里:

switch (start){
            case 0:
            start = 1;
        for (int y = 0; y < 20; y++)
        {
            for (int x = 0; x < 30; x++)
            {
                //le coords du pacman, ghost, T.
                switch (Map[y][x])
                {
                case '@':
                    yPacman = y;
                    xPacman = x;
                case 'G':
                    yGhost1 = y;
                    xGhost1 = x;
                case  'T':
                    yPacman = y;
                    xPacman = x;

                }
            }
        } break;

这个位搜索一次数组映射以获得初始坐标 “@”(吃豆子) "G"(鬼) "T"(愤怒模式下的吃豆人)

它将坐标值存储在全局变量中。

现在代码的res:

  case 1:
                    switch(Map[yPacman][xPacman])
                    {
                case '@':
                    printf(Map[yPacman][xPacman]);
                    printf("\n\n\nCoordinates Pacman: (%i,%i)\n", yPacman, xPacman);
                    break;

                case 'T':
                    printf(Map[yPacman][xPacman]);
                    printf("\n\n\nCoordinates Pacman: (%i,%i)\n", yPacman, xPacman);
                    break;

                default:
                    printf("test");
                    }

                    switch(Map[yGhost1][xGhost1])
                    {
                 case 'G':
                    break;
                    }

                    break;
            }

我制作了 printf 语句以使其更清楚,看看会发生什么。

这是地图数组:

 char Map[40][40] = {"#####################",
                    "#  @                #",
                    "# #        #        #",
                    "# #  # ####### #### #",
                    "# #  #           #  #",
                    "# ####  ###   ## #  #",
                    "#      #   #        #",
                    "# #  ## # # # ## #  #",
                    "#    #      #    #  #",
                    "# #  # ## # ## # ## #",
                    "#              #    #",
                    "#####################"
                   };

基本上问题如下:

当 te 字段中只有一个“@”(“#”是它避开的墙壁)时,没有问题,吃豆子会按照我的意愿移动。

当数组地图中既有 Pacman 又有 Ghost 时, 使用断点和调试工具,它告诉我它运行了第一个

switch(Map[yPacman][xPacman]){
}

语句,然后跳过所有可能的情况(Pacman、ragemode 甚至默认),并直接跳转到 ghost 状态并让 ghost 执行代码中的任何操作。

如果我删除幽灵并将普通吃豆人和愤怒模式吃豆人放在场地中,只有愤怒模式会移动,如果我只放普通吃豆人,吃豆人会移动..

我的问题是它没有正确地通过开关盒,我不确定为什么..

您的 switch 中缺少 break;,因此代码将继续 运行 并为幽灵分配与 pacman 相同的位置。

这里有一个关于如何修复它的例子:

        for (int x = 0; x < 30; x++)
        {
            //le coords du pacman, ghost, T.
            switch (Map[y][x])
            {
            case '@':    // Both @ and T denote pacman, so both symbols can share the same code
            case  'T':
                yPacman = y;
                xPacman = x;
                break;  // break added here so that we don't fall through and set the ghost coordinates as well.
            case 'G':
                yGhost1 = y;
                xGhost1 = x;
                break;  // Not really needed, but people tend to forget to add one when adding new cases, so let's put one in for good measure.
            }