C language (simple game)-walk out of the maze

Source: Internet
Author: User

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}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.