C 语言实现迷宫 DFS算法

原创
2013/11/28 22:43
阅读数 5.6K

    上周学校布置的数据结构教学作业是生成一个迷宫,并且判断是否有通路,于是我索性做了一个迷宫游戏,第一次写这样长的代码,没有组织好,可能在可读性上不是很好,另外应该可以改进很多地方,比如说goto语句的替换,实现DFS时比较早,用了较多的goto,后来就限制使用了,总之这个程序里还是学到了很多,有可以改进的地方,欢迎大家留言批评指正。

先上一个图

后面俩行提示是对齐的,这个截图是较早时搞的,代码里改过了,就是俩个空格的问题,起点是雌性图标,终点是雄性图表。

再看另外一个风格:

这些都是在搜狗输入法特殊符号里找的,试用过很多的符号,最好保留了这俩个。

在开始界面:

 

 

 

在运行过程中如果 F1 ,则输出路线:

 

当然,也可以ESC退出。

下面上代码吧:(之前写的原版本,就不改了,VS2013平台)

先是头文件:headfile.h

/*2013年11月中下旬完成该代码设计,其中可能有一些多余的代码,本来打算用那些知识点来实现,后来没有用到,不过保留着的,也有些事注释掉的
如果写上会增加其他功能,或许在其他程序中会用到,留在备参考
本程序实现的功能:
1:浏览深度优先算法生成的迷宫,得到走迷宫的路线;
2:游戏模式,一个小人走迷宫,寻找另外一个小人,可以中途ESC键退出或F1键寻求帮助*/

#define ERROR 0
#define OVERFLOW 0
#define UNDERFLOW 0
#define OK 1
#define MAX_ARRAY_DIM 3
#define STACKINCREMENT 10
#define STACK_INIT_SIZE 50

typedef int SElemType;
typedef int ElemType;
typedef   struct
{
 SElemType *base;
 SElemType *top;
 int stackSize;
}sqStack;
typedef struct      //坐标,走迷宫时用
{
 int x;
 int y;
}*Pos;
typedef struct Node //存储路线坐标
{
 Pos data;
 Node *next;
}Node, *LinkType;
typedef struct      //存储路线
{
 LinkType top;
 LinkType base;
 int size;
}*Stack;

int visit(int, int);
void make_maze(int**, int);
void Initarray(int **e, int n);
int InitStack(sqStack *s);  //初始化栈,分配50个容量的栈
int Deep_F_S(int **arra_y);
void Push(sqStack *s, ElemType e);
int Pop(sqStack *s);
int text(int **maze, int n);
void print_maze(int **p, int n);
void print_li(int **maze, int n);
void print_num(int**maze, int n);
void print_zf(int **maze, int n);
int road(int **maze, int n);//检测是否有路
int Maze_Path(int **maze, int n, Pos start, Pos end);
int control(char ch);
int Game(int**maze, int n);
void gotoxy(int x, int y);
int Get_n(int **p);

