C++游戏之颜色解密(规则见代码内)

#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
int main(){
	cout<<"The rules of the game: eight colors (red, orange, yellow, green, blue, purple, black, white) take out four random permutations, at the beginning, there will be a random state, R for the position and color are correct, H for the color is correct and the position is wrong, W is the position and color are wrong, you have to make the random color arrangement correct, if all right, the challenge is successful!Good luck!\n";
	string s[]={"red","orange","yellow","green","blue","purple","black","white"},s1[5]={},s2[5]={},t1;
	int n,cnt,t;
	srand(static_cast<unsigned int>(time(0)));
	bool flag[8]={};
	cnt=1;
	t=sizeof(s)/sizeof(s[0]);
	while(cnt<5){
		n=rand()%t;
		if(!flag[n]){
			s1[cnt]=s[n];
			flag[n]=true;
			cnt++;
		}
	}
	memset(flag,0,sizeof(flag));
	cnt=1;
	while(cnt<5){
		n=rand()%t;
		if(!flag[n]){
			s2[cnt]=s[n];
			flag[n]=true;
			cnt++;
		}
	}
	printf("%s %s %s %s\n",s2[1].c_str(),s2[2].c_str(),s2[3].c_str(),s2[4].c_str());
	for(int i=1;i<5;i++){
		t1="";
		bool f=false;
		for(int j=1;j<5;j++){
			if(s1[j]==s2[i]){
				if(i==j){
					cout<<"R";
					t1+='R';
				} 
				else{
					cout<<"H";
					t1+='H';
				}
				f=true;
				break;
			}
		}
		if(!f){
			cout<<"W";
			t1+='W';
		}
	}
	if(t1=="RRRR"){
		cout<<"\nYou win!";
		return 0;
	}
	while(true){
		cin>>s2[1]>>s2[2]>>s2[3]>>s2[4];
		t1="";
		for(int i=1;i<5;i++){
			bool f=false;
			for(int j=1;j<5;j++){
				if(s1[i]==s2[j]){
					if(i==j){
						cout<<"R";
						t1+='R';
					} 
					else{
						cout<<"H";
						t1+='H';
					}
					f=true;
					break;
				}
			}
			if(!f){
				cout<<"W";
				t1+='W';
			}
		}
		if(t1=="RRRR"){
			cout<<"\nYou win!";
			return 0;
		}		
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值