c - 开关盒 - 意外行为

标签 c switch-statement case

<分区>

我正在做一个项目,我试图使代码更高效,但现在我的 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;

该位搜索一次数组映射以获得初始坐标 “@”(吃 bean ) “G”(幽灵) “T”(愤怒模式下的吃 bean 人)

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

现在是代码的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 语句以使其更清晰,看看会发生什么。

这是 map 数组:

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

基本上问题如下:

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

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

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

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

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

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

最佳答案

switch 中缺少break;,因此代码将继续运行并为 ghost 分配与 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.
            }

关于c - 开关盒 - 意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30613828/

相关文章:

sql-server - T-SQL Where 子句 Case 语句优化(StoredProc 的可选参数)

mysql - 为什么MySQL的触发器中的这个CASE...END CASE子句会导致错误提示?

复制缓冲区中的 IP 地址

c - typedef 和 struct 的不兼容指针类型警告

javascript - 处理 switch 语句中的组合案例和重复案例

mysql - 如何在mysql中使用 'select'中的 'case when then'?

c - 什么是 char*argv[ ] 以及它与 char **argv 有何相似之处

c - 两个 -Wformat 警告(未知的转换类型和格式参数太多)

objective-c - 奇怪的 "Switch Case"声明

c - NULL 检查多个文件指针的最佳/最快方法?