多人小游戏

发布时间:2024年01月04日
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll Rand(ll min,ll max){
	return (rand()%(max-min+1))+min;
}
struct people{
	ll name;
	ll scores;
}a[100000],o[100000];
bool cmp(people a,people b){
	if(a.scores<b.scores) return 1;
	else if(a.scores==b.scores) return a.name<b.name;
	else return 0;
}
ll n,b[100000];
void find1(){
	ll t=n*5,x;
	cout<<"轮数:\n"; 
	cin>>x;
	cout<<"游戏说明:\n";
	cout<<"    有一堆小球,其中有一个是红色的,其他都是蓝色的。编号是从1到"<<t;
	cout<<"。现在每个人说一个数,看谁离这个红色小球的编号最近。\n游戏开始\n";
	for(int j=1;j<=x;j++){
		cout<<"第"<<j<<"轮\n"; 
		ll y=Rand(1,t);
		for(int i=1;i<=n;i++){
			ll k;
			cout<<"请"<<b[i]<<"号玩家输入:\n";
			cin>>k;
			while(k>t||k<1){
				cout<<"超出范围\n";
				cout<<"请"<<b[i]<<"号玩家输入:\n";
				cin>>k;
			}
			o[i].scores+=abs(k-y);
			o[i].name=b[i];
			a[i].scores=abs(k-y);
			a[i].name=b[i];
		}
		cout<<"红球位置:"<<y<<"\n";
		cout<<"排行榜:\n";
		sort(a+1,a+n+1,cmp);
		int ii=1;
		for(int i=1;i<=n;i++){
			cout<<"第"<<ii<<"名:"<<a[i].name<<"号玩家,距离:"<<a[i].scores<<"\n";
			while(a[i].scores==a[i+1].scores&&i<=n){
				i++;
				cout<<"第"<<ii<<"名:"<<a[i].name<<"号玩家,距离:"<<a[i].scores<<"\n"; 
			}
			ii++;
		}
	}
	cout<<"总排行榜:\n";
	sort(o+1,o+n+1,cmp); 
	int ii=1;
	for(int i=1;i<=n;i++){
		cout<<"第"<<ii<<"名:"<<o[i].name<<"号玩家,距离:"<<o[i].scores<<"\n";
		while(o[i].scores==o[i+1].scores&&i<=n){
			i++;
			cout<<"第"<<ii<<"名:"<<o[i].name<<"号玩家,距离:"<<o[i].scores<<"\n"; 
		}
		ii++;
	}
	cout<<"游戏结束\n";
}
void find2(){
	ll x,xx,yy,min=1e19,min1,u;
	cout<<"最多轮数:\n";
	cin>>x;
	cout<<"范围(格式:x y):\n1.自定\n2.随机\n";
	cin>>u;
	while(!(u>=1&&u<=2)){
		cout<<"无效输入\n";
		cout<<"范围(格式:x y):\n1.自定\n2.随机\n";
		cin>>u;
	}
	if(u==1){
		cin>>xx>>yy;
	}
	else if(u==2){
		xx=Rand(-1000,1000);
		yy=Rand(-1000,1000);
		while(xx<yy){
			xx=Rand(-1000,1000);
			yy=Rand(-1000,1000);
		} 
	}
	ll y=Rand(xx,yy);
	cout<<"游戏说明:\n";
	cout<<"    随机一个"<<xx<<"到"<<yy<<"之间的数,你们要输入一个数,计算机会告诉你是大了还是小了。";
	cout<<"直到猜对了或猜的轮数超过"<<x<<"。\n";
	ll t,f=0;
	for(int i=1;i<=x;i++){
		cout<<"第"<<i<<"轮\n";
		for(int j=1;j<=n;j++){
			ll k;
			cout<<"请"<<b[j]<<"号玩家输入:\n";
			cin>>k;
			while(k>yy||k<xx){
				cout<<"超出范围\n";
				cout<<"请"<<b[j]<<"号玩家输入:\n";
				cin>>k;
			}
			if(k>y) cout<<"大了\n";
			else if(k<y) cout<<"小了\n";
			else if(k==y){
				f=1;
				t=b[j];
				break;
			}
			if(abs(k-y)<min){
				min=abs(k-y);
				min1=b[j];
			}
		}
		if(f){
			cout<<"获胜者:"<<t<<"号玩家\n"; 
			break;
		}
	}
	if(!f){
		cout<<"    这个数是"<<y<<"\n";
		cout<<"    很可惜,你们没能在规定轮数内找出这个数"<<"第一个离这个数最近的人是";
		cout<<min1<<"号玩家,距离是"<<min<<"\n"; 
	}
	cout<<"游戏结束\n";
}
int main(){
	srand(time(0));
	while(1){
		ll x,y,c[100000]={0};
		cout<<"人数:";
		cin>>n;
		cout<<"1.谁更近?\n2.猜数字\n3.随机\n4.退出\n";
		cin>>x;
		while(!(x>=1&&x<=4)){
			cout<<"无效输入\n";
			cout<<"1.谁更近?\n2.猜数字\n3.随机\n4.退出\n";
			cin>>x;
		}
		if(x==3){
			x=Rand(1,2);
		}
		else if(x==4){
			return 0;
		}
		cout<<"顺序:\n1.自定\n2.随机\n3.正常顺序\n4.取消\n";
		cin>>y;
		while(!(y>=1&&y<=4)){
			cout<<"无效输入\n";
			cout<<"顺序:\n1.自定\n2.随机\n3.正常顺序\n4.取消\n";
			cin>>y;
		}
		if(y==1){
			for(int i=1;i<=n;i++){
				cin>>b[i];
				while(b[i]<1||b[i]>n||c[b[i]]!=0){
					cout<<"错误!玩家范围是1到"<<n<<"。(也可能是顺序中已包含这个数)\n";
					cin>>b[i];
				}
				c[b[i]]=1; 
			} 
		}
		else if(y==2){
			ll k;
			cout<<"顺序:";
			for(int i=1;i<=n;i++){
				ll t=Rand(1,n);
				while(c[t]!=0){
					t=Rand(1,n);
				}
				b[i]=t;
				c[t]=1;
				cout<<b[i]<<" ";
			}
			cout<<"\n1.满意该顺序\n2.不满意该顺序\n";
			cin>>k;
			while(!(k>=1&&k<=2)){
				cout<<"无效输入\n";
				cout<<"\n1.满意该顺序\n2.不满意该顺序\n";
				cin>>k;
			}
			while(k==2){
				memset(c,0,sizeof(c));
				cout<<"顺序:";
				for(int i=1;i<=n;i++){
					ll t=Rand(1,n);
					while(c[t]!=0){
						t=Rand(1,n);
					}
					b[i]=t;
					c[t]=1;
					cout<<b[i]<<" ";
				}
				cout<<"\n1.满意该顺序\n2.不满意该顺序\n";
				cin>>k;
				while(!(k>=1&&k<=2)){
					cout<<"无效输入\n";
					cout<<"\n1.满意该顺序\n2.不满意该顺序\n";
					cin>>k;
				}
			}
		}
		else if(y==3){
			for(int i=1;i<=n;i++) b[i]=i;
		}
		else if(y==4){
			continue;
		}
		if(x==1) find1();
		else if(x==2) find2();
	}
	return 0;
}

文章来源:https://blog.csdn.net/coduck_S12004zbj/article/details/135393049
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。