1#include <stdio.h>2 //macro definition Maze[rows][cols]; rows and columns;3 #defineROWS 74 #defineCOLS 65 //Draw a maze (global variable)6 Charmaze[rows][cols]= {7{'#','#','#','#','#','#'},8{'#','0','#',' ',' ',' '},9{'#',' ','#',' ','#','#'},Ten{'#',' ','#',' ',' ','#'}, One{'#',' ',' ','#',' ','#'}, A{'#','#',' ',' ',' ','#'}, -{'#','#','#','#','#','#'} - }; the //set X, y coordinates (global variables); - intcurrentx=1, currenty=1; - //the XY coordinate after moving (global variable); - intnextx,nexty; + //See if the next step can go int[x][y]== '; - CharStreet =' '; + A //initialization function at voidPrintmaze (); - voidmovetonextposition (); - voidCalculatenextposition (Chardirection); - - - in intMainintargcConst Char*argv[]) { -NEXTX =CurrentX; toNexty =CurrentY; + //the screen prints out the maze; - Printmaze (); the Chardirection; * while(1) { $printf"please move the character, use the keyboard w/s/a/d (up and down) operation \ n");Panax Notoginsengscanf"%c",&direction); - calculatenextposition (direction); the movetonextposition (); + Printmaze (); A if(currentx==rows-1|| currenty==cols-1){ theprintf"customs clearance, hehe!"); + Break; - } $ } $ return 0; - } - the - //Print MapWuyi voidPrintmaze () { the for(inti =0; i<rows;i++){ - for(intj =0; j<cols;j++) { Wuprintf"%c", Maze[i][j]); - } Aboutprintf"\ n"); $ } - } - //Moving People - voidmovetonextposition () { A if(maze[nextx][nexty]==Street) { + Chartemp =Maze[currentx][currenty]; theMaze[currentx][currenty] =Maze[nextx][nexty]; -Maze[nextx][nexty] =temp; $CurrentX =nextx; theCurrentY =nexty; the the}Else{ theNEXTX =CurrentX; -Nexty =CurrentY; in } the } the //Calculate Next Position About voidCalculatenextposition (Chardirection) { the Switch(direction) { the Case 'W':{ theNEXTX = CurrentX-1; + Break; - } the Case 's':{BayiNEXTX = CurrentX +1; the Break; the } - Case 'a':{ -Nexty = CurrentY-1; the Break; the } the Case 'D':{ theNexty = CurrentY +1; - Break; the } the default: the Break;94 } the}
C language (simple game)-out of the maze