c - Switch case - unexpected behavior -


this question has answer here:

i working on project en tried make code more efficient there's unexpected problem switch case can't seem resolve.

the code here:

switch (start){             case 0:             start = 1;         (int y = 0; y < 20; y++)         {             (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; 

this bit searches array map once initial coords of "@" (pacman) "g" (ghost) "t" (pacman in rage mode)

it stores values of coords in global variables.

now res of code:

  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;             } 

i made printf statements make more clear , see happens.

this map array:

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

basically problem follows:

when there's 1 "@" in te field (the "#" walls avoids), there no problem , pacman moves want to.

when there's both pacman , ghost in array map, using breakpoints , debugging tool, shows me runs first

switch(map[ypacman][xpacman]){ } 

statement, skips possible cases (pacman, ragemode , default), , jumps directly ghost state , makes ghost whatever in code.

if delete ghost , put normal pacman , ragemode pacman in field, ragemode move, , if put normal pacman pacman move..

soomy problem doesn't go through switch cases , not sure why..

you missing break; in switch, code continue running , assign ghost same position pacman.

here example on how fix it:

        (int x = 0; x < 30; x++)         {             //le coords du pacman, ghost, t.             switch (map[y][x])             {             case '@':    // both @ , t denote pacman, both symbols can share same code             case  't':                 ypacman = y;                 xpacman = x;                 break;  // break added here don't fall through , set ghost coordinates well.             case 'g':                 yghost1 = y;                 xghost1 = x;                 break;  // not needed, people tend forget add 1 when adding new cases, let's put 1 in measure.             } 

Comments