Location>code7788 >text

walk a labyrinth

Popularity:579 ℃/2024-08-08 08:39:39

Made a small c++ program in one day:

> #include <>
> #include <>
> #include <> // embody getch() function (math.)
> #include <> // embody sleep() function (math.)
> //atlase:
> int main() {
>     char a[50][50] = {
>         "######",
>         "#o # %",
>         "# ## #",
>         "#  # #",
>         "##   #",
>         "######"
>     }
>     char b[50][50] = {
>         "######",
>         "#o   %",
>         "#  # #",
>         "#  ###",
>         "##   #",
>         "######"
>     };
>     char c[50][50] = {
>         "####################",
>         "#o #  ##   ### #####",
>         "# ##     #     #####",
>         "#  # ######### #####",
>         "##   #######     %##",
>         "####################"
>     }; // 第三张atlase]()
>     int i, x, y, p;
>     char ch;
>     int gameRunning = 1; // Add a flag variable to control the game loop



>     // Initialize Player Position
>     x = 1, y = 1;

>     // 让玩家选择atlase
>     printf("选择atlase:1because ofaatlase,2because ofbatlase,3because ofcatlase\n");
>     scanf("%d", &p);

>     // Print the initial maze and start the game
>     if(p == 1) {
>         // Print the initial maze
>         for (i = 0; i <= 5; i++) {
>             puts(a[i]);
>         }
>         // The main loop of the game
>         while (gameRunning) {
>             ch = getch(); // Getting user input
>             // Move players based on user input
>             switch (ch) {
>                 case 's':
>                 case 'S':
>                     if (a[x + 1][y] != '#') {
>                         if (a[x + 1][y] == '%') {
>                             gameRunning = 0; // 设置标志变量because of 0,exit cycle
>                         }
>                         a[x][y] = ' ';
>                         x++;
>                         a[x][y] = 'o';
>                     }
>                     break;
>                 case 'w':
>                 case 'W':
>                     if (a[x - 1][y] != '#') {
>                         if (a[x - 1][y] == '%') {
>                             gameRunning = 0; // 设置标志变量because of 0,exit cycle
>                         }
>                         a[x][y] = ' ';
>                         x--;
>                         a[x][y] = 'o';
>                     }
>                     break;
>                 case 'a':
>                 case 'A':
>                     if (a[x][y - 1] != '#') {
>                         if (a[x][y - 1] == '%') {
>                             gameRunning = 0; // 设置标志变量because of 0,exit cycle
>                         }
>                         a[x][y] = ' ';
>                         y--;
>                         a[x][y] = 'o';
>                     }
>                     break;
>                 case 'd':
>                 case 'D':
>                     if (a[x][y + 1] != '#') {
>                         if (a[x][y + 1] == '%') {
>                             gameRunning = 0; // 设置标志变量because of 0,exit cycle
>                         }
>                         a[x][y] = ' ';
>                         y++;
>                         a[x][y] = 'o';
>                     }
>                     break;
>             }
>             system("cls"); // clear the screen
>             // Reprinting the labyrinth
>             for (i = 0; i <= 5; i++) {
>                 puts(a[i]);
>             }
>         }
>     } else if(p == 2) {
>         // Print the initial maze
>         // Print the initial maze
>         for (i = 0; i <= 5; i++) {
>             puts(b[i]);
>         }
>         // The main loop of the game
>         while (gameRunning) {
>             ch = getch(); // Getting user input
>             // Move players based on user input
>             switch (ch) {
>                 case 's':
>                 case 'S':
>                     if (b[x + 1][y] != '#') {
>                         if (b[x + 1][y] == '%') {
>                             gameRunning = 0; // 设置标志变量because of 0,exit cycle
>                         }
>                         b[x][y] = ' ';
>                         x++;
>                         b[x][y] = 'o';
>                     }
>                     break;
>                 case 'w':
>                 case 'W':
>                     if (b[x - 1][y] != '#') {
>                         if (b[x - 1][y] == '%') {
>                             gameRunning = 0; // 设置标志变量because of 0,exit cycle
>                         }
>                         b[x][y] = ' ';
>                         x--;
>                         b[x][y] = 'o';
>                     }
>                     break;
>                 case 'a':
>                 case 'A':
>                     if (b[x][y - 1] != '#') {
>                         if (b[x][y - 1] == '%') {
>                             gameRunning = 0; // 设置标志变量because of 0,exit cycle
>                         }
>                         b[x][y] = ' ';
>                         y--;
>                         b[x][y] = 'o';
>                     }
>                     break;
>                 case 'd':
>                 case 'D':
>                     if (b[x][y + 1] != '#') {
>                         if (b[x][y + 1] == '%') {
>                             gameRunning = 0; // 设置标志变量because of 0,exit cycle
>                         }
>                         b[x][y] = ' ';
>                         y++;
>                         b[x][y] = 'o';
>                     }
>                     break;
>             }
>             system("cls"); // clear the screen
>             // Reprinting the labyrinth
>             for (i = 0; i <= 5; i++) {
>                 puts(b[i]);
>             }
>         }
>     } else if(p == 3) {
>         // Print the initial maze
>         for (i = 0; i <= 5; i++) {
>             puts(c[i]);
>         }
>         // The main loop of the game
>         while (gameRunning) {
>             ch = getch(); // Getting user input
>             // Move players based on user input
>             switch (ch) {
>                 case 's':
>                 case 'S':
>                     if (c[x + 1][y] != '#') {
>                         if (c[x + 1][y] == '%') {
>                             gameRunning = 0; // 设置标志变量because of 0,exit cycle
>                         }
>                         c[x][y] = ' ';
>                         x++;
>                         c[x][y] = 'o';
>                     }
>                     break;
>                 case 'w':
>                 case 'W':
>                     if (c[x - 1][y] != '#') {
>                         if (c[x - 1][y] == '%') {
>                             gameRunning = 0; // 设置标志变量because of 0,exit cycle
>                         }
>                         c[x][y] = ' ';
>                         x--;
>                         c[x][y] = 'o';
>                     }
>                     break;
>                 case 'a':
>                 case 'A':
>                     if (c[x][y - 1] != '#') {
>                         if (c[x][y - 1] == '%') {
>                             gameRunning = 0; // 设置标志变量because of 0,exit cycle
>                         }
>                         c[x][y] = ' ';
>                         y--;
>                         c[x][y] = 'o';
>                     }
>                     break;
>                 case 'd':
>                 case 'D':
>                     if (c[x][y + 1] != '#') {
>                         if (c[x][y + 1] == '%') {
>                             gameRunning = 0; // 设置标志变量because of 0,exit cycle
>                         }
>                         c[x][y] = ' ';
>                         y++;
>                         c[x][y] = 'o';
>                     }
>                     break;
>             }
>             system("cls"); // clear the screen
>             // Reprinting the labyrinth
>             for (i = 0; i <= 5; i++) {
>                 puts(c[i]);
>             }
>         }
>     }

>     // clear the screen
>     system("cls"); 
>     printf("You win!\n");
>     sleep(5); // wait for 5 Exit after seconds
>     return 0;
> }