这是一个极简版的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;
}
这段代码比较简单,有待升级。