Created
September 27, 2019 06:35
-
-
Save LiteHell/92e1d25e98ee8ae65b03d3c9c5a0331c to your computer and use it in GitHub Desktop.
미로를 만드는 C 프로그램
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _CRT_SECURE_NO_WARNINGS | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#define IS_VALID_COORD(_x, _y) (0 <= (_x) && (_x) < width && 0 <= (_y) && (_y) < height) | |
#define IS_VALID_AND_UNTOUCHED_COORD(_x, _y) IS_VALID_COORD(_x, _y) && (MAZE_AT(_x, _y) == 0) | |
#define MAZE_AT(_x, _y) maze[(_x) + (_y) * width] | |
#define LEFT 8 | |
#define RIGHT 2 | |
#define TOP 1 | |
#define BOTTOM 4 | |
void generateMaze(char* maze, int width, int height, int x, int y); | |
void printMaze(char* maze, int width, int height); | |
int main() | |
{ | |
// 난수 시드 | |
srand(time(NULL)); | |
// 미로 크기 설정 | |
int width, height; | |
printf("미로 너비와 높이를 입력하세요 : "); | |
scanf("%d %d", &width, &height); | |
// 미로 변수 선언 | |
char* maze = (char*)malloc(sizeof(char) * width * height); | |
for (int i = 0; i < width * height; i++) | |
maze[i] = 0; | |
// 미로 생성 | |
generateMaze(maze, width, height, 0, 0); | |
// 미로 출력 | |
printMaze(maze, width, height); | |
// 할당 해제 | |
free(maze); | |
} | |
// 미로를 표시하는 함수 | |
void printMaze(char* maze, int width, int height) { | |
for (int y = 0; y < height; y++) { | |
for (int x = 0; x < width; x++) { | |
printf("+"); | |
// 입구 표시 고려 | |
printf(((MAZE_AT(x, y) & TOP) == 0 && (x != 0 || y != 0)) ? "---" : " "); | |
} | |
printf("+\n"); | |
for (int x = 0; x < width; x++) { | |
printf((MAZE_AT(x, y) & LEFT) == 0 ? "|" : " "); | |
printf(" "); | |
} | |
printf("|\n"); | |
} | |
for (int x = 0; x < width; x++) { | |
if (x == width - 1) | |
// 출구 표시 | |
printf("+ "); | |
else | |
printf("+---"); | |
} | |
printf("+"); | |
} | |
void generateMaze(char* maze, int width, int height, int x, int y) { | |
char options[4] = {LEFT, RIGHT, TOP, BOTTOM}; | |
while (options[0] != 0 || options[1] != 0 || options[2] != 0 || options[3] != 0) { | |
int optionIndex = rand() % 4; | |
char option = options[optionIndex]; | |
if (option == 0) | |
continue; | |
options[optionIndex] = 0; | |
switch (option) { | |
case RIGHT: | |
if (IS_VALID_AND_UNTOUCHED_COORD(x + 1, y) && (MAZE_AT(x, y) & RIGHT) == 0) { | |
MAZE_AT(x, y) |= RIGHT; | |
MAZE_AT(x + 1, y) |= LEFT; | |
generateMaze(maze, width, height, x + 1, y); | |
} | |
break; | |
case LEFT: | |
if (IS_VALID_AND_UNTOUCHED_COORD(x - 1, y) && (MAZE_AT(x, y) & LEFT) == 0) { | |
MAZE_AT(x, y) |= LEFT; | |
MAZE_AT(x - 1, y) |= RIGHT; | |
generateMaze(maze, width, height, x - 1, y); | |
} | |
break; | |
case TOP: | |
if (IS_VALID_AND_UNTOUCHED_COORD(x, y - 1) && (MAZE_AT(x, y) & TOP) == 0) { | |
MAZE_AT(x, y) |= TOP; | |
MAZE_AT(x, y - 1) |= BOTTOM; | |
generateMaze(maze, width, height, x, y - 1); | |
} | |
break; | |
case BOTTOM: | |
if (IS_VALID_AND_UNTOUCHED_COORD(x, y + 1) && (MAZE_AT(x, y) & BOTTOM) == 0) { | |
MAZE_AT(x, y) |= BOTTOM; | |
MAZE_AT(x, y + 1) |= TOP; | |
generateMaze(maze, width, height, x, y + 1); | |
} | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment