回 帖 发 新 帖 刷新版面

主题:[讨论]自己做的迷宫程序(C语言),有图形动画效果

几点说明:
1.本程序是动态的,运行后自动寻找迷宫出路
2.有什么不懂的可以在本贴留言.
3.本程序对C语言刚学完的有很大的意义.
4.四周是墙,坐标(1,1)是入口,右下脚是出口
5.若有不懂,联系QQ:274145322.
声明:本程序用VC调试是无法通过的需要修改
     本程序调试工具是TC.....................
     有些同志们抱怨没有注释,有注释就学不到东西了,查阅资料是非常重要的能力.
6.今日特加上注释以供大家学习。
#include "graphics.h"
#include "dos.h"
#include "stdlib.h"
#include "process.h"

#define MAX_COL 14/*定义迷宫大小*/
#define MAX_ROW 14

typedef struct
{ int vert;
  int horiz;
}offsets;


mapture(int i,int j,int k);/*标记迷宫,(i,j)标记为k模式*/
initmaze();/*初始化迷宫数组*/
findmaze(int i,int j);/*找到了(i,j)可走,标记*/
mapmaze();/*画出原始迷宫*/
int findpath(int row,int col);/*递归函数,找出迷宫路径*/
mapbar();/*画出方格*/
initgrap();/*初始化VGA*/
print();/*迷宫走完后,输出是否成功 */


int startx=50,starty=50;/*画图的屏幕坐标*/
int maze[MAX_ROW][MAX_COL];
offsets move[8]={{0,1},{1,1},{-1,1},{1,0},{-1,0},{0,-1},{1,-1},{-1,-1}}; /*8个方向寻找*/


initmaze()/*初始化迷宫数组 */
{ int i,j;

  for(i=0;i<MAX_ROW;i++)/*迷宫四周设置为1 代表墙*/
    { maze[i][0]=1;
      maze[i][MAX_COL-1]=1;
     }
  for(i=0;i<MAX_COL;i++)
    {  maze[0][i]=1;
       maze[MAX_ROW-1][i]=1;
     }
     randomize();
  for(i=1;i<MAX_ROW-1;i++)/*迷宫图形随机产生 1表示不通 0表示可行*/
     for(j=1;j<MAX_COL-1;j++)
       {
     maze[i][j]=random(2);
       }

}




findmaze(int i,int j)/*找到 (i,j)可走*/
{
   mapture(j,i,2);/*在图形上标记*/
            sleep(1);



}

returnmaze(int i,int j)/*找到(i,j)可走 ,但下一步无路走则标记*/
{

   mapture(j,i,3);/*在图形上标记*/
   sleep(1);
}
接后面

回复列表 (共53个回复)

21 楼

找迷宫出口你用的什么算法?是不是floodfill?

22 楼

#include "graphics.h"
e:\c++\c++实验\网上关于排序题目\不\1.cpp(1) : fatal error C1083: Cannot open include file: 'graphics.h': No such file or directory

23 楼


24 楼


25 楼


运行不了呀!请教一下

