#include "stdio.h"
#include "conio.c"
typedef struct hero
{ int x,y,face;} HERO;
void set_hero(HERO* h,int x,int y,int face)
{h->x=x;h->y=y;h->face=face;}
void go(HERO* h)
{if(h->face%2) h->x+=2-h->face;
else h->y+=h->face-1;
printf("go: %d,%d,%d",h->x,h->y,h->face);}
void goleft(HERO* h)
{if(h->face%2) h->y+=h->face-2;
else h->x+=h->face-1;
printf("goleft: %d,%d,%d",h->x,h->y,h->face);}
void turnleft(HERO* h)
{h->face=(h->face+3)%4;
printf("turnleft: %d,%d,%d",h->x,h->y,h->face);}
void turnright(HERO* h)
{h->face=(h->face+1)%4;
printf("turnright: %d,%d,%d",h->x,h->y,h->face);}
void print_hero(HERO* h, int b)
{
printf("printf: %d,%d,%d",h->x,h->y,h->face);
getchar();
gotoxy(h->x + 1, h->y + 1);
if (b)
{
switch (h->face)
{
case 0: printf("%c", 24); break;
case 1: printf("%c", 16); break;
case 2: printf("%c", 25); break;
case 3: printf("%c", 27); break;
default: break;
}
}
else printf(" ");
}
int maze[10][10] =
{
0, 0, 0, 1, 0, 0, 0, 1, 0, 0,
1, 0, 1, 1, 0, 1, 1, 1, 1, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 1, 1, 0, 1, 1, 1,
0, 0, 1, 0, 1, 1, 0, 0, 0, 1,
1, 0, 1, 0, 1, 1, 0, 1, 0, 1,
0, 0, 1, 0, 1, 1, 0, 1, 0, 1,
0, 1, 1, 0, 0, 0, 0, 1, 0, 1,
0, 0, 0, 0, 1, 0, 1, 1, 0, 1,
0, 1, 1, 1, 1, 0, 0, 0, 0, 0
};
void print_maze()
{
int i, j;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
if (maze[i][j]) printf("%c", 219);
else printf(" ");
}
printf("\n");
}
}
int gomaze(HERO* h)
{
HERO t = *h; int i;
for (i = 0; i < 2; t = *h)
{
print_hero(h, 1); getchar(); sleep(1); go(&t); getchar();
if (t.x >= 0 && t.x < 10 && t.y >= 0 && t.y < 10 && !maze[t.y][t.x])
{
print_hero(h, 0); getchar(); go(h); getchar();/*前方可走则向前走*/
if (h->x == 9 && h->y == 9) return 1; goleft(&t);
if (h->x == 0 && h->y == 0) i++;
if (t.x >= 0 && t.x < 10 && t.y >= 0 && t.y < 10 && !maze[t.y][t.x]) turnleft(h);/*左方无墙向左转*/
}
else turnright(h);/*前方不可走向右转*/
}
return 0;
}
main()
{
HERO Tom;/*有个英雄叫Tom*/
set_hero(&Tom, 0, 0, 0);/*放在(0,0)面朝北*/
clrscr();
print_maze();
gomaze(&Tom);/*Tom走迷宫*/
}