码迷,mamicode.com
首页 > 其他好文 > 详细

迷宫游戏 用栈实现

时间:2014-05-13 11:40:53      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:迷宫游戏      优先级   

#include<iostream>
#include<string>
#include<stack>
using namespace std;
#define n 8

stack <int *> s;

int * createMaze(){//初始化迷宫
	int i,j;
	int * a;
	a=new int[n*n];
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			*(a+n*i+j)=-1;//不设置为0的原因是超过矩阵范围的位置
		}				//系统默认的是0,会引起麻烦
	}
	*(a+n*0+1)=3;s.push(a+n*0+1);//当前位置入栈
	*(a+n*1+1)=1;*(a+n*1+2)=1;*(a+n*1+3)=1;*(a+n*1+5)=1;
	*(a+n*2+3)=1;*(a+n*2+4)=1;*(a+n*2+5)=1;*(a+n*2+6)=1;
	*(a+n*3+1)=1;*(a+n*3+2)=1;*(a+n*3+3)=1;*(a+n*3+5)=1;
	*(a+n*4+1)=1;*(a+n*4+4)=1;
	*(a+n*5+1)=1;*(a+n*5+2)=1;*(a+n*5+4)=1;*(a+n*5+5)=1;*(a+n*5+6)=1;
	*(a+n*6+2)=1;*(a+n*6+3)=1;*(a+n*6+4)=1;*(a+n*6+6)=1;
	*(a+n*7+6)=1;
	return a;
}
//程序中标记-1,1,2,3,4的值表示意义如下:
//-1:障碍物(方块)
//1:可行走的通道且还未曾被五角星通过
//2:五角星走过的通道(这样说不完全准确),更准确的说是已经压栈的元素(地址)
//3:标识五角星
//4:遇到再也走不通的位置,离开时所填充的障碍(与-1的作用相同,但是不显示出来)

void printMaze(int * a){//打印迷宫
	int i,j;
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			if(*(a+n*i+j)==1||*(a+n*i+j)==2||*(a+n*i+j)==4){
				cout<<"  ";
			}
			else if(*(a+n*i+j)==3){
				cout<<"★";
			}
			else{//*(a+n*i+j)==-1
				cout<<"■";
			}
		}
		cout<<endl;
	}
}

void run(int * a,char ch){
	int i,j;
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			if(*(a+n*i+j)==3){
				switch(ch){
				case ‘w‘:
					if(*(a+n*(i-1)+j)==1){
						*(s.top())=2;
						s.push(a+n*(i-1)+j);//入栈
						*(s.top())=3;
					}
					if(*(a+n*(i-1)+j)==2){
						*(s.top())=-1;
						s.pop();//出栈
						*(s.top())=3;
					}
					return;
				case ‘a‘:
					if(*(a+n*i+j-1)==1){
						*(s.top())=2;
						s.push(a+n*i+j-1);//入栈
						*(s.top())=3;
					}
					if(*(a+n*i+j-1)==2){
						*(s.top())=-1;
						s.pop();//出栈
						*(s.top())=3;
					}
					return;
				case ‘s‘:
					if(*(a+n*(i+1)+j)==1){
						*(s.top())=2;
						s.push(a+n*(i+1)+j);//入栈
						*(s.top())=3;
					}
					if(*(a+n*(i+1)+j)==2){
						*(s.top())=-1;
						s.pop();//出栈
						*(s.top())=3;
					}
					return;
				case ‘d‘:
					if(*(a+n*i+j+1)==1){
						*(s.top())=2;
						s.push(a+n*i+j+1);//入栈
						*(s.top())=3;
					}
					if(*(a+n*i+j+1)==2){
						*(s.top())=-1;
						s.pop();//出栈
						*(s.top())=3;
					}
					return;
				}
			}
		}
	}
}

char getDirection(int * a){//得到方向
	int i,j;
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			if(*(a+n*i+j)==3){
				//必须按优先级排列
				if(*(a+n*i+j+1)==1){
					return ‘d‘;
				}
				if(*(a+n*(i+1)+j)==1){
					return ‘s‘;
				}
				if(*(a+n*i+j-1)==1){
					return ‘a‘;
				}
				if(*(a+n*(i-1)+j)==1){
					return ‘w‘;
				}

				if(*(a+n*i+j+1)==2){
					return ‘d‘;
				}
				if(*(a+n*(i+1)+j)==2){
					return ‘s‘;
				}
				if(*(a+n*i+j-1)==2){
					return ‘a‘;
				}
				if(*(a+n*(i-1)+j)==2){
					return ‘w‘;
				}

				else{
					cout<<"无效按键"<<endl;
				}
			}
		}
	}
}

int handle(){
	int * a;
	int count=1;
	string step;//用string是为了避免用户多输入字符而引起错误
	a=createMaze();
	printMaze(a);
	cout<<"请按任意键进行下一步!"<<endl;
	while(*(a+n*7+6)!=3){
		cout<<"第"<<count<<"步:";
		cin>>step;
		run(a,getDirection(a));
		printMaze(a);
		count++;
	}
	cout<<"恭喜你,顺利到达终点!"<<endl;
	return 0;
}

int main(){
	handle();
	return 0;
}



迷宫游戏 用栈实现,布布扣,bubuko.com

迷宫游戏 用栈实现

标签:迷宫游戏      优先级   

原文地址:http://blog.csdn.net/u011700203/article/details/25663443

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!