程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> C語言基礎知識 >> 董乘宇--迷宮程序1.10版

董乘宇--迷宮程序1.10版

編輯:C語言基礎知識
程序目的:
   輸入一個任意大小的迷宮,用棧求出一條走出迷宮的路徑,並
   顯示在屏幕上。
   程序實現:
   可以實現載入迷宮和保存迷宮,附帶文件中有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版的迷宮游戲全部代碼
   用戶名:migong
   ----------------------------------------------------------------------------------
   /*
   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 10
   typedef 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(m<4n<4) Error("Input");
   if(m>30n>20) Error("Maze too large");
   for(i=0;i<30;i++)
   for(j=0;j<20;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(y>3) { y--; gotoxy(x,y); }
   break;
   case Down: if(y<n) { y++; gotoxy(x,y); }
   break;
   case Left: if(x>4) { x-=2; gotoxy(x,y); }
   break;
   case Right: if(x<2*m-2) { x+=2; gotoxy(x,y); }
   break;
   case Del: if(y-2==StartPlace.y&&x/2-1==StartPlace.x) b_start=FALSE;
   if(y-2==EndPlace.y&&x/2-1==EndPlace.x) b_end=FALSE;
   putch('''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''); Maze[y-2][x/2-1]=1; gotoxy(x,y);
   break;
   case Enter: if(y-2==StartPlace.y&&x/2-1==StartPlace.x) break;
   if(y-2==EndPlace.y&&x/2-1==EndPlace.x) break;
   putch(''''''''''''''''''''''''''''''''#''''''''''''''''''''''''''''''''); Maze[y-2][x/2-1]=0; gotoxy(x,y);
   break;
   case Home: if(Maze[y-2][x/2-1]&&!b_start)
   {
   StartPlace.x=x/2-1;
   StartPlace.y=y-2;
   putch(''''''''''''''''''''''''''''''''S'''''''''''''''''''''''''''''''');
   gotoxy(x,y);
   b_start=TRUE;
   }
   break;
   case End: if(Maze[y-2][x/2-1]&&!b_end)
   {
   EndPlace.x=x/2-1;
   EndPlace.y=y-2;
   putch(''''''''''''''''''''''''''''''''E'''''''''''''''''''''''''''''''');
   gotoxy(x,y);
   b_end=TRUE;
   }
   break;
   case Esc: gotoxy(2,22); printf("exit"); sleep(1); exit(1);
   case F9: if(b_start&&b_end) flag=TRUE; break;
   case F2: gotoxy(2,22);
   printf("Savename:");
   scanf("%s",savename);
   gotoxy(2,22);
   if(SaveMaze(savename)) printf("Save OK! ");
   else printf("Save fail! ");
   sleep(1);
   gotoxy(2,2
 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved