C++猜数字游戏

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值