跳到正文

个人记录

C++Game

_DEQUE_约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年12月6日
收录
2025年12月12日
更新
2026年9月4日
篇幅
约 3 分钟

版本

  1. v1当前

随机变换迷宫

C++
#include <bits/stdc++.h>
#include <conio.h>
using namespace std;

const int N=1000;

int x=1,y=1,n,m;

char a[N][N];

int main(){
	int s;
	cout<<"请输入种子:";
	cin>>s;
	srand(s);
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	int n=30,m=30;
	memset(a,'.',sizeof(a));
	for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
        	if(i==y&&j==x)cout<<"@";
        	else cout<<a[i][j];
		}
		cout<<endl;
	}
	while(1){
		bool ll=0;
		int t=_getch();
		if(t==224||t==0){
			int t=_getch();
			switch(t){
                case 72:
                    y--;
                    if(a[y][x]=='#'||y<=0)y++,ll=1;
//					cout<<x<<" "<<y<<endl;
                    break;
                case 80:
                    y++;
                    if(a[y][x]=='#'||y>n)y--,ll=1;
//					cout<<x<<" "<<y<<endl;
                    break;
                case 75:
                    x--;
                    if(a[y][x]=='#'||x<=0)x++,ll=1;
//					cout<<x<<" "<<y<<endl;
                    break;
                case 77:
                    x++;
                    if(a[y][x]=='#'||x>m)x--,ll=1;
//					cout<<x<<" "<<y<<endl;
                    break;
            }
            if(ll)continue;
            system("cls");
            for(int i=1;i<=n;i++){
            	for(int j=1;j<=m;j++){
            		a[i][j]=(rand()%3==0?'#':'.');
            		if(i==y&&j==x)cout<<"@";
            		else cout<<a[i][j];
				}
				cout<<endl;
			}
		}
	}




	return 0;
}
原文