------Configuration: 迷宫 - Win32 Debug--------------------
Compiling...
迷宫.cpp
D:\程序\迷宫\迷宫.cpp(2) : error C2065: 'settextstyle' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(4) : error C2065: 'outtextxy' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(8) : warning C4508: 'print' : function should return a value; 'void' return type assumed
D:\程序\迷宫\迷宫.cpp(13) : error C2065: 'maze' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(13) : error C2109: subscript requires array or pointer type
D:\程序\迷宫\迷宫.cpp(13) : error C2109: subscript requires array or pointer type
D:\程序\迷宫\迷宫.cpp(13) : error C2106: '=' : left operand must be l-value
D:\程序\迷宫\迷宫.cpp(14) : error C2065: 'mapture' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(15) : error C2065: 'sleep' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(17) : error C2065: 'move' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(17) : error C2109: subscript requires array or pointer type
D:\程序\迷宫\迷宫.cpp(17) : error C2228: left of '.vert' must have class/struct/union type
D:\程序\迷宫\迷宫.cpp(18) : error C2109: subscript requires array or pointer type
D:\程序\迷宫\迷宫.cpp(18) : error C2228: left of '.horiz' must have class/struct/union type
D:\程序\迷宫\迷宫.cpp(19) : error C2109: subscript requires array or pointer type
D:\程序\迷宫\迷宫.cpp(19) : error C2109: subscript requires array or pointer type
D:\程序\迷宫\迷宫.cpp(20) : error C2109: subscript requires array or pointer type
D:\程序\迷宫\迷宫.cpp(20) : error C2109: subscript requires array or pointer type
D:\程序\迷宫\迷宫.cpp(20) : error C2106: '=' : left operand must be l-value
D:\程序\迷宫\迷宫.cpp(21) : error C2065: 'findmaze' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(22) : error C2065: 'MAX_ROW' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(22) : error C2065: 'MAX_COL' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(24) : error C2065: 'getch' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(25) : error C2065: 'exit' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(29) : error C2109: subscript requires array or pointer type
D:\程序\迷宫\迷宫.cpp(29) : error C2109: subscript requires array or pointer type
D:\程序\迷宫\迷宫.cpp(29) : error C2106: '=' : left operand must be l-value
D:\程序\迷宫\迷宫.cpp(30) : error C2065: 'returnmaze' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(43) : error C2065: 'starty' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(43) : error C2065: 'startx' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(47) : error C2065: 'setfillstyle' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(47) : error C2065: 'SOLID_FILL' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(47) : error C2065: 'YELLOW' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(48) : error C2065: 'bar' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(56) : warning C4508: 'mapbar' : function should return a value; 'void' return type assumed
D:\程序\迷宫\迷宫.cpp(63) : error C2065: 'detectgraph' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(64) : error C2065: 'initgraph' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(65) : warning C4508: 'initgrap' : function should return a value; 'void' return type assumed
D:\程序\迷宫\迷宫.cpp(71) : error C2373: 'mapture' : redefinition; different type modifiers
D:\程序\迷宫\迷宫.cpp(74) : error C2065: 'BLUE' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(78) : error C2065: 'RED' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(82) : error C2065: 'setcolor' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(82) : error C2065: 'GREEN' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(83) : error C2065: 'circle' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(85) : error C2065: 'floodfill' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(88) : warning C4508: 'mapture' : function should return a value; 'void' return type assumed
D:\程序\迷宫\迷宫.cpp(97) : error C2109: subscript requires array or pointer type
D:\程序\迷宫\迷宫.cpp(97) : error C2109: subscript requires array or pointer type
D:\程序\迷宫\迷宫.cpp(101) : warning C4508: 'mapmaze' : function should return a value; 'void' return type assumed
D:\程序\迷宫\迷宫.cpp(108) : error C2065: 'initmaze' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(110) : error C2109: subscript requires array or pointer type
D:\程序\迷宫\迷宫.cpp(110) : error C2109: subscript requires array or pointer type
D:\程序\迷宫\迷宫.cpp(110) : error C2106: '=' : left operand must be l-value
D:\程序\迷宫\迷宫.cpp(111) : error C2109: subscript requires array or pointer type
D:\程序\迷宫\迷宫.cpp(111) : error C2109: subscript requires array or pointer type
D:\程序\迷宫\迷宫.cpp(111) : error C2106: '=' : left operand must be l-value
D:\程序\迷宫\迷宫.cpp(115) : error C2109: subscript requires array or pointer type
D:\程序\迷宫\迷宫.cpp(115) : error C2109: subscript requires array or pointer type
D:\程序\迷宫\迷宫.cpp(115) : error C2106: '=' : left operand must be l-value
D:\程序\迷宫\迷宫.cpp(121) : error C2065: 'closegraph' : undeclared identifier
D:\程序\迷宫\迷宫.cpp(122) : warning C4508: 'main' : function should return a value; 'void' return type assumed
D:\程序\迷宫\迷宫.cpp(122) : error C2143: syntax error : missing ';' before '.'
执行 cl.exe 时出错.

迷宫.obj - 1 error(s), 0 warning(s)

26 楼

.......
   程序的注释少了点,感觉有些地方不够严谨。
         搂主辛苦了!

27 楼

25楼的朋友
  出现这么多的错,可能是你的编译器的问题哈,用TC编译,没有问题!

28 楼

请教高手,我用的是  microsoft visual c++ 6.0,为什么不能运行?

29 楼

我在TC里面编译怎么编译不过,说不能生成.obj文件

30 楼


vc 没graphics.h

我来回复

您尚未登录,请登录后再回复。点此登录或注册