猜数字(建议双人)
一切要输入的地方都按照程序的要求来,否则会引起错误。
#include <bits/stdc++.h>
#include <windows.h>
using namespace std;
int main() {
cout<<"请输入游玩人数,单人将自动与机器游玩"<<"\n";
int p;
cin>>p;
if(p==1){
system("cls");
cout<<"请输入数据范围(1-n),填写正整数n,必须小于2^30"<<"\n";
int n;
cin>>n;
int number;
srand( (unsigned)time( NULL ) );
number=rand();number%=n;
system("cls");
int times=1;
while(1){
cout<<"第"<<times<<"次:请输入猜的数字"<<"\n";
int k;
cin>>k;
if(k>number) cout<<"大了"<<"\n";
else if(k<number) cout<<"小了"<<"\n";
else{cout<<"猜对了!"<<"\n"<<"你用了"<<times<<"次。";return 0;}
times++;
}
}
else{
if(p>2){
system("cls");
cout<<"您的游玩人数大于2人,建议双人游玩,输入1切换双人,否则1人出题轮流猜"<<"\n";
int k;
cin>>k;
if(k==1) p=2;
}
}
if(p==2){
system("cls");
cout<<"请输入两人称呼,空格隔开,名字里不要有空格"<<"\n";
string name,name2;
cin>>name>>name2;
system("cls");
cout<<"现在请 "<<name<<" 玩家输入数字,必须小于2^30"<<"\n";
int n;
cin>>n;
system("cls");
int timesA=1;
while(1){
cout<<"第"<<timesA<<"次,请输入:";
int k;
cin>>k;
cout<<name2<<" : "<<k<<"\n";
if(k>n) cout<<name<<" : 大了"<<"\n";
else if(k<n) cout<<name<<" : 小了"<<"\n";
else{cout<<name<<" : 答对了!"<<"\n"<<"系统 :您用了"<<timesA<<"次";cout<<"接下来轮到 "<<name<<" 猜了";Sleep(3000);break;}
timesA++;
}
system("cls");
cout<<"现在请 "<<name2<<" 玩家输入数字,必须小于2^30"<<"\n";
cin>>n;
system("cls");
int timesB=1;
while(1){
cout<<"第"<<timesB<<"次,请输入:";
int k;
cin>>k;
cout<<name<<" : "<<k<<"\n";
if(k>n) cout<<name2<<" : 大了"<<"\n";
else if(k<n) cout<<name2<<" : 小了"<<"\n";
else{
cout<<name2<<" : 答对了!"<<"\n"<<"系统 :您用了"<<timesB<<"次"<<"\n";
cout<<"系统 : ";
if(timesA<timesB) cout<<name2<<" 用的次数少!";
else if(timesA>timesB) cout<<name<<" 用的次数少!";
else cout<<"平局!";
return 0;
}
timesB++;
}
}
else{
system("cls");
cout<<"现在请选定1人出题,其他人轮流猜,1人1题,你有5秒的决定时间"<<"\n";
Sleep(5000);
cout<<"现在请出题人输入称呼"<<"\n";
string chutiusername;
cin>>chutiusername;
cout<<"现在请按顺序输入猜数者称呼,空格隔开,名字里不要用空格"<<"\n";
string username[p+1];
for(int i=1;i<=p-1;i++) cin>>username[i];
system("cls");
int times[p+1];
for(int i=1;i<=p-1;i++){
cout<<"请 "<<username[i]<<" 作准备"<<"\n";
times[i]=1;
Sleep(1500);
cout<<"现在请出题人 "<<chutiusername<<" 为玩家 "<<username[i]<<" 出题"<<"\n";
int n;
cin>>n;
system("cls");
while(1){
cout<<"第"<<times[i]<<"次,请输入:";
int k;
cin>>k;
cout<<username[i]<<" : "<<k<<"\n";
if(k>n) cout<<chutiusername<<" : 大了"<<"\n";
else if(k<n) cout<<chutiusername<<" : 小了"<<"\n";
if(k==n){
cout<<chutiusername<<" : 答对了!"<<"\n";
cout<<"系统 :您用了"<<times[i]<<"次"<<"\n";
Sleep(3000);
system("cls");
break;
}
times[i]++;
}
}
int s=1e9;
string u;
for(int i=1;i<=p-1;i++){
if(times[i]<s){s=times[i];u=username[i];}
}
cout<<"系统 :"<<u<<" 用的次数最少,获得胜利!";
return 0;
}
return 0;
}