main.cpp

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include<windows.h>
#include "headfile.h"
int n = 0;
static int i = 0, j = 0;
int main()
{
 printf("design by hutao!\n2013\\11\\24\n");
 system("pause");
 system("cls");

 HANDLE color;
 color = GetStdHandle(STD_OUTPUT_HANDLE);
 SetConsoleTextAttribute(color, FOREGROUND_GREEN);
 //SetConsoleTextAttribute(color, BACKGROUND_RED | BACKGROUND_INTENSITY);

 Pos start, end;
 start = (Pos)malloc(sizeof(Pos));
 end = (Pos)malloc(sizeof(Pos));

 static int **p;

 printf("1:game\t2:visit\n");
 int c = 0;
 scanf_s("%d", &c);

PRI:
 printf("plsese input the size of this maze you want:\t");
 scanf_s("%d", &n);                        //how many lines of this maze

 if (n < 5)
 {
  system("cls");
  printf("It's too small!\n");
  goto PRI;
 }
 else if (n % 2 == 0)
 {
  system("cls");
  printf("please input odd!\n");
  goto PRI;
 }
 else if (n > 35)
 {
  printf("It's too hard! are you sure? 1:yes 2:no\t");
  int a = 0;
  scanf_s("%d", &a);
  while (a != 1)
   goto PRI;
 }
 else if (n < 15)
 {
  printf("It's too easy! are you sure? 1:yes 2:no\t");
  int a = 0;
  scanf_s("%d", &a);
  while (a != 1)
   goto PRI;
 }

 p = (int **)malloc(sizeof(int *)* n);
 for (i = 0; i < n; i++)
  p[i] = (int *)malloc(sizeof(int)* n); //get the momery of the array

 make_maze(p, n);                            //make this erray

 end->x = end->y = n - 2; //设置入口和出口
 start->y = start->x = 1;

 switch (c) //选择功能,是走迷宫游戏还是看迷宫生成
 {
 case 1: Game(p, n);
  if (GetAsyncKeyState(VK_F1))
  {
   Maze_Path(p, n, start, end);
   print_maze(p, n);
   system("pause"); return OK;
  }
  else
   system("pause"); return OK;
 case 2:print_maze(p, n); break;                           //output the maze you make
 default: printf("don't do this!\n");
 } 
 if (Maze_Path(p, n, start, end) == ERROR)
  printf("\nThis maze haven't path!\n");
 else
 {
  printf("\nThe maze has the path to end!\nDo you want to get the path?\n1: yes 2: no\t");
  int a;
  scanf_s("%d", &a);
  switch (a)
  {
  case 1: system("cls"); print_maze(p, n); break;
  case 2: break;
  }
 }

 system("pause");
 return 0;
}

fun.cpp

 

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <stdarg.h>
#include<windows.h>
#include "headfile.h"

extern int n;
int x = 1;
static int i = 0, j = 0;
sqStack  *visited_x = (sqStack*)malloc(sizeof(sqStack));
sqStack  *visited_y = (sqStack*)malloc(sizeof(sqStack));

