# 文字迷宮冒險 ## module ```c= #include <stdio.h> #include <stdlib.h> #include <conio.h> #define MAZE_WIDTH 15 #define MAZE_HEIGHT 7 #define PLAYER 'P' #define WALL '#' #define EXIT 'E' // 函式:顯示迷宮 void printMaze(char maze[MAZE_HEIGHT][MAZE_WIDTH]) { for (//自己寫//) { for (//自己寫//) { printf("%c ", maze[i][j]); } printf("\n"); } } int main() { char maze[MAZE_HEIGHT][MAZE_WIDTH] = { {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}, {'#', 'P', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'}, {'#', '#', '#', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', ' ', '#'}, {'#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'}, {'#', '#', ' ', '#', '#', '#', '#', ' ', '#', '#', '#', ' ', '#', '#', '#'}, {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'E'}, {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}}; int playerX = 1; // 初始玩家 X 座標 int playerY = 1; // 初始玩家 Y 座標 while (1) { system("cls"); // 清空螢幕上的內容 maze[playerY][playerX] = PLAYER; // 更新玩家位置 printMaze(maze); // 顯示迷宮 char input = getch(); // 從使用者取得輸入 maze[playerY][playerX] = ' '; // 清除先前的玩家位置 switch (input) { case 'w': // 如果輸入為 'w' // 自己寫// break; case 's': // 如果輸入為 's' // 自己寫// break; case 'a': // 如果輸入為 'a' // 自己寫// break; case 'd': // 如果輸入為 'd' // 自己寫// break; default: break; } if (//自己寫//) // 若玩家到達出口 { printf("Congratulations! You found the exit!\n"); system("pause"); // 暫停程式,等待使用者按下任意鍵後結束 break; } } return 0; } ``` ## easy ![](https://hackmd.io/_uploads/rknmZWcq3.png) code ```c= #include <stdio.h> #include <conio.h> #include <stdlib.h> #define MAZE_WIDTH 15 #define MAZE_HEIGHT 7 #define PLAYER 'P' #define WALL '#' #define EXIT 'E' // 函式:顯示迷宮 void printMaze(char maze[MAZE_HEIGHT][MAZE_WIDTH]) { for (int i = 0; i < MAZE_HEIGHT; i++) { for (int j = 0; j < MAZE_WIDTH; j++) { printf("%c ", maze[i][j]); } printf("\n"); } } int main() { char maze[MAZE_HEIGHT][MAZE_WIDTH] = { {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}, {'#', 'P', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'}, {'#', '#', '#', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', ' ', '#'}, {'#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'}, {'#', '#', ' ', '#', '#', '#', '#', ' ', '#', '#', '#', ' ', '#', '#', '#'}, {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'E'}, {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}}; int playerX = 1; // 初始玩家 X 座標 int playerY = 1; // 初始玩家 Y 座標 while (1) { system("cls"); // 清空螢幕上的內容 maze[playerY][playerX] = PLAYER; // 更新玩家位置 printMaze(maze); // 顯示迷宮 char input = getch(); // 從使用者取得輸入 maze[playerY][playerX] = ' '; // 清除先前的玩家位置 switch (input) { case 'w': // 如果輸入為 'w' if (maze[playerY - 1][playerX] != WALL) // 若上方不是牆壁 { playerY--; // 玩家往上移動 } break; case 's': // 如果輸入為 's' if (maze[playerY + 1][playerX] != WALL) // 若下方不是牆壁 { playerY++; // 玩家往下移動 } break; case 'a': // 如果輸入為 'a' if (maze[playerY][playerX - 1] != WALL) // 若左方不是牆壁 { playerX--; // 玩家往左移動 } break; case 'd': // 如果輸入為 'd' if (maze[playerY][playerX + 1] != WALL) // 若右方不是牆壁 { playerX++; // 玩家往右移動 } break; default: break; } if (maze[playerY][playerX] == EXIT) // 若玩家到達出口 { printf("Congratulations! You found the exit!\n"); system("pause"); // 暫停程式,等待使用者按下任意鍵後結束 break; } } return 0; } ``` ## medium ![](https://hackmd.io/_uploads/H1SSWWqq2.png) code ```c= #include <stdio.h> #include <conio.h> #include <stdlib.h> #define MAZE_WIDTH 15 #define MAZE_HEIGHT 7 #define PLAYER 'P' #define WALL '#' #define EXIT 'E' #define TREASURE 'T' // 函式:顯示迷宮 void printMaze(char maze[MAZE_HEIGHT][MAZE_WIDTH]) { for (int i = 0; i < MAZE_HEIGHT; i++) { for (int j = 0; j < MAZE_WIDTH; j++) { printf("%c ", maze[i][j]); } printf("\n"); } } int main() { char maze[MAZE_HEIGHT][MAZE_WIDTH] = { {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}, {'#', 'P', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'}, {'#', '#', '#', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', ' ', '#'}, {'#', 'T', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'}, {'#', '#', ' ', '#', '#', '#', '#', ' ', '#', '#', '#', ' ', '#', '#', '#'}, {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'E'}, {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}}; int playerX = 1; // 初始玩家 X 座標 int playerY = 1; // 初始玩家 Y 座標 // 寶藏 int treasureX = 1; // 初始寶藏 X 座標 int treasureY = 3; // 初始寶藏 Y 座標 int find_treasure = 0; while (1) { system("cls"); // 清空螢幕上的內容 maze[playerY][playerX] = PLAYER; // 更新玩家位置 maze[14][5] = EXIT; // 更新出口 printMaze(maze); // 顯示迷宮 char input = getch(); // 從使用者取得輸入 maze[playerY][playerX] = ' '; // 清除先前的玩家位置 // 玩家移動 switch (input) { case 'w': // 如果輸入為 'w' if (maze[playerY - 1][playerX] != WALL) // 若上方不是牆壁 { playerY--; // 玩家往上移動 } break; case 's': // 如果輸入為 's' if (maze[playerY + 1][playerX] != WALL) // 若下方不是牆壁 { playerY++; // 玩家往下移動 } break; case 'a': // 如果輸入為 'a' if (maze[playerY][playerX - 1] != WALL) // 若左方不是牆壁 { playerX--; // 玩家往左移動 } break; case 'd': // 如果輸入為 'd' if (maze[playerY][playerX + 1] != WALL) // 若右方不是牆壁 { playerX++; // 玩家往右移動 } break; default: break; } // 玩家碰到寶藏 if (maze[playerY][playerX] == TREASURE) { printf("You find the treasure!\n"); find_treasure++; system("pause"); } if (maze[playerY][playerX] == EXIT) // 若玩家到達出口 { if (find_treasure == 0) { printf("You need to fine the treasure first!\n"); playerX = 13; playerY = 5; system("pause"); } else { printf("Congratulations! You found the exit!\n"); break; } } } system("pause"); // 暫停程式,等待使用者按下任意鍵後結束 return 0; } ``` ## hard ![](https://hackmd.io/_uploads/HycI-b99h.png) code ```c= #include <stdio.h> #include <conio.h> #include <stdlib.h> #define MAZE_WIDTH 15 #define MAZE_HEIGHT 7 #define PLAYER 'P' #define WALL '#' #define EXIT 'E' #define GUARD 'G' #define TREASURE 'T' // 函式:顯示迷宮 void printMaze(char maze[MAZE_HEIGHT][MAZE_WIDTH]) { for (int i = 0; i < MAZE_HEIGHT; i++) { for (int j = 0; j < MAZE_WIDTH; j++) { printf("%c ", maze[i][j]); } printf("\n"); } } int main() { char maze[MAZE_HEIGHT][MAZE_WIDTH] = { {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}, {'#', 'P', ' ', ' ', ' ', 'G', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'}, {'#', '#', '#', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', ' ', '#'}, {'#', 'T', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', 'G', ' ', ' ', '#'}, {'#', '#', ' ', '#', '#', '#', '#', ' ', '#', '#', '#', ' ', '#', '#', '#'}, {'#', 'G', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'E'}, {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}}; int playerX = 1; // 初始玩家 X 座標 int playerY = 1; // 初始玩家 Y 座標 // 守衛A int guardAX = 1; // 初始守衛A X 座標 int guardAY = 5; // 初始守衛A Y 座標 // 守衛A路線 int guardArouteX[20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2}; int guardArouteY[20] = {5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}; // 守衛A counter int guardA = 1; // 守衛B int guardBX = 5; // 初始守衛B X 座標 int guardBY = 1; // 初始守衛B Y 座標 // 守衛B路線 int guardBrouteX[4] = {5, 5, 5, 5}; int guardBrouteY[4] = {1, 2, 3, 2}; // 守衛B counter int guardB = 1; // 守衛C int guardCX = 11; // 初始守衛C X 座標 int guardCY = 3; // 初始守衛C Y 座標 // 守衛C路線 int guardCrouteX[4] = {11, 11, 11, 11}; int guardCrouteY[4] = {3, 4, 5, 4}; // 守衛C counter int guardC = 1; // 寶藏 int treasureX = 1; // 初始寶藏 X 座標 int treasureY = 3; // 初始寶藏 Y 座標 int find_treasure = 0; while (1) { system("cls"); // 清空螢幕上的內容 maze[playerY][playerX] = PLAYER; // 更新玩家位置 maze[14][5] = EXIT; // 更新出口 printMaze(maze); // 顯示迷宮 char input = getch(); // 從使用者取得輸入 maze[playerY][playerX] = ' '; // 清除先前的玩家位置 // 玩家移動 switch (input) { case 'w': // 如果輸入為 'w' if (maze[playerY - 1][playerX] != WALL) // 若上方不是牆壁 { playerY--; // 玩家往上移動 } break; case 's': // 如果輸入為 's' if (maze[playerY + 1][playerX] != WALL) // 若下方不是牆壁 { playerY++; // 玩家往下移動 } break; case 'a': // 如果輸入為 'a' if (maze[playerY][playerX - 1] != WALL) // 若左方不是牆壁 { playerX--; // 玩家往左移動 } break; case 'd': // 如果輸入為 'd' if (maze[playerY][playerX + 1] != WALL) // 若右方不是牆壁 { playerX++; // 玩家往右移動 } break; default: break; } // 玩家碰到守衛 if (maze[playerY][playerX] == GUARD) { printf("Oh no! You are caught by the guard!\n"); break; } // 玩家碰到寶藏 if (maze[playerY][playerX] == TREASURE) { printf("You find the treasure!\n"); find_treasure++; system("pause"); } // 移動守衛 A maze[guardAY][guardAX] = ' '; guardAX = guardArouteX[guardA]; guardAY = guardArouteY[guardA]; guardA++; if (guardA == 20) { guardA = 0; } maze[guardAY][guardAX] = GUARD; // 移動守衛 B maze[guardBY][guardBX] = ' '; guardBX = guardBrouteX[guardB]; guardBY = guardBrouteY[guardB]; guardB++; if (guardB == 4) { guardB = 0; } maze[guardBY][guardBX] = GUARD; // 移動守衛 C maze[guardCY][guardCX] = ' '; guardCX = guardCrouteX[guardC]; guardCY = guardCrouteY[guardC]; guardC++; if (guardC == 4) { guardC = 0; } maze[guardCY][guardCX] = GUARD; // 守衛碰到玩家 if (maze[playerY][playerX] == GUARD) { system("cls"); printMaze(maze); printf("Oh no! You are caught by the guard!\n"); break; } if (maze[playerY][playerX] == EXIT) // 若玩家到達出口 { if (find_treasure == 0) { printf("You need to fine the treasure first!\n"); playerX = 13; playerY = 5; system("pause"); } else { printf("Congratulations! You found the exit!\n"); break; } } } system("pause"); // 暫停程式,等待使用者按下任意鍵後結束 return 0; } ``` ## other 其他可能的功能可以加到遊戲中: * 障礙物隨機生成:在迷宮中隨機生成一些額外的障礙物,增加迷宮的複雜度。 * 額外寶藏:放置多個寶藏。每個寶藏有不同的價值,玩家需要收集更多的寶藏來獲得高分。 * 時間限制:限制玩家在一定時間內完成迷宮,增加遊戲的挑戰性。 * 隱藏道路:添加隱藏道路,玩家需要發現它們才能縮短路線或繞過守衛。 * 暫停技能:the world,使用暫停,使得在一段時間內守衛暫停巡邏,玩家可以安全通過。 * 機關陷阱:添加一些機關陷阱,如陷阱門、移動牆壁等,玩家需要避開這些陷阱。 * 不同難度等級:增加不同難度等級,每個等級有不同的迷宮佈局、守衛巡邏路線和寶藏數量。 * 故事模式:加入一個故事情節,讓玩家在迷宮中解謎,透過故事展開探索。 * 音效和音樂:增加遊戲音效和背景音樂,提升遊戲氛圍。