#include<iostream>
#include<cstdlib>
#include<ctime>
#include<windows.h>
const int STEP=8;
const int N=4;
int a[N],b[N],xxx,yyy,step;
using namespace std;
void main1(){
srand(time(0));
for(int i=0;i<N;i++){
a[i]=rand()%10;
for(int j=0;j<i;j++ ){
if(a[j]==a[i]){
i--;break;
}
}
}
return;
}
int main2(){
int n;
xxx=0,yyy=0;
cin>>n;
for(int i=0;i<N;i++){
b[i]=n%10;
n/=10;
}
for(int i=0;i<N;i++){
if(a[i]==b[i]){
xxx++;
}
else{
for(int j=0;j<N;j++)
if(a[i]==b[j])
yyy++;
}
}
//输出
cout<<"你猜的结果是:";
cout<<xxx<<"A"<<yyy<<"B"<<endl;
return 0;
}
bool win(){
if(xxx==N){
cout<<"恭喜你,猜对了!!!";
return 1;
}
else{
return 0;
}
}
bool lose(){
if(step==8){
cout<<"次数用完,游戏失败!!!\n";
return 1;
}
else{
cout<<"你还有"<<STEP-step<<"次机会!";
return 0;
}
}
int main(){
SetConsoleTitle("Rickey的猜数字游戏");
main1();
while(1){
main2();
step++;
if(win()){
break;
}
if(lose()){
break;
}
}
cout<<"是否继续游戏?\n";
cout<<"输入1继续,其他退出!\n";
char ch;
cin>>ch;
if(ch=='1'){
system("cls");
step=0;
main();
}
else{
cout<<"欢迎下次游戏!";
}
return 0;
}
C++猜数字游戏
最新推荐文章于 2025-04-19 15:13:07 发布