c++21点小游戏(极简)

这是一个极简版的c++21点代码,非常容易看懂,结构简单。(dev-c++运行通过)

#include<iostream>
#include<stdlib.h>
#include<windows.h>
#include<cstring>
#include<time.h>
using namespace std;
int main()
{
    cout<<"        21点"<<endl;
    cout<<"----------------------"<<endl;
    cout<<"输入1开始"<<endl;
    int n;
    cin>>n;
    if (n==1)
    {
        system("cls");
        cout<<"开始"<<endl;
        //输入昵称 
        string name1,name2;
        cout<<"请输入玩家1昵称"<<endl;
        cin>>name1;
        cout<<"请输入玩家2昵称"<<endl;
        cin>>name2;
        /**/
        string op1,op2;
        int n1=0,n2=0;
        int s1=0,s2=0;
        //玩家1 
        cout<<"请玩家1拿牌"<<endl;
        while (true)
        {
            cout<<"是否继续拿?(y/n)"<<endl;
            cin>>op1;
            if (op1=="y")
            {
                srand(time(NULL));
                s1 += (rand()%(7-1+1)+1);
                n1++;
            }
            else if (op1=="n")
            {
                cout<<"你一共拿了"<<n1<<"张牌";
                break;
            }
            else
            {
                cout<<"请不要乱输!"; 
            }
        }
        Sleep(3000);
        system("cls");
        //玩家2 
        cout<<"请玩家2拿牌"<<endl;
        while (true)
        {
            cout<<"是否继续拿?(y/n)"<<endl;
            cin>>op2;
            if (op2=="y")
            {
                srand(time(NULL));
                s2 += (rand()%(7-1+1)+1);
                n2++;
            }
            else if (op1=="n")
            {
                cout<<"你一共拿了"<<n2<<"张牌";
                break;
            }
            else
            {
                cout<<"请不要乱输!"; 
            }
        }
        Sleep(3000);
        system("cls");
        cout<<"正在统计....."<<endl;
        Sleep(1000);
        cout<<"玩家1总分数:"<<s1<<endl;
        cout<<"玩家2总分数:"<<s2<<endl;
        if (s1<=21 && s2<=21)
        {
            if (s1>s2)    cout<<"【"<<name1<<"】"<<"is the winner!!!";
            if (s1==s2)    cout<<"【"<<name1<<"】"<<"and"<<"【"<<name2<<"】"<<"come out even!!!";
            if (s1<s2)    cout<<"【"<<name2<<"】"<<"is the winner!!!";
        }
        if(s1>21 && s2>21)
        {
            cout<<"两方都超过21分,"<<"【"<<name1<<"】"<<"and"<<"【"<<name2<<"】"<<"come out even!!!";
        }
        if (s1>21 && s2<=21)    cout<<"【"<<name2<<"】"<<"is the winner!!!";
        if (s2>21 && s1<=21)    cout<<"【"<<name1<<"】"<<"is the winner!!!";
    }
    return 0;
}

这段代码比较简单,有待升级。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值