Zach philipp
Zach philipp

Reputation: 337

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). 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. 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".

#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");
  }
}

Upvotes: 0

Views: 478

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409364

Why not just put the "action not allowed" printing in an else branch? 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]. Otherwise you will decrement yValue twice.

I also recommend that you add some checks for indexes going out of bounds as well. For example by adding a yValue > 0 check:

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).

Upvotes: 4

Related Questions