猜数字小游戏

#include<iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{

    int flag,n,a,min,max;
    while(1)
    {
        system("color f4");
        system("cls");
        min=-1;
        max=101;
        srand(time(NULL));
        int ans=rand()%100+1;
        cout<<"猜数字游戏"<<endl;
        cout<<"是否开始玩此游戏,请输入选择序号"<<endl;
        cout<<"1 确认  2 取消"<<endl;
        cin>>flag;
        if(flag==1)
        {
            cout<<"请输入你想玩的游戏难度序号"<<endl;
            cout<<"1 无数条命"<<endl;
            cout<<"2 6条命"<<endl;
            cout<<"3 3条命"<<endl;
            cin>>flag;
            switch(flag)
            {
                case 1:
                    n=10;
                    break;
                case 2:
                    n=6;
                    break;
                case 3:
                    n=3;
                    break;
            }
            cout<<"开始游戏,游戏规则如下"<<endl;
            cout<<"请输入1-100的数字"<<endl;
            for(int i=n; i>=1; i--)
            {
                if(n==10)
                {
                    i++;
                }
                else
                {
                    cout<<"你还有"<<i<<"条命"<<endl;
                }
                cin>>a;
                if(a>ans)
                {
                    if(a<max) max=a;
                    cout<<"猜大了"<<endl;
                }
                else if(a<ans)
                {
                    if(a>min) min=a;
                    cout<<"猜小了"<<endl;
                }
                else
                {
                    cout<<"猜对了"<<endl;
                    break;
                }
                if(i==1)
                {
                    cout<<"机会用完"<<endl;
                }
                cout<<"请重新猜,范围是"<<min<<"到"<<max<<"(不包括"<<min<<"和"<<max<<")"<<endl;
            }

        }
        else if(flag==2)
        {
            break;
        }
        else
        {
            cout<<"    错误;"<<endl;
        }
        system("pause");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值