数据结构:用栈写的迷宫小游戏

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M 10
#define N 10
//typedef int ElemType;
typedef struct PosType{
	int x;//行号
	int y;//列号 
}PosType;
typedef struct ElemType{
	PosType seat;//通道的坐标
	int dirction;//下一个通道在当前通道的位置 
}ElenType;
typedef struct SqStack{
	ElemType data;
	struct SqStack *next;
}SqStack;
typedef SqStack *LinkStack;
//初始化
int InsiStack(LinkStack &s)
{
	s = NULL;
	return 1;
 }
 //入栈
 int Push(LinkStack &s,ElemType e)
 {
 	LinkStack p;
 	p = (LinkStack)malloc(sizeof(SqStack));
 	if(!p)
 	{
 		return 0;
	 }
	 p -> data = e;
	 p -> next = s;
	 s = p;
	 return 1;
  } 
  //出栈
  int Pop(LinkStack &s,ElemType &e)
  {
  	LinkStack q;
  	if(s == NULL)
  	{
  		return 0;
	 }
	 e = s -> data;
	 q = s;
	 s = s -> next;
	 free(q);
	 return 1;
	 
   } 
   //判断是否为空栈
   int StackEmpty(LinkStack s)
   {
   	if(s == NULL)
   	{
   		return 1;
	   }
	   else
	   {
	   	return 0;
	   }
	} 
	//迷宫平面图 
	static char maze[M][N] = {
	{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
	{'#', ' ', ' ', '#', ' ', ' ', ' ', '#', ' ', '#'},
	{'#', ' ', ' ', '#', ' ', ' ', ' ', '#', ' ', '#'},
	{'#', ' ', ' ', ' ', ' ', '#', '#', ' ', ' ', '#'},
	{'#', ' ', '#', '#', '#', ' ', ' ', ' ', ' ', '#'},
	{'#', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', '#'},
	{'#', ' ', '#', ' ', ' ', ' ', '#', ' ', ' ', '#'},
	{'#', ' ', '#', '#', '#', ' ', '#', '#', ' ', '#'},
	{'#', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
	{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}
};
PosType start = {1,1};//入口 
PosType end = {8,8};//出口 
int pass(PosType curpos)
{
	if(maze[curpos.x][curpos.y] == ' ')
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
int footPrint(PosType curpos)
{
	maze[curpos.x][curpos.y] = '1';//标记 
}
PosType nextPrint(PosType curpos,int dirction)
{
	switch(dirction)
	{
		case 1:
			curpos.y ++;//向东,列加1 
			break;
		case 2:
			curpos.x ++;//向南 
			break;
		case 3:
			curpos.y --;
			break;
		case 4:
			curpos.x --;
			break;
	}
	return curpos;
}
int MarkPrint(PosType curpos)
{
	maze[curpos.x][curpos.y] = '0';//走过但不能到达出口 
}
int mazePath(PosType start,PosType end)
{
	LinkStack s;
	InsiStack(s);
	PosType curpos = start;//设定入口 
	do{
	   if(pass(curpos))//当前可以通过 
	   {
	   	footPrint(curpos);//留下足迹 
	   	ElemType e;
	   	e.seat = curpos;
	   	e.dirction = 1;
	   	Push(s,e);//入栈 
	   	if(curpos.x == end.x && curpos.y == end.y)
	   	{
	   		return 1;
		 }
		 curpos = nextPrint(curpos,1);
	   }
	   else
	   {
	   	ElemType e;
	   	Pop(s,e);
	   	while(e.dirction == 4 && !StackEmpty(s))
	   	{
	   		MarkPrint(e.seat);//走过但不能到达出口 
	   		Pop(s,e);
		  }
		  if(e.dirction < 4)
		  {
		  	e.dirction ++;
		  	Push(s,e);
		  	curpos = nextPrint(e.seat,e.dirction);
		  }
	   }
		
		
	}while(!StackEmpty(s));
	return 0;
}
 void mazePrint()//打印迷宫
 {
 	int i,j;
 	system("color 74");
 	for(i = 0;i < M;i ++)
 	{
 		for(j = 0;j < N;j ++)
 		{
 		
 			printf("%c ",maze[i][j]);
 			//system("color 74");
		 }
		 printf("\n");
	 }
	 printf("\n");
  } 
  void YouPrint(PosType start,PosType end)//有出口和入口 
  {
  	int i,j;
 	for(i = 0;i < M;i ++)
 	{
 		for(j = 0;j < N;j ++)
 		{
 			if(start.x == i && j == start.y)
 		    	{
 				printf("A ");
			 }
			 else if(end.x == i && end.y == j) 
			 {
			 	printf("B ");
			 }
 		    else
 			printf("%c ",maze[i][j]);
 			
 			//system("color 74");
		 }
		 printf("\n");
	 }
	 printf("\n");
  }
  void LuPrint()//打印迷宫里的路
  {
  	int i,j;
  	for(i = 0;i < M;i ++)
  	{
  		for(j = 0;j < N;j ++)
  		{
  			if(i == 0 || j == 0 || j == N - 1 || i == M - 1 || maze[i][j] == '1')
  			{
  				printf("%c ",maze[i][j]);
			  }
			  else
			  {
			  	printf("  ");
			  }
		  }
		  printf("\n");
	  }
	  printf("\n");
   }
   int YanZhen(char *name,char *pass,char *name1,char *pass1)//密码验证 
   {
   	int count = 0;
   	if(strcmp(name,name1) == 0 && strcmp(pass,pass1) == 0)
   	{
   		count = 1;
	   }
	   return count;
   }
int main()
{

	int choice;
	char name[50] = "周峰";//初始的用户名 
	char pass[50] = "12345";//初始密码 
	char name1[50];
	char pass1[50];

	printf("****************************************\n");
	printf("****************<<<地球人勇闯夺命岛>>>");
	printf("****************************************\n");
	printf("友情提示:输入入口和出口,看能否走出夺命岛\n");
	printf("1,用户注册过\n");
	printf("2,没有注册过\n");
	printf("3,退出系统\n");
	printf("请输入你的选择:");
	scanf("%d",&choice); 
	switch(choice)
	{
		case 1:
			printf("请输入你的用户名:");
			scanf("%s",name1);
			printf("请输入你的密码:");
			scanf("%s",pass1);
			if(YanZhen(name,pass,name1,pass1) == 1)
			{
				printf("登陆成功\n");
				printf("迷宫生成\n");
            	mazePrint();
             	PosType start;
             	PosType end;
	
            	printf("请输入迷宫的入口坐标\n");
            	scanf("%d %d",&start.x,&start.y);
	            printf("请输入迷宫的出口坐标\n");
	            scanf("%d %d",&end.x,&end.y); 
	            printf("输入出口和入口的状态\n");
	            YouPrint(start,end);
	           if(mazePath(start,end))
	            {
		            printf("存在通路\n");
		            printf("迷宫的现在的状态:\n");
		            mazePrint();
		            printf("迷宫中的路径\n");
		            LuPrint();
               	}
	          else
            	{
		            printf("不存在通路");
	            }
			}
			break;
		case 2:
			printf("注册:\n");
		    printf("请输入你的用户名:");
			scanf("%s",name);
			printf("请输入你的密码:");
			scanf("%s",pass);
			printf("注册成功\n");
			system("cls");//清屏 
		    printf("****************************************\n");
	        printf("****************<<<地球人勇闯夺命岛>>>**************\n");
	        printf("****************************************\n");
          	printf("友情提示:输入入口和出口,看能否走出夺命岛\n");
			printf("登陆\n");
			printf("请输入你的用户名:");
			scanf("%s",name1);
			printf("请输入你的密码:");
			scanf("%s",pass1);
			if(YanZhen(name,pass,name1,pass1) == 1)
			{
				printf("登陆成功\n");
				printf("迷宫生成\n");
            	mazePrint();
             	PosType start;
             	PosType end;
	
            	printf("请输入迷宫的入口坐标\n");
            	scanf("%d %d",&start.x,&start.y);
	            printf("请输入迷宫的出口坐标\n");
	            scanf("%d %d",&end.x,&end.y);
				 printf("输入出口和入口的状态\n");
	            YouPrint(start,end); 
	           if(mazePath(start,end))
	            {
		            printf("存在通路\n");
		            printf("迷宫的现在的状态:\n");
		            mazePrint();
		            printf("迷宫中的路径\n");
		            LuPrint();
               	}
	          else
            	{
		            printf("不存在通路");
	            }
			}
			break;
		case 3:
			printf("欢迎下次使用\n");
			exit(0);
			break;
	}
  
	
}

运行截图
在这里插入图片描述
在这里插入图片描述

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值