跳到正文

个人记录

迷宫游戏

Charles_with_wkc约 7 分钟
原文
文章信息与版本

文章信息

发布
2024年6月29日
收录
2025年12月12日
更新
2026年10月11日
篇幅
约 7 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
#include<windows.h>
#include<ctime>
using namespace std;
const int N=1005;
int a[N][N];
void xitong();
void c();
void s(int n);
void zhangai();
void kaishi();
void mapp();
void tuichu();
void chongqi(); 
void caozuo();
void shang();
void xia();
void zuo();
void you();
void shengli();
void sr();
int n,m,k,x,y,dx=1,dy=1;
char ch;
bool f=0;
int main(){
	sr();
	memset(a,0,sizeof(0));
	xitong();
	while(true){
		f=0;
		kaishi();
		caozuo();
	}
	return 0;
} 
void sr(){
	srand((unsigned)time(NULL));
	return ;
}
void c(){
	system("cls");
	return ;
}
void s(int n){
	Sleep(n);
	return ;
}
void shengli(){
	s(100);
	c();
	cout<<"恭";
	s(100);
	cout<<"喜";
	s(100);
	cout<<"胜";
	s(100);
	cout<<"利!";
	f=1;
	return ;
}
void shang(){
	if(dy==1){
		return ;
	}
	else{
		if(a[dx-1][dy]==1){
			return ;
		}
		else{
			a[dx][dy]=0;
			dx--;
			if(a[dx][dy]==3){
				shengli();
			}
			else{
				a[dx][dy]=2;
			}
		}
	}
	return ;
}
void xia(){
	if(dy==m){
		return ;
	}
	else{
		if(a[dx+1][dy]==1){
			return ;
		}
		else{
			a[dx][dy]=0;
			dx++;
			if(a[dx][dy]==3){
				shengli();
			}
			else{
				a[dx][dy]=2;
			}
		}
	}
	return ;
}
void zuo(){
	if(dx==1){
		return ;
	}
	else{
		if(a[dx][dy-1]==1){
			return ;
		}
		else{
			a[dx][dy]=0;
			dy--;
			if(a[dx][dy]==3){
				shengli();
			}
			else{
				a[dx][dy]=2;
			}
		}
	}
	return ;
}
void you(){
	if(dx==n){
		return ;
	}
	else{
		if(a[dx][dy+1]==1){
			return ;
		}
		else{
			a[dx][dy]=0;
			dy++;
			if(a[dx][dy]==3){
				shengli();
			}
			else{
				a[dx][dy]=2;
			}
		}
	}
	return ;
}
void caozuo(){
	while(true){
		if(f==1){
			s(300);
			c();
			break;
		}
		s(300);
		c();
		mapp();
		cout<<"向上请按1,";
		s(100);
		cout<<"向下请按2,";
		s(100);
		cout<<"向左请按3,";
		s(100);
		cout<<"向右请按4"<<endl;
		cin>>ch;
		if(ch=='1'){
			shang();
		}
		else if(ch=='2'){
			xia();
		}
		else if(ch=='3'){
			zuo();
		}
		else if(ch=='4'){
			you();
		}
	}
	return ;
}
void chongqi(){
	int t=5;
	while(t--){
		c();
		cout<<"重";
		s(500);
		cout<<"启";
		s(500);
		cout<<"中";
		s(500);
		cout<<"…";
		s(500);
		cout<<"…";
		s(500);
	}
	return ;
}
void tuichu(){
	c();
	cout<<"欢";
	s(100);
	cout<<"迎";
	s(100);
	cout<<"下";
	s(100);
	cout<<"次";
	s(100);
	cout<<"使";
	s(100);
	cout<<"用!";
	return ;
}
void zhangai(){
	while(k--){
		sr();
		x=rand()%n;
		y=rand()%m;
		if(a[x][y]==0&&a[x][y]!=2&&a[x][y]!=3&&x>0&&y>0){
			a[x][y]=1;
		}
		else{
			k++;
		}
	}
	return ;
}
void mapp(){
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(a[i][j]==1){
				cout<<"□";
			}
			else if(a[i][j]==2){
				cout<<"☆";
			}
			else if(a[i][j]==0){
				cout<<"·";
			}
			else if(a[i][j]==3){
				cout<<"×";
			}
		}
		cout<<endl;
	}
	return ;
}
void xitong(){
	cout<<"欢";
	s(100);
	cout<<"迎";
	s(100);
	cout<<"来";
	s(100);
	cout<<"到";
	s(100);
	cout<<"迷";
	s(100);
	cout<<"宫";
	s(100);
	cout<<"游";
	s(100);
	cout<<"戏";
	s(100);
	cout<<"!";
	s(1000);
	c();
	return ;
}
void kaishi(){
	cout<<"开始请按y,";
	s(100);
	cout<<"退出请按n,";
	s(100);
	cout<<"重启请按c"<<endl;
	cin>>ch;
	if(ch=='n'){
		tuichu();
	} 
	else if(ch=='c'){
		chongqi();
	}
	c();
	cout<<"输";
	s(100);
	cout<<"入";
	s(100);
	cout<<"迷";
	s(100);
	cout<<"宫";
	s(100);
	cout<<"行";
	s(100);
	cout<<"数:";
	cin>>n;
	s(100);
	c();
	cout<<"输";
	s(100);
	cout<<"入";
	s(100);
	cout<<"迷";
	s(100);
	cout<<"宫";
	s(100);
	cout<<"列";
	s(100);
	cout<<"数:";
	cin>>m;
	s(100);
	c();
	cout<<"输";
	s(100);
	cout<<"入";
	s(100);
	cout<<"迷";
	s(100);
	cout<<"宫";
	s(100);
	cout<<"障";
	s(100);
	cout<<"碍";
	s(100);
	cout<<"数:";
	cin>>k;
	s(100);
	c();
	sr();
	x=rand()%n;
	y=rand()%m;
	a[x][y]=3;
	a[1][1]=2;
	zhangai();
	return ;
}
原文