迷宫
图老师设计创意栏目是一个分享最好最实用的教程的社区,我们拥有最用心的各种教程,今天就给大家分享迷宫的教程,热爱PS的朋友们快点看过来吧!
作者:董乘宇程序目的:
输入一个任意大小的迷宫,用栈求出一条走出迷宫的路径,并
显示在屏幕上。
程序实现:
可以实现载入迷宫和保存迷宫,附带文件中有4个测试迷宫路径的
文件test1~4.dd。请将这些文件拷贝到TC当前目录下,或者在载
入时写明完全路径。由于屏幕大小的限制,当用户自己输入迷宫
时一定要注重:迷宫大小是有限制的,不小于4*3,不大于30*20。
否则会出现错误信息。输入开始时全是墙,用上下左右键移动,
用Del键删除墙,形成通路,用Enter键添加墙。输入结束时可以
将迷宫保存下来,以dd为扩展名。输入完毕时用F9键来得到结果,
找到路径时,屏幕下方会出现Path found,否则出现Path not found。
程序经Turbo C 2.0编译调试成功。运行时不用添加任何运行库。
不可以在VC上编译。
下载DOS版和windows版的迷宫游戏全部代码
----------------------------------------------------------------------------------
/*
MazePath Demo BY Turbo C 2.0 Copyright(c) RoverUnion. All right reserved. Filename: Maze.c Author Dongchengyu. Ver 1.10
*/#include stdio.h
#include stdlib.h
#include malloc.h
#include conio.h
#include dos.h#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0#define F9 0x43
#define Esc 0x1b
#define Del 0x53
#define Home 0x47
#define End 0x4f
#define Space 0x20
#define Up 0x48
#define Down 0x50
#define Left 0x4b
#define Right 0x4d
#define Enter 0x0d
#define F2 0x3c
#define F3 0x3d#define STACK_IN99v_SIZE 200
#define STACKINCREMENT 10typedef int Boolean;
typedef int Status;typedef strUCt {
int x;
int y;
} PosType;typedef struct {
int ord;
PosType seat;
int di;
} SElemType;typedef struct {
int td;
int foot;
int mark;
} MazeType;typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
} Stack;
int Maze[20][30];
MazeType maze[20][30];
PosType StartPlace;
PosType EndPlace;
int count;
int m,n;
Boolean b_start=FALSE,b_end=FALSE;void CreatMaze(void);
Status SaveMaze(char *filename);
Status LoadMaze(char *filename);
void Error(char *message);Status InitStack(Stack *s);
Status DestroyStack(Stack *s);
Status ClearStack(Stack *s);
Boolean StackEmpty(Stack *s);
int StackLength(Stack *s);
Status Push(Stack *s,SElemType e);
SElemType Pop(Stack *s,SElemType e);
Status GetTop(Stack *s,SElemType *e);
Status StackTraverse(Stack *s,Status (* visit)(SElemType *se));
Boolean Pass(PosType curpos);
void MarkPrint(PosType seat);
void FootPrint(PosType curpos);
PosType NextPos(PosType seat,int di);
Status MazePath(PosType start,PosType end);void CreatMaze(void)
/* Form the maze. */
{
void Error(char *message);
Status SaveMaze(char *filename);
Status LoadMaze(char *filename);
int i,j;
int x,y;
char c;
char savename[12],loadname[12];
Boolean flag=FALSE,load=FALSE;
clrscr();
printf("Menu:");
printf("1.Load Mazefile:(*.dd)");
printf("2.Input Maze:");
printf("Input your choice: ");
do
{
c=getch();
switch(c)
{
case ''''''''''''''''''''''''''''''''1'''''''''''''''''''''''''''''''': putch(''''''''''''''''''''''''''''''''1''''''''''''''''''''''''''''''''); break;
case ''''''''''''''''''''''''''''''''2'''''''''''''''''''''''''''''''': putch(''''''''''''''''''''''''''''''''2''''''''''''''''''''''''''''''''); break;
case Esc: sleep(1); exit(1);
default: break;
}
}
while(c!=''''''''''''''''''''''''''''''''1''''''''''''''''''''''''''''''''&&c!=''''''''''''''''''''''''''''''''2'''''''''''''''''''''''''''''''') ;
if(c==''''''''''''''''''''''''''''''''1'''''''''''''''''''''''''''''''')
{
printf("LoadName: ");
scanf("%s",loadname);
if(LoadMaze(loadname))
{
sleep(1); load=TRUE;
}
else { gotoxy(1,9); printf("Load fail! "); }
}
if(!load)
{
printf("Input the maze''''''''''''''''''''''''''''''''s size:");
printf("Input Length :");
scanf("%d",&m);
printf("Input Width :");
scanf("%d",&n);
if(m4n4) Error("Input");
if(m30n20) Error("Maze too large");
for(i=0;i30;i++)
for(j=0;j20;j++)
Maze[j][i]=2;
StartPlace.x=0;
StartPlace.y=0;
EndPlace.x=0;
EndPlace.y=0;
clrscr();
printf("");
for(i=1;i=n;i++)
{
for(j=1;j=m;j++)
{
printf(" #");
Maze[i-1][j-1]=0;
}
printf("");
}
}
gotoxy(65,5);
printf("''''''''''''''''''''''''''''''''#'''''''''''''''''''''''''''''''':Wall");
gotoxy(65,7);
printf("Start:Home");
gotoxy(65,9);
printf("End:End");
gotoxy(65,11);
printf("Delete Wall:Del");
gotoxy(65,13);
printf("Enter Wall:Enter");
gotoxy(65,15);
printf("Save Maze:F2");
gotoxy(65,17);
printf("Complete:F9");
gotoxy(65,19);
printf("Exit:Esc");
gotoxy(4,3);
x=4;y=3;
do
{
c=getch();
switch(c)
{
case Up: if(y3) { y--; gotoxy(x,y); }
break;
case Down: if(yn) { y++; gotoxy(x,y); }
break;
case Left: if(x4) { x-=2; gotoxy(x,y); }
break;
case Right: if(x2*m-2) { x+=2; gotoxy(x,y); }
break;
case Del: if(y-2==StartPlace.y&&x/2-1==StartPlace.x) b_start=FALSE