#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <time.h>
#include <math.h>
#define MAZE_BOARD_HEIGHT 21
#define MAZE_BOARD_WIDTH 21
#define POINT_X 4 //보드시작좌표x
#define POINT_Y 2 //보드시작좌표y
#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80
#define DELAY 100
#define EXIT 50
int maze[21][21] = { 0 };
int closed[21][21] = { 0 };
int gmat[21][21] = { 0 };
int expand[21][21] = { -1 };
int action[21][21] = { -1 };
void astar(void)
{
int start[1][2] = { 6, 3 };
int delta[4][2] = {{ -1, 0 }, { 0, -1 }, { 1, 0 }, {0,1}};
int goal[1][2] = { 20, 20 };
int open[1][5];
int cost = 1;
int x, y,i;
int g,h,f;
int found, resign = 0;
int count;
closed[0][0] = 1;
x = start[0][0];
y = start[0][1];
g = 0;
h = abs(x - goal[0][0]) + abs(y - goal[0][1]);
f = g + h;
count = 0;
open[0][0] = f;
open[0][1] = g;
open[0][2] = h;
open[0][3] = x;
open[0][4] = y;
while (~found && ~resign)
if (length(open) == 0)
resign = 1;
else
open
}
void setCursor(int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
COORD getCursor(void)
{
COORD curPoint;
CONSOLE_SCREEN_BUFFER_INFO pos;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &pos);
curPoint.X = pos.dwCursorPosition.X;
curPoint.Y = pos.dwCursorPosition.Y;
return curPoint;
}
void removeCursor(void)
{
CONSOLE_CURSOR_INFO cur;
GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cur);
cur.bVisible = 0;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cur);
}
void showBoard(void)
{
int x, y;
COORD cur = getCursor();
srand((unsigned)time(NULL));
for (x = 0; x<MAZE_BOARD_HEIGHT; x++)
{
for (y = 0; y<MAZE_BOARD_WIDTH; y++)
{
maze[x][y] = rand() % 2;
} /*미로를 랜덤으로 발생*/
}
for (x = 0; x<MAZE_BOARD_HEIGHT; x++) /*벽은 모두 1로 채운다*/
{
maze[0][x] = 1;
maze[MAZE_BOARD_WIDTH - 1][x] = 1;
}
for (x = 0; x<MAZE_BOARD_HEIGHT; x++)
{
maze[x][MAZE_BOARD_WIDTH - 1] = 1;
maze[x][0] = 1;
}
maze[1][1] = 2;
maze[MAZE_BOARD_HEIGHT - 2][MAZE_BOARD_WIDTH - 2] = 3;
for (y = 0; y<MAZE_BOARD_HEIGHT; y++)
{
for (x = 0; x<MAZE_BOARD_WIDTH; x++)
{
setCursor(cur.X + (x * 2), cur.Y + y);
if (maze[y][x] == 1)
printf("■");
else if (maze[y][x] == 2)
printf("@");
else if (maze[y][x] == 3)
printf("★");
else
printf("□");
}
}
setCursor(cur.X, cur.Y);
}
void showCharacter(void)
{
COORD cur = getCursor();
printf("⊙");
setCursor(cur.X, cur.Y);
}
int detect(int x, int y)
{
int x1 = 0;
int y1 = 0;
// 커서위치얻기
COORD cur = getCursor();
// 미로내에서의위치계산
x1 = cur.X + x;
y1 = cur.Y + y;
x1 = x1 / 2 - 2;
y1 = y1 - 2;
// 미로밖에있느냐
if (!((x1 >= 0 && x1 <MAZE_BOARD_WIDTH) && (y1 >= 0 && y1 <MAZE_BOARD_HEIGHT)))
{
return 1;
}
//배열을넘어가지않는이유
if (maze[y1][x1] == 1)
return 1;
//미션성공
else if (maze[y1][x1] == 3)
return EXIT;
else
return 0;
}
void RemoveCharacter_Set(int x, int y)
{
int value = detect(x, y);
if (value == 0)
{
COORD cur = getCursor();
printf("□");
setCursor(cur.X + x, cur.Y + y);
}
else if (value == EXIT)
{
setCursor(10, 15);
printf("성공");
system("pause");
exit(1);
}
}
void character_static(void)
{
int kb;
setCursor(6, 3); //케릭터시작위치
while (1)
{
while (!_kbhit())
{
showCharacter();
Sleep(DELAY);
}
kb = _getch();
switch (kb)
{
case UP:
RemoveCharacter_Set(0, -1);
break;
case DOWN:
RemoveCharacter_Set(0, 1);
break;
case RIGHT:
RemoveCharacter_Set(2, 0);
break;
case LEFT:
RemoveCharacter_Set(-2, 0);
break;
}
}
}
int main()
{
removeCursor(); //커서깜박이지우기
setCursor(POINT_X, POINT_Y); //보드시작좌표
showBoard(); //미로판보여주기
character_static(); //케릭터움직이기
getchar();
}
미로를 랜덤하게 만들고 user가 직접 작동하는거는 만들었는데 여기다가 A*알고리즘을 도입해서 로봇이
길을 못찾으면 찾을때까지 미로를 계속 생성하고 그것을 토대로 가장 빠른길을 보여주고 싶은데
3일째 A* 이해할라고해도 도대체가 이해가 안갑니다. f= g+h로 해서 가장 작은 f찾아서 움직이는거 같은데
c로 구현할라면 g값 백터따로 h값 백터 따로 거기다가 찾았던곳은 closed 시켜서 다시 안가게 만들어야하고(이것도 백터따로)
골을 찾는 조건도 모르겠고 A*가 이해안가네요 ㅠㅜ A*좀 이해하기 쉬운방법 없을까요 c로? 이해만 하면 코딩해서 h값따라
로봇이 움직이게 하는건 할수있을거같은데 A*를 대략적으론 알겠는데 c로 구현하기가 어렵네요.....
A* c로 구현할라면 어찌해야하나요? 간단한 예제같은거 없을까요 ㅠㅜㅠㅜㅠㅜ