Untitled
unknown
c_cpp
2 years ago
6.7 kB
4
Indexable
#include <stdio.h> #include <stdlib.h> void initializeArray(char **array, int width, int height) { for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { array[i][j] = '.'; } } // 隨機在地圖上放置 3 個 M 並且不重複 int count = 0; while (count < 3) { int x = rand() % width; int y = rand() % height; if (array[y][x] == '.') { array[y][x] = 'M'; count++; } } count = 0; while (count < 2) { int x = rand() % width; int y = rand() % height; if (array[y][x] == '.') { array[y][x] = 'B'; count++; } } // 隨機放置 1 個 P 且上下左右都不會有 M int x = rand() % width; int y = rand() % height; while (array[y][x] != '.') { x = rand() % width; y = rand() % height; } array[y][x] = 'P'; // 隨機放置 1 個 S 並確保 上下左右都不會有 P x = rand() % width; y = rand() % height; while (array[y][x] != '.' || (y > 0 && array[y - 1][x] == 'P') || (y < height - 1 && array[y + 1][x] == 'P') || (x > 0 && array[y][x - 1] == 'P') || (x < width - 1 && array[y][x + 1] == 'P')) { x = rand() % width; y = rand() % height; } array[y][x] = 'S'; } void printArray(char **array, int width, int height) { for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { printf("%c ", array[i][j]); } printf("\n"); } } int move(char **array, int width, int height, char direction) { int px = -1, py = -1; // 找玩家的位置 for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (array[i][j] == 'P') { px = j; py = i; break; } } if (px != -1) break; } switch (direction) { case 'w': if (py > 0 && array[py - 1][px] != 'M' && array[py - 1][px] != 'S') if (py > 0 && array[py - 1][px] != 'M') { array[py][px] = '.'; array[py - 1][px] = 'P'; }else if (array[py - 1][px] == 'M') { return 1; }else { return 0; } break; case 's': if (py < height - 1 && array[py + 1][px] != 'M' && array[py + 1][px] != 'S') { array[py][px] = '.'; array[py + 1][px] = 'P'; } else if (array[py+1][px] == 'M') { return 1; }else { return 2; } break; case 'a': if (px > 0 && array[py][px - 1] != 'M' && array[py][px-1] != 'S') { array[py][px] = '.'; array[py][px - 1] = 'P'; } else if (array[py][px - 1] == 'M') { return 1; }else { return 2; } break; case 'd': if (px < width - 1 && array[py][px + 1] != 'M' && array[py][px +1] != 'S') { array[py][px] = '.'; array[py][px + 1] = 'P'; } else if (array[py][px + 1] == 'M') { return 1; }else { return 2; } break; default: printf("Invaild Input\n"); return 0; } return 0; } // map中的所有 M 會向 上下左右 隨機移動一格 void moveMonster(char **array, int width, int height) { // 找怪物的位置並回機移動一格 for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (array[i][j] == 'M') { array[i][j] = '.'; int direction; do { direction = rand() % 4; // 檢查移動後是否會超出地圖範圍 switch (direction) { case 0: if (i > 0 && array[i - 1][j] != 'P') { i--; } break; case 1: if (i < height - 1 && array[i + 1][j] != 'P') { i++; } break; case 2: if (j > 0 && array[i][j - 1] != 'P') { j--; } break; case 3: if (j < width - 1 && array[i][j + 1] != 'P') { j++; } break; } } while (i < 0 || i >= height || j < 0 || j >= width); array[i ][j] = '!'; } } } //搜尋map並把所有!換成M for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (array[i][j] == '!') { array[i][j] = 'M'; } } } } int main() { int width, height; scanf("%d", &width); scanf("%d", &height); char **array = (char **)malloc(height * sizeof(char *)); for (int i = 0; i < height; i++) { array[i] = (char *)malloc(width * sizeof(char)); } initializeArray(array, width, height); printArray(array, width, height); int isGameOver = 0; while (isGameOver == 0) { // let user input direction char direction; scanf(" %c", &direction); switch (move(array, width, height, direction)) { case 1: isGameOver = 1; printf("Game over!\n"); break; case 2: isGameOver = 1; printf("You win!\n"); break; default: break; } if (isGameOver == 1) { break; } moveMonster(array, width, height); printArray(array, width, height); } for (int i = 0; i < height; i++) { free(array[i]); } free(array); return 0; }
Editor is loading...
Leave a Comment