跳到正文

休闲·娱乐

Pyramid

seanli1008约 10 分钟
原文

这是 2025年10月4日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本
C++
//Pyramid Created by seanli1008 on 2025/10/3.
//Copyright (c) 2025年 seanli1008. All rights reserved.
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
mt19937 rnd(time(0));
const int dx[]={0,0,-1,1};
const int dy[]={1,-1,0,0};
int x,y,stx,sty,edx,edy,maze[50][50];
bool vis[50][50],chosen[100];
bool is_valid(int x,int y){
	if(x<1 || x>20 || y<1 || y>20)
		return false;
	return true;
}
bool stuck(int x,int y){
	for(int d=0;d<4;d++){
		if(is_valid(x+dx[d],y+dy[d]) && !vis[x+dx[d]][y+dy[d]])
			return false;
	}
	return true;
}
void debug(){
	for(int i=1;i<=20;i++){
		for(int j=1;j<=20;j++){
			if(maze[i][j]==1)
				cout<<". ";
			else if(maze[i][j]==2)
				cout<<"x ";
			else
				cout<<"# ";
		}
		cout<<endl;
	}
}
void generate_map(){
	//=== generate main path ===
	vector<pair<int,int>> path;
	x=y=1;
	stx=sty=1;
	vis[x][y]=true;
	maze[x][y]=1;
	bool flag=true;
	for(int i=1;i<=50;i++){
		vector<int> tmp;
		for(int d=0;d<4;d++){
			if(is_valid(x+dx[d],y+dy[d]) && !vis[x+dx[d]][y+dy[d]] && !stuck(x+dx[d],y+dy[d])){
				if(d==0 || d==2)
					tmp.push_back(d);
				else{
					tmp.push_back(d);
					tmp.push_back(d);
					tmp.push_back(d);
				}
			}
		}
		int k=tmp.size();
		if(k==0){
			flag=false;
			break;
		}
		int pos=rnd()%k;
		x+=dx[tmp[pos]],y+=dy[tmp[pos]];
		vis[x][y]=true;
		maze[x][y]=1;
		path.push_back({x,y});
	}
	while(!flag){
		flag=true;
		memset(maze,0,sizeof(maze));
		memset(vis,false,sizeof(vis));
		path.clear();
		x=y=1;
		vis[x][y]=true;
		maze[x][y]=1;
		for(int i=1;i<=50;i++){
			vector<int> tmp;
			for(int d=0;d<4;d++){
				if(is_valid(x+dx[d],y+dy[d]) && !vis[x+dx[d]][y+dy[d]] && !stuck(x+dx[d],y+dy[d])){
					if(d==0 || d==2)
						tmp.push_back(d);
					else{
						tmp.push_back(d);
						tmp.push_back(d);
						tmp.push_back(d);
					}
				}
			}
			int k=tmp.size();
			if(k==0){
				flag=false;
				break;
			}
			int pos=rnd()%k;
			x+=dx[tmp[pos]],y+=dy[tmp[pos]];
			vis[x][y]=true;
			maze[x][y]=1;
			path.push_back({x,y});
		}
	}
	//=== generate main path ===
	//debug();
	edx=x,edy=y;
	int delta=rnd()%5+1,sign=rnd()%2,tot=20;
	if(sign==0)
		tot-=delta;
	else
		tot+=delta;
	for(int i=1;i<=tot;i++){
		int pos=rnd()%50;
		while(chosen[pos])
			pos=rnd()%50;
		int what_the_heck=rnd()%3;
		if(what_the_heck==0)
			pos=min(pos-5,1);
		chosen[pos]=true;
		int r=path[pos].first,c=path[pos].second;
		int dep=rnd()%5+8;
		int surprise=rnd()%3;
		if(surprise==0){
			dep+=10;
			pos=min(pos-10,1);
		}
		for(int j=1;j<=dep;j++){
			vector<int> tmp;
			for(int d=0;d<4;d++){
				if(is_valid(r+dx[d],c+dy[d]) && !vis[r+dx[d]][c+dy[d]] && maze[r+dx[d]][c+dy[d]]!=1 && maze[r+dx[d]][c+dy[d]]!=2){
					if(d==0 || d==4){
						tmp.push_back(d);
						tmp.push_back(d);
						tmp.push_back(d);
					}
					else
						tmp.push_back(d);
				}
			}
			int k=tmp.size();
			if(k==0)
				break;
			int ababa=rnd()%k;
			r+=dx[tmp[ababa]],c+=dy[tmp[ababa]];
			vis[r][c]=true;
			maze[r][c]=2;
		}
	}
	//debug();
}
void print(){
	for(int i=1;i<=20;i++){
		for(int j=1;j<=20;j++){
			if(abs(x-i)<=2 && abs(y-j)<=2){
				if(i==x && j==y)
					cout<<"o ";
				else if(i==edx && j==edy)
					cout<<"! ";
				else if(maze[i][j]==0)
					cout<<"# ";
				else
					cout<<". ";
			}
			else
				cout<<"? ";
		}
		cout<<endl;
	}
}
int main(){
	cout<<"Pyramid developed by seanli1008"<<endl;
	Sleep(1000);
	system("cls");
	cout<<"You are a treasure hunter. There is a priceless treasure in this pyramid. Please find it. "<<endl;
	Sleep(1000);
	cout<<"However, it's really dark. you can only see things near you. It's garunteed that you can get the treasure. "<<endl;
	Sleep(1000);
	cout<<"Use up/down/left/right to move. '#' stands for wall and '.' stands for land"<<endl;
	cout<<"o is yourself. ! stands for treasure. "<<endl;
	Sleep(1000);
	cout<<"If you understand the rules, enter 'ready' to start the game. "<<endl;
	string _;
	cin>>_;
	if(_!="ready")
		return 0;
	system("cls");
	cout<<"loading map... (it may take a bit of time)"<<endl;
	generate_map();
	system("cls");
	x=1,y=1;
	while(x!=edx || y!=edy){
		print();
		int op;
		while(true){
			if(_kbhit()){
				int ch=_getch();
				if(ch==0xE0 || ch==0){
					ch=_getch();
					if(ch==72){
						op=1;
						break;
					}
					if(ch==80){
						op=2;
						break;
					}
					if(ch==75){
						op=3;
						break;
					}
					if(ch==77){
						op=4;
						break;
					}
				}
			}
		}
		if(op==1){//up
			if(is_valid(x-1,y) && maze[x-1][y]!=3 && maze[x-1][y]!=0)
				x--;
		}
		if(op==2){//down
			if(is_valid(x+1,y) && maze[x+1][y]!=3 && maze[x+1][y]!=0)
				x++;
		}
		if(op==3){//left
			if(is_valid(x,y-1) && maze[x][y-1]!=3 && maze[x][y-1]!=0)
				y--;
		}
		if(op==4){//right
			if(is_valid(x,y+1) && maze[x][y+1]!=3 && maze[x][y+1]!=0)
				y++;
		}
		system("cls");
	}
	cout<<"WoW! You find the treasure! Congratulations!"<<endl;
	return 0;
}