void make_maze(int **maze, int n)
{
 int i = 0, j = 0;
 int a = 0;
 for (i = 0; i < n; i++)
 {
  for (j = 0; j < n; j++)
  {

   if (i % 2 == 1 && j % 2 == 1)
    maze[i][j] = 0;
   else
    maze[i][j] = 1;
  }
 }
 InitStack(visited_x);
 InitStack(visited_y);

 time_t t;
 srand((unsigned)time(&t));
 a += (int)(rand());//随机生成起点
 switch (a)
 {
 case 0:i = (n - 1) / 2; j = (n - 1) / 2; break;
 case 1:i = (n - 1) / 2 - 2; j = (n - 1) / 2; break;
 case 2:i = (n - 1) / 2 + 2; j = (n - 1) / 2; break;
 case 3:i = (n - 1) / 2; j = (n - 1) / 2 - 2; break;
 case 4:i = (n - 1) / 2; j = (n - 1) / 2 + 2; break;
 }
 Push(visited_x, i);
 Push(visited_y, j); //生成起点入栈 
 Deep_F_S(maze);
}
void print_maze(int**maze, int n)
{
 //输出迷宫
 int a = 3;
 for (i = 0; i < n; i++)
 {
  for (j = 0; j < n; j++)
  {
   if (maze[i][j] == 4)
    maze[i][j] = 0;
  }
 }
PR:
 printf("please choose the style you want:\n1 :¤\n2 :╔\n3 : 0、1、2\n");
 scanf_s("%d", &a);
 switch (a)
 {
 case 1: system("cls"); print_zf(maze, n); break;
 case 2: system("cls"); print_li(maze, n); break;
 case 3: system("cls"); print_num(maze, n); break;
 default:system("cls"); printf("please input correct choise!\n"); goto PR;
 }

 gotoxy(4, 2);
 printf("◎");
 gotoxy(2 * n - 2, n - 1);
 printf("♂");
 printf("\n\n");
}
void print_zf(int **maze, int n)
{
 int m, o;
 printf("\n");
 for (m = 0; m < n; m++)
 {
  printf("  ");
  for (o = 0; o < n; o++)
  {
   switch (maze[m][o])
   {
   case 1: printf("¤"); break;
   case 0:printf("  "); break;
   case 2:printf("+ "); break;
   }
  }
  printf("\n");
 }
 printf("\n");
}
void print_num(int**maze, int n)
{
 int m, o;
 printf("\n");
 for (m = 0; m < n; m++)
 {
  printf("  ");
  for (o = 0; o < n; o++)
  {
   printf("%d ", maze[m][o]);
  }
  printf("\n");
 }
 printf("\n");
}
void print_li(int **maze, int n)
{
 int m = 0, o = 0;
 printf("\n");
 for (m = 0; m < n; m++)
 {
  printf("  ");
  for (o = 0; o < n; o++)
  {
   if (m == 0 && o == 0)
    printf("╔");
   else if (m == n - 1 && o == n - 1)
    printf("╝");
   else if (m == 0 && o == n - 1)
    printf("╗");
   else if (m == n - 1 && o == 0)
    printf("╚");
   else if (o == 0 && m>0 && m < n - 1)
   {
    if (maze[m][o + 1] != 1)
     printf("║");
    else
     printf("╠");
   }
   else if (o == n - 1 && m>0 && m < n - 1)
   {
    if (maze[m][o - 1] != 1)
     printf("║");
    else
     printf("╣");
   }
   else if (m == 0 && o>0 && o < n - 1)
   {
    if (maze[m + 1][o] != 1)
     printf("═");
    else
     printf("╦");
   }
   else if (m == n - 1 && o>0 && o < n - 1)
   {
    if (maze[m - 1][o] != 1)
     printf("═");
    else
     printf("╩");
   }
   else if (m>0 && o > 0 && m < n - 1 && o < n - 1)//除了四周
   {
    if (maze[m][o] == 1)
    {
     if (maze[m - 1][o] == 1 && maze[m][o - 1] == 1 && maze[m + 1][o] != 1 && maze[m][o + 1] != 1)//上左下右
      printf("╝");
     if (maze[m - 1][o] == 1 && maze[m][o - 1] != 1 && maze[m + 1][o] != 1 && maze[m][o + 1] == 1)
      printf("╚");
     if (maze[m - 1][o] != 1 && maze[m][o - 1] != 1 && maze[m + 1][o] == 1 && maze[m][o + 1] == 1)
      printf("╔");
     if (maze[m - 1][o] != 1 && maze[m][o - 1] == 1 && maze[m + 1][o] == 1 && maze[m][o + 1] != 1)
      printf("╗");
     if (maze[m - 1][o] != 1 && maze[m][o - 1] != 1 && maze[m + 1][o] != 1 && maze[m][o + 1] == 1)
      printf("═");
     if (maze[m - 1][o] != 1 && maze[m][o - 1] == 1 && maze[m + 1][o] != 1 && maze[m][o + 1] != 1)
      printf("═");
     if (maze[m - 1][o] == 1 && maze[m][o - 1] != 1 && maze[m + 1][o] != 1 && maze[m][o + 1] != 1)
      printf("║");
     if (maze[m - 1][o] != 1 && maze[m][o - 1] != 1 && maze[m + 1][o] == 1 && maze[m][o + 1] != 1)
      printf("║");
     if (maze[m - 1][o] == 1 && maze[m][o - 1] == 1 && maze[m + 1][o] == 1 && maze[m][o + 1] == 1)
      printf("╬");
     if (maze[m - 1][o] == 1 && maze[m + 1][o] == 1)//上下是墙
     {
      if (maze[m][o - 1] == 1 && maze[m][o + 1] != 1)
       printf("╣");
      else if (maze[m][o + 1] == 1 && maze[m][o - 1] != 1)
       printf("╠");
      else if (maze[m][o + 1] != 1 && maze[m][o - 1] != 1)
       printf("║");
     }
     if (maze[m][o - 1] == 1 && maze[m][o + 1] == 1)//左右是墙
     {
      if (maze[m - 1][o] == 1 && maze[m + 1][o] != 1)
       printf("╩");
      else if (maze[m + 1][o] == 1 && maze[m - 1][o] != 1)
       printf("╦");
      else if (maze[m + 1][o] != 1 && maze[m - 1][o] != 1)
       printf("═");
     }
    }


   }
   if (maze[m][o] == 0)
    printf("  ");
   else if (maze[m][o] == 2)
    printf("* ");
  }
  printf("\n");
 }
 printf("\n");
}
int Deep_F_S(int **maze)
{
S:while (x == 0)
{
   goto X;
}
  int i = 1;
  int j = 1;
X: x = 0;
  int a = 0; //在随机数时用作存储容器
IF:
  if (i == 1 && j == 1 && (maze[i + 2][j] == 0 || maze[i][j + 2] == 0))
  { //左上
   if (maze[i][j + 2] == 0)
   {
    maze[i][j] = 2;
    maze[i][j + 1] = 0;
    Push(visited_x, i);
    Push(visited_y, j);
    j += 2;
   }
   else
   {
    maze[i][j] = 2;
    maze[i + 1][j] = 0;
    Push(visited_x, i);
    Push(visited_y, j);
    i += 2;
   }
   goto IF;
  }
  else if (i == 1 && j == 1)  //上一条语句执行失败才会执行这条语句
  {
   goto AA;
  }
  if (i == n - 2 && j == n - 2 && (maze[n - 4][n - 2] == 0 || maze[n - 2][n - 4] == 0))
  {//右下
   if (maze[n - 2][n - 4] == 0)
   {
    maze[i][j] = 2;
    maze[i][j - 1] = 0;
    Push(visited_x, i);
    Push(visited_y, j);
    j -= 2;
   }
   else
   {
    maze[i][j] = 2;
    maze[i - 1][j] = 0;
    Push(visited_x, i);
    Push(visited_y, j);
    i -= 2;
   }
   goto IF;
  }
  else if (i == n - 2 && j == n - 2)
  {
   goto AA;
  }
  if (i == 1 && j == n - 2 && (maze[i + 2][j] == 0 || maze[i][j - 2] == 0))
  {//右上
   if (maze[i + 2][j] == 0)
   {
    maze[i][j] = 2;
    maze[i + 1][j] = 0;
    Push(visited_x, i);
    Push(visited_y, j);
    i += 2;
   }
   else
   {
    maze[i][j] = 2;
    maze[i][j - 1] = 0;
    Push(visited_x, i);
    Push(visited_y, j);
    j -= 2;
   }
   goto IF;
  }
  else if (i == 1 && j == n - 2)
  {
   goto AA;
  }
  if (i == n - 2 && j == 1 && (maze[i - 2][j] == 0 || maze[i][j + 2] == 0))
  {//左下
   if (maze[i - 2][j] == 0)
   {
    maze[i][j] = 2;
    maze[i - 1][j] = 0;
    Push(visited_x, i);
    Push(visited_y, j);
    i -= 2;
   }
   else
   {
    maze[i][j] = 2;
    maze[i][j + 1] = 0;
    Push(visited_x, i);
    Push(visited_y, j);
    j += 2;
   }
   goto IF;
  }
  else if (i == n - 2 && j == 1)
  {
   goto AA;
  }
  if (i == 1 && (maze[i + 2][j] == 0 || maze[i][j - 2] == 0 || maze[i][j + 2] == 0))
  {//上
   if (maze[i + 2][j] == 0)
   {
    maze[i][j] = 2;
    maze[i + 1][j] = 0;
    Push(visited_x, i);
    Push(visited_y, j);
    i += 2;
    goto IF;
   }
   else if (maze[i][j + 2] == 0)
   {
    maze[i][j] = 2;
    maze[i][j + 1] = 0;
    Push(visited_x, i);
    Push(visited_y, j);
    j += 2;
    goto IF;
   }
   else if (maze[i][j - 2] == 0)
   {
    maze[i][j] = 2;
    maze[i][j - 1] = 0;
    Push(visited_x, i);
    Push(visited_y, j);
    j -= 2;
    goto IF;
   }
   else
    printf("ERROR");
  }
  else if (i == 1)
  {
   goto AA;
  }
  if (i == n - 2 && (maze[i - 2][j] == 0 || maze[i][j - 2] == 0 || maze[i][j + 2] == 0))
  {//下 
   if (maze[i - 2][j] == 0)
   {
    maze[i][j] = 2;
    maze[i - 1][j] = 0;
    Push(visited_x, i);
    Push(visited_y, j);
    i -= 2;
    goto IF;
   }
   else if (maze[i][j - 2] == 0)
   {
    maze[i][j] = 2;
    maze[i][j - 1] = 0;
    Push(visited_x, i);
    Push(visited_y, j);
    j -= 2;
    goto IF;
   }
   else if (maze[i][j + 2] == 0)
   {
    maze[i][j] = 2;
    maze[i][j + 1] = 0;
    Push(visited_x, i);
    Push(visited_y, j);
    j += 2;
    goto IF;
   }
   else
    printf("ERROR");
  }
  else if (i == n - 2)
  {
   goto AA;
  }
  if (j == 1 && (maze[i][j + 2] == 0 || maze[i - 2][j] == 0 || maze[i + 2][j] == 0))
  {//左
   if (maze[i][j + 2] == 0)
   {
    maze[i][j] = 2;
    maze[i][j + 1] = 0;
    Push(visited_x, i);
    Push(visited_y, j);
    j += 2;
    goto IF;
   }
   else if (maze[i - 2][j] == 0)
   {
    maze[i][j] = 2;
    maze[i - 1][j] = 0;
    Push(visited_x, i);
    Push(visited_y, j);
    i -= 2;
    goto IF;
   }
   else if (maze[i + 2][j] == 0)
   {
    maze[i][j] = 2;
    maze[i + 1][j] = 0;
    Push(visited_x, i);
    Push(visited_y, j);
    i += 2;
    goto IF;
   }
   else
    printf("ERROR");
  }
  else if (j == 1)
  {
   goto AA;
  }
  if (j == n - 2 && (maze[i][j - 2] == 0 || maze[i - 2][j] == 0 || maze[i + 2][j] == 0))
  {//右
   if (maze[i][j - 2] == 0)
   {
    maze[i][j] = 2;
    maze[i][j - 1] = 0;
    Push(visited_x, i);
    Push(visited_y, j);
    j -= 2;
    goto IF;
   }
   else if (maze[i + 2][j] == 0)
   {
    maze[i][j] = 2;
    maze[i + 1][j] = 0;
    Push(visited_x, i);
    Push(visited_y, j);
    i += 2;
    goto IF;
   }
   else if (maze[i - 2][j] == 0)
   {
    maze[i][j] = 2;
    maze[i - 1][j] = 0;
    Push(visited_x, i);
    Push(visited_y, j);
    i -= 2;
    goto IF;
   }
   else
    printf("ERROR");
  }
  else if (j == n - 2)
  {
   goto AA;
  }
  if (maze[i + 2][j] == 0 || maze[i - 2][j] == 0 || maze[i][j - 2] == 0 || maze[i][j + 2] == 0)
  {
  SW:
   a = (int)rand() % 4; // 生成随机数

   switch (a)
   {
   case 0:
   {
    if (maze[i - 2][j] == 0) //up
    {
     maze[i][j] = 2;
     maze[i - 1][j] = 0;
     Push(visited_x, i);
     Push(visited_y, j);
     i -= 2;
     goto IF;
    }
    else
     goto SW;  //随机种子

   }     break;
   case 1:
   {
    if (maze[i][j - 2] == 0)//left
    {
     maze[i][j] = 2;
     maze[i][j - 1] = 0;
     Push(visited_x, i);
     Push(visited_y, j);
     j -= 2;
     goto IF;
    }
    else
     goto SW;  //随机种子
   }   break;
   case 2:
   {
    if (maze[i + 2][j] == 0)//down
    {
     maze[i][j] = 2;
     maze[i + 1][j] = 0;
     Push(visited_x, i);
     Push(visited_y, j);
     i += 2;
     goto IF;
    }
    else
     goto SW;  //随机种子
   }   break;
   case 3:
   {
    if (maze[i][j + 2] == 0)
    {
     maze[i][j] = 2;
     maze[i][j + 1] = 0;
     Push(visited_x, i);
     Push(visited_y, j);
     j += 2;
     goto IF;
    }
    else
     goto SW;  //随机种子
   } break;
   default: printf("ERROR");
   }

  }
AA:while (text(maze, n) == 0)
{
    maze[i][j] = 2;
    i = Pop(visited_x);
    j = Pop(visited_y);
    goto S;
}
   for (i = 0; i < n; i++)
   {
    for (j = 0; j < n; j++)
    {
     if (maze[i][j] == 2)
      maze[i][j] = 0;
    }
   }

   return OK;
}
int InitStack(sqStack *s)  //初始化栈,分配50个容量的栈
{

 s->base = (ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));
 if (!(s->base))
  exit(0);   // 表示正常退出
 s->top = s->base;
 s->stackSize = STACK_INIT_SIZE;
 return OK;
}
void Push(sqStack *s, ElemType e)
{
 // 如果栈满,追加空间
 if ((s->top - s->base) >= (s->stackSize))
 {
  s->base = (ElemType *)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(ElemType));
  if (!s->base)
   exit(0);

  s->top = s->base + s->stackSize;              // 设置栈顶
  s->stackSize = s->stackSize + STACKINCREMENT; // 设置栈的最大容量
 }

 *((*s).top) = e;
 s->top++;
}
int Pop(sqStack *s)
{
 ElemType *e;
 e = (ElemType*)malloc(sizeof(ElemType));
 if (s->top == s->base)
  return 0;
 *e = *--(s->top);   // 将栈顶元素弹出并修改栈顶指针
 return *e;
}
int text(int **maze, int n)
{
 int i = 0, j = 0, a = 0;
 for (i = 0; i < n; i++)
 {
  for (j = 0; j < n; j++)
  {
   if (i % 2 == 1 && j % 2 == 1 && maze[i][j] == 0)
    return 0;
  }
 }
 return OK;
}
int Maze_Path(int **maze, int n, Pos start, Pos end)//如果是向左走了一步,那就一定会退回来,这个要控制刚过来的地方不能回去;
{
 visited_x->top = visited_x->base;
 visited_y->top = visited_y->base;
 Pos now;
 now = (Pos)malloc(sizeof(Pos));
 now->x = start->x;
 now->y = start->y;
 do
 {
  while (maze[now->x + 1][now->y] == 0 || maze[now->x][now->y + 1] == 0 ||
   maze[now->x - 1][now->y] == 0 || maze[now->x][now->y - 1] == 0)//如果有一个方向可通
  {
   if (maze[now->x + 1][now->y] == 0) //如果下边可通
   {
    maze[now->x][now->y] = 2; //留下足迹
    Push(visited_x, now->x);
    Push(visited_y, now->y);
    now->x++;
    if (now->x == end->x && now->y == end->y)
    {
     maze[now->x][now->y] = 2; //留下足迹
     return OK;
    }
    else
     continue;
   }
   if (maze[now->x][now->y + 1] == 0) //如果右边可通
   {
    maze[now->x][now->y] = 2; //留下足迹
    Push(visited_x, now->x);
    Push(visited_y, now->y);
    now->y++;
    if (now->x == end->x && now->y == end->y)
    {
     maze[now->x][now->y] = 2; //留下足迹
     return OK;
    }
    else
     continue;
   }
   if (maze[now->x][now->y - 1] == 0) //如果左边可通
   {
    maze[now->x][now->y] = 2; //留下足迹
    Push(visited_x, now->x);
    Push(visited_y, now->y);
    now->y--;
    if (now->x == end->x && now->y == end->y)
    {
     maze[now->x][now->y] = 2; //留下足迹
     return OK;
    }
    else
     continue;
   }
   if (maze[now->x - 1][now->y] == 0) //如果上边可通
   {
    maze[now->x][now->y] = 2; //留下足迹
    Push(visited_x, now->x);
    Push(visited_y, now->y);
    now->x--;
    if (now->x == end->x && now->y == end->y)
    {
     maze[now->x][now->y] = 2; //留下足迹
     return OK;
    }
    else
     continue;
   }
  }
  maze[now->x][now->y] = 4; //进了死胡同,则……
  now->x = Pop(visited_x);
  now->y = Pop(visited_y);
 } while (now->x != start->x&&now->y != start->y);
 return ERROR;
}
int Game(int**maze, int n)
{
 int a = 0;
 system("cls");
 printf("direction:↑\t↓\t←\t→\nhelp: F1\nquit: ESC\n"); 
 system("pause"); 
 system("cls");
 print_maze(maze, n);

 char ch = '0';
 Pos now;
 Pos start;
 Pos end;
 now = (Pos)malloc(sizeof(Pos));
 now->x = 1;
 now->y = 1;//起点位置
 start = (Pos)malloc(sizeof(Pos));
 start->x = 1;
 start->y = n - 2;
 end = (Pos)malloc(sizeof(Pos));
 end->x = n - 2;
 end->y = n - 2;

 gotoxy((now->x + 1) * 2, now->y + 1);
 printf("♀");  //输出起点;

 gotoxy(1, end->x + 3);
 printf("direction:↑\t↓\t←\t→\n help: F1\n quit: ESC\n");
 gotoxy(1, end->x + 6); //隐藏光标

 while (now->x != n - 2 || now->y != n - 2)//当没有到达终点时
 {
  if (GetAsyncKeyState(VK_UP))//四个if判断是否有方向键输入
  {
   if (maze[now->x - 1][now->y] == 0)
   {
    gotoxy((now->y + 1) * 2, now->x + 1 - 1);
    printf("♀");
    gotoxy((now->y + 1) * 2, now->x + 1);
    printf("  ");
    now->x--;
    gotoxy(0, end->x + 6);
    Sleep(100);
   }
  }
  else if (GetAsyncKeyState(VK_DOWN))
  {
   if (maze[now->x + 1][now->y] == 0)//下边可以走,则相应位置输出小人,现在位置输出空格
   {
    gotoxy((now->y + 1) * 2, now->x + 1 + 1); //上边的x加一是变化行所以这里对应的列加以一;单加一只是不变,再加一才是加一
    printf("♀");
    gotoxy((now->y + 1) * 2, now->x + 1);   //横坐标的变化要俩倍是因为涉及非英文字符的输出迷宫
    printf("  ");
    now->x++;
    gotoxy(0, end->x + 6);
    Sleep(100);
   }
  }
  else if (GetAsyncKeyState(VK_LEFT))
  {
   if (maze[now->x][now->y - 1] == 0)
   {
    gotoxy((now->y + 1) * 2 - 2, now->x + 1);
    printf("♀");
    gotoxy((now->y + 1) * 2, now->x + 1);
    printf("  ");
    now->y--;
    gotoxy(0 , end->x + 6);
    Sleep(100);
   }
  }
  else if (GetAsyncKeyState(VK_RIGHT))
  {
   if (maze[now->x][now->y + 1] == 0)
   {
    gotoxy((now->y + 1) * 2 + 2, now->x + 1);
    printf("♀");
    gotoxy((now->y + 1) * 2, now->x + 1);
    printf("  ");
    now->y++;
    gotoxy(0, end->x + 6);
    Sleep(100);
   }
  }
  
  else if (GetAsyncKeyState(VK_F1))
  {
   system("cls");
   return OK;
  }
  else if (GetAsyncKeyState(VK_ESCAPE))
  {
   system("cls");
   printf("COME ON!\n");
   return OK;
  }
  
 }
 printf("\n\n\n\t\t\tYou are win!\n\t\t\t");

 return OK;

}
void gotoxy(int x, int y)
{
 COORD pos = { x, y };
 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
 SetConsoleCursorPosition(hOut, pos);
}

 以上就是完整的代码了,也可以直接到我的网盘里下载整个工程,如果是vs2013或12平台应该可以直接用

http://pan.baidu.com/share/link?shareid=56644180&uk=657817718&third=15

 

展开阅读全文
打赏
0
18 收藏
分享
加载中
HACK_ynu博主

引用来自“TMACkan”的评论

哎= =,LZ你的代码实现不错,但是阅读起来有点吃力。。。加油

嗯嗯,是的,当时写的时候自己水平不够,缺乏整体认识,所以有些乱,慢慢会改进的!

2014/04/07 23:37
回复
举报
哎= =,LZ你的代码实现不错,但是阅读起来有点吃力。。。加油
2013/12/23 20:27
回复
举报
HACK_ynu博主

引用来自“巴顿”的评论

楼主不错啊。

一般般啦,刚学不久,有待提升,,呵呵,
2013/12/02 12:59
回复
举报
楼主不错啊。
2013/11/30 09:40
回复
举报
更多评论
打赏
4 评论
18 收藏
0
分享
返回顶部
顶部