C language (simple game)-walk out of the maze
1 # include <stdio. h> 2 // macro definition maze [ROWS] [COLS]; ROWS and columns; 3 # define ROWS 7 4 # define COLS 6 5 // draw a maze (global variable) 6 char maze [ROWS] [COLS] = {7 {'#','#','#','#','#','#'}, 8 {'#', '0', '#', ''}, 9 {'#','','#','', '#', '#'}, 10 {'#', '', '#','', '', '#'}, 11 {'#', '','', '#', '', '#'}, 12 {'#','#','','','', '#'}, 13 {'#', '#'} 14}; 15 // set X, Y coordinate (global variable); 16 int currentX = 1, currentY = 1; 17 // the XY coordinate after moving (global variable ); 18 int nextX, nextY; 19 // check whether int [x] [y] = ''; 20 char street ='' can be taken next ''; 21 22 // initialization function 23 void printMaze (); 24 void moveToNextPosition (); 25 void calculateNextPosition (char direction); 26 27 28 29 int main (int argc, const char * argv []) {30 nextX = currentX; 31 nextY = currentY; 32 // The screen prints out the maze; 33 printMaze (); 34 char direction; 35 while (1) {36 printf ("Move the character, W/S/A/D (up/down) on the keyboard \ n"); 37 scanf ("% c", & direction); 38 CalculateNextPosition (direction); 39 moveToNextPosition (); 40 printMaze (); 41 if (currentX = ROWS-1 | currentY = COLS-1) {42 printf ("customs clearance, huh, huh! "); 43 break; 44} 45} 46 return 0; 47} 48 49 50 // print map 51 void printMaze () {52 for (int I = 0; I <ROWS; I ++) {53 for (int j = 0; j <COLS; j ++) {54 printf ("% c ", maze [I] [j]); 55} 56 printf ("\ n"); 57} 58} 59 // 60 void moveToNextPosition () {61 if (maze [nextX] [nextY] = street) {62 char temp = maze [currentX] [currentY]; 63 maze [currentX] [currentY] = maze [nextX] [nextY]; 64 maze [nextX] [nextY] = temp; 65 currentX = nextX; 66 currentY = nextY; 67 68} else {69 nextX = currentX; 70 nextY = currentY; 71} 72} 73 // calculate the next position 74 void calculateNextPosition (char direction) {75 switch (direction) {76 case 'W': {77 nextX = currentX-1; 78 break; 79} 80 case's ': {81 nextX = currentX + 1; 82 break; 83} 84 case 'A': {85 nextY = currentY-1; 86 break; 87} 88 case 'D': {89 nextY = currentY + 1; 90 break; 91} 92 default: 93 break; 94} 95}