diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0fd9c013d2d07c1202e5b0cd2f38dca689a9eaec --- /dev/null +++ b/main.cpp @@ -0,0 +1,280 @@ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <Windows.h> + +#define POSSIBLE_ZEROS 100 +#define EXPLAIN " *** �� ���α��� �̷�ã�� ���α��Դϴ�._____(202020815 �迵�� - homework#2) *** " +#define NEXTMOVE "���� ��ġ�� �̵��մϴ�.\n\n" +#define ARRIVE "�ⱸ�� �����߽��ϴ�.\n" +#define FAILURE "���� ���� �Ա��� �ǵ��ƿԽ��ϴ�.\n" +#define EOF "\n****************************** �̷�ã�� �Լ��� �����մϴ�. �����մϴ� ******************************" + + +int startexit[2]; // �Ա��� �ⱸ�� ���⼳���� ������ ���� +char maze[12][12]; // 12x12 �̷�(2���� �迭) +int S; // Flag���� ������ ���� +int direct[4][2] = { { 1, 0 }, { 0, -1 }, { -1, 0 }, { 0, 1 } }; // ���� ���� ( ���� - 0 , ���� - 1 , ���� - 2, ���� - 3 ) +int startX, startY, endX, endY; // ��������, ���������� ��ġ�� ������ ���� + + +void MAZE_INPUT_ZEROS(void); // �������� ��θ� ����� �Լ� +void Coord_XY(void); // �������� ���������� ���������� ����� �Լ� +void traverseMaze(char maze[12][12], const int xCoord, const int yCoord, int x, int y, int direction); // �̷θ� Ž���ϴ� �Լ� +void printMaze(char maze[12][12]); // �̷θ� ����ϴ� �Լ� +void Initialize_Maze(void); // �̷θ� 1's�� �ʱ�ȭ �ϴ� �Լ� + + +int main() { + + + Initialize_Maze(); // �̷θ� 1�� �ʱ�ȭ �ϱ� + + puts(EXPLAIN); + printf("--------------------------------------------------------------------------------\n"); + + MAZE_INPUT_ZEROS(); // �������� 0's �� ���Ե� �̷� + Coord_XY(); // ���ⱸ ���� + puts("�������� ������ �̷�\n"); + printMaze(maze); // �̷� ��� + + printf("--------------------------------------------------------------------------------\n"); + puts("Start!"); + + switch (S) + { + + case 1: traverseMaze(maze, endX, endY, startX, startY, 2); //������ ���� ���� ��� �̷θ� Ž���ϴ� �Լ� ���� + break; + case 2: traverseMaze(maze, endX, endY, startX, startY, 0); //������ ������ ���� ��� �̷θ� Ž���ϴ� �Լ� ���� + break; + case 3: traverseMaze(maze, endX, endY, startX, startY, 1); //������ ���� ���� ��� �̷θ� Ž���ϴ� �Լ� ���� + break; + case 4: traverseMaze(maze, endX, endY, startX, startY, 3); //������ �Ʒ��� ���� ��� �̷θ� Ž���ϴ� �Լ� ���� + break; + + default: + break; + + } + + + puts(EOF); + + return 0; + +} + +void printMaze(char maze[12][12]){ // �̷θ� ����ϴ� �Լ� + + for (int i = 0; i < 12; i++) + { + for (int j = 0; j < 12; j++) + { + printf("%c ", maze[i][j]); + } + printf("\n"); + } +} +void traverseMaze(char maze[12][12], const int xCoord, const int yCoord, int x, int y, int direction) { // �̷θ� Ž���ϴ� �Լ� + + + maze[y][x] = 'X'; // ������ġ 'X'�� ǥ�����ֱ� + + + if (y == endY && x == endX){ // ���������� �����ϱ� + + system("Pause"); + printMaze(maze); + printf(ARRIVE); + puts(EOF); + exit(0); + return; + + } + + direction = (direction + 1) % 4; + + if (maze[y + direct[direction][1]][x + direct[direction][0]] == '0' // ���� ���� Ž�� + && 0 <= y + direct[direction][1] && y + direct[direction][1] <= 11 + && 0 <= x + direct[direction][0] && x + direct[direction][0] <= 11) + { + system("Pause"); + printMaze(maze); + printf(NEXTMOVE); + traverseMaze(maze, endX, endY, x + direct[direction][0], y + direct[direction][1], (direction + 2) % 4); // ����Լ� ���� + system("Pause"); + printMaze(maze); + printf(NEXTMOVE); + + } + direction = (direction + 1) % 4;; + + + if (maze[y + direct[direction][1]][x + direct[direction][0]] == '0' // ���� ���� Ž�� + && 0 <= y + direct[direction][1] && y + direct[direction][1] <= 11 + && 0 <= x + direct[direction][0] && x + direct[direction][0] <= 11) + { + system("Pause"); + printMaze(maze); + printf(NEXTMOVE); + traverseMaze(maze, endX, endY, x + direct[direction][0], y + direct[direction][1], (direction + 2) % 4); // ����Լ� ���� + system("Pause"); + printMaze(maze); + printf(NEXTMOVE); + } + direction = (direction + 1) % 4; + + + if (maze[y + direct[direction][1]][x + direct[direction][0]] == '0' // ���� ���� Ž�� + && 0 <= y + direct[direction][1] && y + direct[direction][1] <= 11 + && 0 <= x + direct[direction][0] && x + direct[direction][0] <= 11) + { + system("Pause"); + printMaze(maze); + printf(NEXTMOVE); + traverseMaze(maze, endX, endY, x + direct[direction][0], y + direct[direction][1], (direction + 2) % 4); // ����Լ� ���� + system("Pause"); + printMaze(maze); + printf(NEXTMOVE); + } + direction = (direction + 1) % 4; + + + if (maze[y + direct[direction][1]][x + direct[direction][0]] == '0' // ���� ���� Ž�� + && 0 <= y + direct[direction][1] && y + direct[direction][1] <= 11 + && 0 <= x + direct[direction][0] && x + direct[direction][0] <= 11) + { + system("Pause"); + printMaze(maze); + printf(NEXTMOVE); + traverseMaze(maze, endX, endY, x + direct[direction][0], y + direct[direction][1], (direction + 2) % 4); // ����Լ� ���� + system("Pause"); + printMaze(maze); + printf(NEXTMOVE); + } + + + if (y == startY && x == startX){ // ���������� �ǵ��� ������ �����ϱ� + + printMaze(maze); + printf(FAILURE); + return; + + } + +} +void MAZE_INPUT_ZEROS(void) { //�������� ��θ� ����� �Լ� + + int i, j; + + + srand(3); // seed�� ���� + + for (i = 0; i < 12; i++) { // �������� 0 ���� + for (j = 0; j < 12; j++) + maze[i][j] = rand() % 2 + '0'; + } + + for (int i = 0; i < 12; i++) { // �����ڸ�(��) + maze[i][0] = '1'; + maze[i][11] = '1'; + maze[0][i] = '1'; + maze[11][i] = '1'; + } + + int num = 0; + + for (i = 0; i < 12; i++) // 0�� ���� �˻� + { + for (j = 0; j < 12; j++) + { + if (maze[i][j] == '0') + num++; + } + } + + if (num > POSSIBLE_ZEROS) //0�� ������ 100�� �������� �˻�. 100���� ������ ����� + return MAZE_INPUT_ZEROS(); + + + return; +} +void Coord_XY(void) { // �������� ���������� ���������� ����� �Լ� + + srand(2); //seed�� ���� + + int i[2]; + for (int k = 0; k < 2; k++) { + i[k] = rand() % 12; + } + + startexit[0] = i[0]; + startexit[1] = i[1]; + + + int flag1 = rand() % 4 + 1; + int flag2 = rand() % 4 + 1; + + while (flag1 == flag2) // ���δٸ� edge���� �Ա��� �ⱸ �����ֱ� + flag2 = rand() % 4 + 1; + + S = flag1; + + switch (flag1) // �������� ���� + { + case 1: maze[startexit[0]][0] = '0'; + startX = 0; + startY = startexit[0]; + break; + case 2: maze[startexit[0]][11] = '0'; + startX = 11; + startY = startexit[0]; + break; + case 3: maze[0][startexit[0]] = '0'; + startX = startexit[0]; + startY = 0; + break; + case 4: maze[11][startexit[0]] = '0'; + startX = startexit[0]; + startY = 11; + break; + default: + break; + } + + switch (flag2) // �ⱸ���� ���� + { + case 1: maze[startexit[1]][0] = '0'; + endX = 0; + endY = startexit[1]; + break; + case 2: maze[startexit[1]][11] = '0'; + endX = 11; + endY = startexit[1]; + break; + case 3: maze[0][startexit[1]] = '0'; + endX = startexit[1]; + endY = 0; + break; + case 4: maze[11][startexit[1]] = '0'; + endX = startexit[1]; + endY = 11; + break; + default: + break; + } + + + return; + +} +void Initialize_Maze(void){ // 1's �� �ʱ�ȭ �� �̷� + + int i, j; + for (i = 0; i < 12; i++) { + for (j = 0; j < 12; j++) + maze[i][j] = '1'; + } + +} \ No newline at end of file