U E D R , A S I H C RSS

최다인/Maze (rev. 1.1)

최다인/Maze

  • 2014년 3월 31일 개발

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <Windows.h>
#pragma warning(disable:4996)

#define SIZE 12

void findStart(char[SIZE][SIZE+2], int*, int*, char*);
void findRoute(char[SIZE][SIZE+2], int*, int*, char*);
char findEnd(char[SIZE][SIZE+2], int*, int*, char);

int main()
{
	char maze[SIZE][SIZE+2];
	char direction;
	int i, x, y;

	strcpy(maze[0], "############\n\0");
	strcpy(maze[1], "#...#......#\n\0");
	strcpy(maze[2], "S.#.#.####.#\n\0");
	strcpy(maze[3], "###.#....#.#\n\0");
	strcpy(maze[4], "#....###.#.E\n\0");
	strcpy(maze[5], "####.#.#.#.#\n\0");
	strcpy(maze[6], "#..#.#.#.#.#\n\0");
	strcpy(maze[7], "##.#.#.#.#.#\n\0");
	strcpy(maze[8], "#........#.#\n\0");
	strcpy(maze[9], "######.###.#\n\0");
	strcpy(maze[10],"#......#...#\n\0");
	strcpy(maze[11],"############\n\0");

	/*
	for(i=0;i<SIZE;i++){
		gets(maze[i]);
	}
	*/

	findStart(maze, &x, &y, &direction);
	maze[x][y] = 'X';
	while(1){
		for(i=0;i<12;i++)
			printf("%s",maze[i]);
		maze[x][y] = '.';
		findRoute(maze, &x, &y, &direction);
		if(findEnd(maze, &x, &y, direction) == 1)
			break;
		maze[x][y] = 'X';
		Sleep(100);
		system("cls");
	}
	system("cls");
	maze[x][y] = 'X';
	for(i=0;i<12;i++)
		printf("%s",maze[i]);
	system("pause");
	return 0;
}

void findStart(char maze[SIZE][SIZE+2], int* x, int* y, char* direction)
{
	int i, j;

	for(i=0;i<SIZE;i++){
		for(j=0;j<SIZE;j++){
			if(maze[i][j] == 'S'){
				*x = i;
				*y = j;
				if(i == 0){
					*direction = 3;
					(*x)++;
				}
				else if(j == 0){
					*direction = 2;
					(*y)++;
				}
				else if(i == SIZE-1){
					*direction = 1;
					(*x)--;
				}
				else if(j == SIZE-1){
					*direction = 4;
					(*y)--;
				}
				return;
			}
		}
	}
}

char findEnd(char maze[SIZE][SIZE+2], int* x, int* y, char direction)
{
	if(maze[*x+1][*y] == 'E'){
		(*x)++;
		return 1;
	}

	if(maze[*x-1][*y] == 'E'){
		(*x)--;
		return 1;
	}

	if(maze[*x][*y+1] == 'E'){
		(*y)++;
		return 1;
	}

	if(maze[*x][*y-1] == 'E'){
		(*y)--;
		return 1;
	}

	if(direction == 0){
		findStart(maze,x,y,&direction);
		return 1;
	}

	return 0;
}

void findRoute(char maze[SIZE][SIZE+2], int* x, int* y, char* direction)
{
	if(*direction == 1){
		if(maze[*x][*y+1] == '#'){
			if(maze[*x-1][*y] == '.'){
				(*x)--;
				return;
			}
			else{
				if(maze[*x][*y-1] == '.'){
					(*y)--;
					*direction = 4;
					return;
				}
				else{
					(*x)++;
					*direction = 3;
					return;
				}
			}
		}
		else{
			(*y)++;
			*direction = 2;
			return;
		}
	}

	if(*direction == 2){
		if(maze[*x+1][*y] == '#'){
			if(maze[*x][*y+1] == '.'){
				(*y)++;
				return;
			}
			else{
				if(maze[*x-1][*y] == '.'){
					(*x)--;
					*direction = 1;
					return;
				}
				else{
					(*y)--;
					*direction = 4;
					return;
				}
			}
		}
		else{
			(*x)++;
			*direction = 3;
			return;
		}
	}

	if(*direction == 3){
		if(maze[*x][*y-1] == '#'){
			if(maze[*x+1][*y] == '.'){
				(*x)++;
				return;
			}
			else{
				if(maze[*x][*y+1] == '.'){
					(*y)++;
					*direction = 2;
					return;
				}
				else{
					(*x)--;
					*direction = 1;
					return;
				}
			}
		}
		else{
			(*y)--;
			*direction = 4;
			return;
		}
	}

	if(*direction == 4){
		if(maze[*x-1][*y] == '#'){
			if(maze[*x][*y-1] == '.'){
				(*y)--;
				return;
			}
			else{
				if(maze[*x+1][*y] == '.'){
					(*x)++;
					*direction = 3;
					return;
				}
				else{
					(*y)++;
					*direction = 2;
					return;
				}
			}
		}
		else{
			(*x)--;
			*direction = 1;
			return;
		}
	}

	return;
}

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
Processing time 0.0160 sec