简体   繁体   English

多维数组中的 C 程序迷宫游戏遍历无法弄清楚如何检查用户输入是否是迷宫中的有效移动

[英]C program Maze game traversal in multidimensional array having trouble figuring out how to check if user input is a valid move in the maze

In the switch statement, I need some way to check if the '0' character is making a valid move into a ' '(empty space in maze) and not into a '*' (wall).在 switch 语句中,我需要一些方法来检查 '0' 字符是否正在有效地移动到 ' '(迷宫中的空白区域)而不是进入 '*'(墙)。 If the user tries to make an invalid move then it ignores that move and moves on to the next loop.如果用户尝试进行无效移动,则它会忽略该移动并继续进行下一个循环。 In my switch statement, cases A, D, and S successfully make '0' move through the maze.在我的 switch 语句中,案例 A、D 和 S 成功地使“0”穿过迷宫。 When I try to implement this if statement I have in case W, the output still moves the '0' character and will print "W action not allowed".当我尝试在 case W 中实现这个 if 语句时,输出仍然移动 '0' 字符并打印“W action not allowed”。

#include <stdio.h>

void printMaze(char MAZE[50][50]);

int main(void) 
{
  int yValue = 9;
  int xValue = 0;
  char movement; 

  char MAZE[50][50] =
  {
    {'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
    {'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
    {'*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*'},
    {'*',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
    {'*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*',' ','*'},
    {'*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*',' ',' ',' ',' ',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ','*'},
    {'*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*',' ','*',' ','*','*','*','*','*','*','*',' ','*',' ','*'},
    {'*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*',' ','*',' ','*','*','*','*','*','*','*',' ','*',' ','*'},
    {'*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*',' ',' ',' ','*','*','*',' ','*',' ','*','*','*','*','*','*','*',' ','*',' ','*'},
    {'0',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*',' ','*',' ',' '},
    {'*',' ','*',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*',' ','*',' ','*'},
    {'*',' ','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*',' ','*',' ','*'},
    {'*',' ','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ','*'},
    {'*',' ','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*'},
    {'*',' ','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ','*'},
    {'*',' ','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
    {'*',' ','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
    {'*',' ','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
    {'*',' ','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
    {'*',' ','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ','*'},
    {'*',' ','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
    {'*',' ','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
    {'*',' ','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
    {'*',' ','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
    {'*',' ','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ','*'},
    {'*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
    {' ',' ',' ',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ','*'},
    {'*',' ','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*',' ','*'},
    {'*',' ','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*',' ','*'},
    {'*',' ','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*',' ','*'},
    {'*',' ','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*',' ','*'},
    {'*',' ','*',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ','*'},
    {'*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
    {'*',' ','*',' ','*','*','*',' ',' ',' ',' ',' ','*','*',' ',' ',' ',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
    {'*',' ','*',' ','*','*',' ',' ','*','*','*',' ','*','*',' ','*','*',' ','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
    {'*',' ','*',' ','*','*',' ','*','*','*','*',' ','*','*',' ','*','*',' ','*','*',' ',' ',' ',' ',' ',' ','*','*',' ',' ',' ',' ',' ','*','*',' ','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
    {'*',' ','*',' ','*','*',' ','*',' ',' ','*',' ','*','*',' ','*','*','*','*','*','*','*','*','*','*',' ','*','*',' ','*','*','*',' ',' ','*',' ','*','*',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ','*'},
    {'*',' ','*',' ','*','*',' ','*','*',' ','*',' ','*','*',' ',' ',' ',' ',' ','*','*','*','*','*','*',' ','*','*',' ','*','*','*','*',' ','*',' ','*','*',' ','*','*','*','*','*','*','*','*','*','*','*'},
    {'*',' ','*',' ','*','*',' ','*','*',' ',' ',' ','*','*','*','*','*','*',' ','*','*','*','*','*','*',' ','*','*',' ','*','*','*','*',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*','*','*','*','*'},
    {'*',' ','*',' ','*','*',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*',' ',' ',' ',' ','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*',' ','*','*','*',' ',' '},
    {'*',' ','*',' ','*','*',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*',' ','*','*','*',' ','*'},
    {'*','*','*',' ','*','*',' ','*','*','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*',' ','*','*','*',' ','*'},
    {'*',' ',' ',' ','*','*',' ',' ',' ','*',' ','*','*','*','*',' ','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*',' ','*','*','*',' ','*'},
    {'*',' ','*','*','*','*',' ','*',' ','*',' ','*','*','*','*',' ','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*',' ','*','*','*',' ','*'},
    {'*',' ','*','*','*','*',' ','*',' ','*',' ',' ',' ','*','*',' ','*','*','*','*','*',' ','*','*','*','*','*','*','*','*',' ',' ',' ',' ','*',' ','*','*','*','*','*','*','*','*',' ','*','*','*',' ','*'},
    {'*',' ','*','*','*','*',' ','*',' ','*','*','*',' ','*','*',' ','*','*','*','*','*',' ','*','*','*','*','*','*','*','*',' ','*','*','*','*',' ','*','*','*','*','*','*','*','*',' ','*','*','*',' ','*'},
    {'*',' ','*','*','*','*',' ','*',' ',' ','*','*',' ','*','*',' ','*','*','*','*','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*','*','*','*',' ','*','*','*','*','*','*','*','*',' ',' ',' ',' ',' ','*'},
    {'*',' ','*','*','*','*',' ','*','*',' ','*','*',' ','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*',' ','*','*','*','*','*'},
    {'*',' ',' ',' ',' ',' ',' ','*','*',' ',' ',' ',' ','*','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
    {'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'}};

    //printMaze(MAZE);
    while(1){
      printMaze(MAZE);
      printf("Enter direction you want to move \nA(left)\nS(down)\nD(right)\nW(up)\n");
      movement = getchar();
      switch(movement){
        case 'A' :
        case 'a' :
        //player is moving character to the left
            printf("A action executed\n");
            MAZE[yValue][xValue] = ' ';
            xValue --;
            MAZE[yValue][xValue] = '0';
            break;


        case 'D' :
        case 'd' :
        //player is moving character to the right
            printf("D action executed\n");
            MAZE[yValue][xValue] = ' ';
            xValue ++;
            MAZE[yValue][xValue] = '0';
            break;


        case 'S' :
        case 's' :
        //player is moving character down
            printf("S action executed\n");
            MAZE[yValue][xValue] = ' ';
            yValue ++;
            MAZE[yValue][xValue] = '0';
            break;



        case 'W' :
        case 'w' :
        //player is moving character up
          if(MAZE[yValue -- ][xValue] == ' '){
              printf("W action executed\n");
              MAZE[yValue][xValue] = ' ';
              yValue --;
              MAZE[yValue][xValue] = '0'; 
              break;
          }
          printf("W action not allowed");
          break;

      }
    }
}
void printMaze(char MAZE[][50])
{
  for (int x = 0; x < 50; x++)
  {
    for(int y = 0; y < 50; y++)
    {
      printf("%c ", MAZE[x][y]);
    }
    printf("\n");
  }
}

Why not just put the "action not allowed" printing in an else branch?为什么不将“不允许操作”打印在else分支中? Like喜欢

if(MAZE[yValue - 1][xValue] == ' '){
    printf("W action executed\n");
    MAZE[yValue][xValue] = ' ';
    yValue --;
    MAZE[yValue][xValue] = '0'; 
}else{
    printf("W action not allowed");
}
break;

Do note that I changed MAZE[yValue -- ] to MAZE[yValue - 1] .请注意,我将MAZE[yValue -- ]更改为MAZE[yValue - 1] Otherwise you will decrement yValue twice .否则,您将减少yValue两次

I also recommend that you add some checks for indexes going out of bounds as well.我还建议您为超出范围的索引添加一些检查。 For example by adding a yValue > 0 check:例如通过添加yValue > 0检查:

if(yValue > 0 && MAZE[yValue - 1][xValue] == ' '){

And of course that you do the same checks for all the cases.当然,您对所有情况都进行相同的检查。


Also note that getchar returns an int , which is very important if you want to check for error or end-of-file (which you always should do).另请注意, getchar返回一个int ,如果您想检查错误或文件结尾(您总是应该这样做),这非常重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM