#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include "graphics.h"
#define WALL -1
#define START -3
#define END -4
#define NOTWAY -2
#define MX 60
#define MY 60
#define _Y (MY*2+1)
#define _X (MX*2+1)
// int maze[MY * 2 + 1][MX * 2 + 1], sign = 1, sign_old;
int ** maze, sign = 1, maker_n = 1, *maker_step, i_maker_step = -1;
int _x, _y, mx, my;
struct position
{
int x;
int y;
}direction[] = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } }, maker, temp, explorer;
int can_move(int y, int x)
{
if (y<0 || x<0 || x >= _x || y >= _x)
return 0;
else if (maze[y][x] == WALL || maze[y][x] == NOTWAY || maze[y][x] == START || maze[y][x]>0)
return 0;
else
return 1;
}
int draw_arrow(int x, int y, int r, int direction)
{
switch (direction)
{
case 0:
line(x - r, y, x + r, y);
line(x, y - r, x + r, y);
line(x, y + r, x + r, y);
break;
case 1:
line(x - r, y, x + r, y);
line(x, y - r, x - r, y);
line(x, y + r, x - r, y);
break;
case 2:
line(x, y - r, x, y + r);
line(x - r, y, x, y + r);
line(x + r, y, x, y + r);
break;
case 3:
line(x, y - r, x, y + r);
line(x - r, y, x, y - r);
line(x + r, y, x, y - r);
break;
default:
return 0;
break;
}
return 1;
}
void printf_pad(void)
{
int i, j;
int max = 0;
int x, y;
char res[20] = "res/res-1.png";
/* struct step
{
int y;
int x;
}steps[_x*_x + 1], end;*/
struct step
{
int y;
int x;
}*steps, end;
steps = (struct step *)malloc((_x*_x + 1)*sizeof(struct step));
x = mx * 2 + 1;
y = my * 2 + 1;
initgraph(605, 605);
setbkcolor(WHITE);
setcolor(BLACK);
setcaption("Maze");
for (j = 0; j < y; j += 2)
for (i = 1; i < x - 1; i += 2)
if (maze[j][i] == WALL)
line(2 + (i / 2) * 600.0 / mx, 2 + (j / 2) * 600.0 / mx, 2 + (i / 2 + 1) * 600.0 / mx, 2 + (j / 2) * 600.0 / mx);
for (j = 0; j < x; j += 2)
for (i = 1; i < y - 1; i += 2)
if (maze[i][j] == WALL)
line(2 + (j / 2) * 600.0 / mx, 2 + (i / 2) * 600.0 / mx, 2 + (j / 2) * 600.0 / mx, 2 + (i / 2 + 1) * 600.0 / mx);
for (i = 0; i<_x; i++)
{
for (j = 0; j<_x; j++)
{
if (maze[i][j] == WALL)
;
else if (maze[i][j]>0)
{
steps[maze[i][j]].y = i;
steps[maze[i][j]].x = j;
if (maze[i][j]>max)
max = maze[i][j];
}
else if (maze[i][j] == END)
{
end.y = i;
end.x = j;
steps[max + 1].y = i;
steps[max + 1].x = j;
}
else
;
}
}
setcolor(RED);
for (i = 1; i <= max; i++)
{
if (steps[i].x % 2 && steps[i].y % 2)
{
for (j = 0; j < 4; j++)
{
if (steps[i].x + direction[j].x == steps[i + 1].x&&steps[i].y + direction[j].y == steps[i + 1].y)
break;
}
draw_arrow(2 + 600.0 / my / 2 * 0.1 + (steps[i].x)*(600.0 / mx / 2), 2 + 600.0 / my / 2 * 0.1 + (steps[i].y)*(600.0 / my / 2), 600.0 / my / 2 * 0.9, j);
//circle(2 + 600.0 / my / 2 * 0.1 + (steps[i].x)*(600.0 / mx / 2), 2 + 600.0 / my / 2 * 0.1+ (steps[i].y)*(600.0 / my / 2), 600.0 / my / 2 * 0.9);
Sleep(20);
}
}
PIMAGE image[6], screen;
screen = newimage();
getimage(screen, 0, 0, 605, 605);
for (i = 0; i < 6; i++)
{
image[i] = newimage();
getimage(image[i], res, 0, 0);
res[8] += 1;
}
setfont(60, 0, "宋体");
setbkmode(TRANSPARENT);
settextjustify(CENTER_TEXT, CENTER_TEXT);
for (i = 0; i < 4; i++)
{
for (j = 0; j < 6; j++)
{
setfont(60 + ((j == 2 || j == 5) ? 0 : 1) * 10, 0, "宋体");
outtextxy(303, 250, "SUCCESS");
putimage_withalpha(NULL, image[j], 100, 305);
//putimage(100, 200, image[j], SRCINVERT);
if (j == 2 || j == 5)
Sleep(150);
Sleep(200);
//cleardevice();
putimage(0, 0, screen);
}
}
putimage(0, 0, screen);
getch();
closegraph();
return;
}
int move(int n)
{
int i;
if (maze[explorer.y][explorer.x] == END)
{
printf_pad();
//getchar();
exit(0);
}
maze[explorer.y][explorer.x] = n;
for (i = 0; i<4; i++)
{
if (can_move(explorer.y + direction[i].y, explorer.x + direction[i].x))
{
explorer.y += direction[i].y;
explorer.x += direction[i].x;
move(n + 1);
maze[explorer.y][explorer.x] = NOTWAY;
explorer.y -= direction[i].y;
explorer.x -= direction[i].x;
}
}
maze[explorer.y][explorer.x] = NOTWAY;
return 0;
}
int is_map_created()
{
int i, j, x, y;
x = mx * 2 + 1;
y = my * 2 + 1;
for (i = 1; i<y - 1; i += 2)
for (j = 1; j<x - 1; j += 2)
if (maze[i][j] == 0)
{
temp.y = i;
temp.x = j;
return 0;
}
return 1;
}
int can_go(int x, int y)
{
if (x<1 || y<1 || x >= mx * 2 + 1 || y >= my * 2 + 1)
return 0;
else if (maze[y][x]>0)
return 0;
return 1;
}
int can_set(int x, int y)
{
if (x<1 || y<1 || x >= mx * 2 || y >= my * 2)
return 0;
else if (maze[y][x]<1)
return 0;
return 1;
}
void print_maze()
{
int i, j, x, y;
x = mx * 2 + 1;
y = my * 2 + 1;
for (i = 0; i<y; i++)
{
for (j = 0; j<x; j++)
{
if (maze[i][j] == WALL)
{
printf("#");
}
else
printf(" ");
}
printf("\n");
}
}
int make_map()
{
int step, i, direction_temp, count = 0, d;
maze[maker.y][maker.x] = sign;
for (i = 0; i<4; i++)
{
if (can_go(maker.x + 2 * direction[i].x, maker.y + 2 * direction[i].y))
{
count++;
}
}
if (count == 0)
return 0;
step = rand() % 3 + 1;
direction_temp = rand() % 4;
for (i = 0; i<step; i++)
{
if (can_go(maker.x + 2 * direction[direction_temp].x, maker.y + 2 * direction[direction_temp].y))
{
maze[maker.y + direction[direction_temp].y][maker.x + direction[direction_temp].x] = sign;
maze[maker.y + 2 * direction[direction_temp].y][maker.x + 2 * direction[direction_temp].x] = sign;
maker.x += 2 * direction[direction_temp].x;
maker.y += 2 * direction[direction_temp].y;
}
else
{
d = rand() % 4;
direction_temp = d;
}
}
return 1;
}
int com_map()
{
int i;
for (i = 0; i<4; i++)
{
if (can_set(maker.x + 2 * direction[i].x, maker.y + 2 * direction[i].y))
{
maze[maker.y + direction[i].y][maker.x + direction[i].x] = sign + 1;
return 1;
}
}
return 0;
}
int make_map_dps()
{
int step, i, direction_temp, count = 0, d;
maze[maker.y][maker.x] = 1;
if (maker_n == 100)
{
printf_pad();
getchar();
closegraph();
exit(0);
}
for (i = 0; i<4; i++)
{
if (can_go(maker.x + 2 * direction[i].x, maker.y + 2 * direction[i].y))
{
count++;
}
}
if (count == 0)
return 0;
step = rand() % 3 + 1;
direction_temp = rand() % 4;
for (i = 0; i<step; i++)
{
if (can_go(maker.x + 2 * direction[direction_temp].x, maker.y + 2 * direction[direction_temp].y))
{
maze[maker.y + direction[direction_temp].y][maker.x + direction[direction_temp].x] = sign;
maze[maker.y + 2 * direction[direction_temp].y][maker.x + 2 * direction[direction_temp].x] = sign;
maker.x += 2 * direction[direction_temp].x;
maker.y += 2 * direction[direction_temp].y;
maker_step[++i_maker_step] = direction_temp + 1;
maker_n++;
}
else
{
d = rand() % 4;
direction_temp = d;
}
}
return 1;
}
int move_back()
{
int i;
if (i_maker_step<0 || i_maker_step >= mx*my)
exit(0);
i = maker_step[i_maker_step--] - 1;
maker.y = maker.y - 2 * direction[i].y;
maker.x = maker.x - 2 * direction[i].x;
return 1;
}
int main()
{
int i, j, x, y, s, e, maker_total;
int *p_temp;
char str[10];
setinitmode(NULL);
initgraph(400, 300);
setcaption("MAZE");
inputbox_getline("请输入迷宫的大小", "请输入一个数字,输入完请回车", str, sizeof(str) / sizeof(*str));
mx = my = atoi(str);
if (mx<1 || mx>200)
{
mx = MX;
my = MY;
_x = _X;
_y = _Y;
}
else
{
_x = mx * 2 + 1;
_y = my * 2 + 1;
}
maker_total = mx*my;
x = mx * 2 + 1;
y = my * 2 + 1;
closegraph();
maker_step = (int *)calloc(mx*my, sizeof(int));
maze = (int **)calloc(y, sizeof(int *));
p_temp = (int *)calloc(x*y, sizeof(int));
for (i = 0; i<y; i++)
{
maze[i] = p_temp + x*i;
}
for (i = 0; i<y; i++)
{
for (j = 0; j<x; j++)
{
maze[i][j] = !(j % 2) || !(i % 2) || i == 0 || i == y - 1 ? WALL : 0;
}
}
maker.y = 1;
maker.x = 1;
srand(time(NULL));
while (maker_n != maker_total)
{
while (make_map_dps())
;
move_back();
}
/* while (!is_map_created())
{
while (make_map())
;
sign++;
maker = temp;
if (sign >= 3)
com_map();
}
maker = temp;
if (sign >= 3)
com_map();*/
for (i = 0; i<y; i++)
for (j = 0; j<x; j++)
if (maze[i][j]>0)
maze[i][j] = 0;
srand(time(NULL));
s = rand() % (mx);
e = rand() % (mx);
explorer.x = 2 * s + 1;
explorer.y = 0;
maze[0][2 * s + 1] = START;
maze[y - 1][2 * e + 1] = END;
printf_pad();
//move(1);
return 0;
}