#include<iostream>
#include<math.h>
#include<time.h>
using namespace std;
int x=1;
int rand(int n ); //声明
bool win(); //定义变量win为逻辑型变量
void money(); //运行赌注
int main()
{
cout<<"\t※※※※※※※※※※欢迎进入21点※※※※※※※※※※\n\n";
cout<<"\t\t 游戏规则:\n";
cout<<endl;
cout<<"\t\t 1.玩家最多可下赌注1~100;\n";
cout<<endl;
cout<<"\t\t 2.如果牌点数的总数超过21点则暴点,自动判数;\n";
cout<<endl;
cout<<"\t\t 3.计算机方在大于等于16点时不再要牌。\n";
cout<<endl;
cout<<"\t※※※※※※※※※※祝您好运※※※※※※※※※※※※※\n";
money();
return 0; //死循环
}
int rand(int n ) //调用函数
{
return rand()%n; //产生随机数,对生成的随机数取余
}
void money()
{
int a=400,b=400,c;//定义玩家和庄家的赌注各400
for(;;) //控制游戏循环
{
cout<<"请输入您要押的钱数(现在您还有"<<a<<")";
cin>>c; //玩家所压赌注
if(c>100||c<1){cout<<"请最多输入1-100请重新输入"<<endl;
cin>>c;
}else if(win())
{
a+=c;b-=c;//控制赌注
cout<<"您现在的钱数为"<<a;
cout<<"\n对手还有"<<b;
if(a==800||b<=0)
{
cout<<"对手已经没钱了。您赢了游戏结束了"<<endl;
exit(0);
}
else
cout<<"\n请继续下注"<<endl;
}
else
{
a-=c;b+=c;//控制赌注
cout<<"您现在的钱数为"<<a;
cout<<"\n对手还有"<<b;
if(a<=0||b>=800)
{
cout<<"您已经没钱在玩了。游戏结束了"<<endl;
exit(0);
}
else
cout<<"\n请继续下注"<<endl;
}
}
}
bool win()
{bool zhen=true;
bool jia=false;
int n,r,b,z;
char d;
int num_i; //庄家的牌
int num_r; //玩家的牌
srand(time(NULL)); //调用随机函数NULL空函数
r=rand(13)+1; //取一个数字
int i;
i=rand(13)+1;
cout<<"您的牌是"<<r<<endl;
a:cout<<"对手的牌是"<<i<<endl; //a是标记
cout<<"您是否再要一张牌?"<<endl;
cout<<"要请输入1,开牌请输入2"<<endl;
int c;//控制玩家继续或开牌
cin>>c;
while(x){
if(c==1)
{
n=rand(13)+1;
cout<<"\n您又要到一张"<<n;
r+=n;
num_r+=1;
cout<<"\n\n您现在总点数为"<<r<<'\n';
if(r>21){cout<<"牌点数过了21\n";goto b;}//跳到标记b
goto a; //跳到标记a
break;
}
else if(c==2)
{x=0;
cout<<"\n轮到电脑叫牌"<<endl;
cout<<"\n现在电脑的牌是"<<i;
while(i<=18&&i<r)
{
cout<<"现在电脑在要一张牌"<<endl;
b=rand(13)+1;
cout<<"\n电脑又要到一张"<<b;
i+=b;
num_i+=1;
cout<<"\n电脑的点数是"<<i;
}
cout<<"\n好了现在开牌"; }
else
{cout<<"输入错误请重新输入:";
cin>>c;}
}
if((i>r&&i<21)||(i==r&&(num_i>num_r)))//比较对手大小
{b:cout<<"这回合电脑赢了\n\n"<<endl;cout<<"是否继续游戏(y/n)\n\n"<<endl;
cin>>d;
if (d=='n')
{cout<<"游戏结束"<<endl;exit(0);}
else
return jia;}
else
{cout<<"这回合您赢了\n\n"<<endl;cout<<"是否继续游戏(y/n)\n\n"<<endl;
if (d=='n')
{cout<<"游戏结束"<<endl;exit(0);}
else
return zhen;}
}
#include<math.h>
#include<time.h>
using namespace std;
int x=1;
int rand(int n ); //声明
bool win(); //定义变量win为逻辑型变量
void money(); //运行赌注
int main()
{
cout<<"\t※※※※※※※※※※欢迎进入21点※※※※※※※※※※\n\n";
cout<<"\t\t 游戏规则:\n";
cout<<endl;
cout<<"\t\t 1.玩家最多可下赌注1~100;\n";
cout<<endl;
cout<<"\t\t 2.如果牌点数的总数超过21点则暴点,自动判数;\n";
cout<<endl;
cout<<"\t\t 3.计算机方在大于等于16点时不再要牌。\n";
cout<<endl;
cout<<"\t※※※※※※※※※※祝您好运※※※※※※※※※※※※※\n";
money();
return 0; //死循环
}
int rand(int n ) //调用函数
{
return rand()%n; //产生随机数,对生成的随机数取余
}
void money()
{
int a=400,b=400,c;//定义玩家和庄家的赌注各400
for(;;) //控制游戏循环
{
cout<<"请输入您要押的钱数(现在您还有"<<a<<")";
cin>>c; //玩家所压赌注
if(c>100||c<1){cout<<"请最多输入1-100请重新输入"<<endl;
cin>>c;
}else if(win())
{
a+=c;b-=c;//控制赌注
cout<<"您现在的钱数为"<<a;
cout<<"\n对手还有"<<b;
if(a==800||b<=0)
{
cout<<"对手已经没钱了。您赢了游戏结束了"<<endl;
exit(0);
}
else
cout<<"\n请继续下注"<<endl;
}
else
{
a-=c;b+=c;//控制赌注
cout<<"您现在的钱数为"<<a;
cout<<"\n对手还有"<<b;
if(a<=0||b>=800)
{
cout<<"您已经没钱在玩了。游戏结束了"<<endl;
exit(0);
}
else
cout<<"\n请继续下注"<<endl;
}
}
}
bool win()
{bool zhen=true;
bool jia=false;
int n,r,b,z;
char d;
int num_i; //庄家的牌
int num_r; //玩家的牌
srand(time(NULL)); //调用随机函数NULL空函数
r=rand(13)+1; //取一个数字
int i;
i=rand(13)+1;
cout<<"您的牌是"<<r<<endl;
a:cout<<"对手的牌是"<<i<<endl; //a是标记
cout<<"您是否再要一张牌?"<<endl;
cout<<"要请输入1,开牌请输入2"<<endl;
int c;//控制玩家继续或开牌
cin>>c;
while(x){
if(c==1)
{
n=rand(13)+1;
cout<<"\n您又要到一张"<<n;
r+=n;
num_r+=1;
cout<<"\n\n您现在总点数为"<<r<<'\n';
if(r>21){cout<<"牌点数过了21\n";goto b;}//跳到标记b
goto a; //跳到标记a
break;
}
else if(c==2)
{x=0;
cout<<"\n轮到电脑叫牌"<<endl;
cout<<"\n现在电脑的牌是"<<i;
while(i<=18&&i<r)
{
cout<<"现在电脑在要一张牌"<<endl;
b=rand(13)+1;
cout<<"\n电脑又要到一张"<<b;
i+=b;
num_i+=1;
cout<<"\n电脑的点数是"<<i;
}
cout<<"\n好了现在开牌"; }
else
{cout<<"输入错误请重新输入:";
cin>>c;}
}
if((i>r&&i<21)||(i==r&&(num_i>num_r)))//比较对手大小
{b:cout<<"这回合电脑赢了\n\n"<<endl;cout<<"是否继续游戏(y/n)\n\n"<<endl;
cin>>d;
if (d=='n')
{cout<<"游戏结束"<<endl;exit(0);}
else
return jia;}
else
{cout<<"这回合您赢了\n\n"<<endl;cout<<"是否继续游戏(y/n)\n\n"<<endl;
if (d=='n')
{cout<<"游戏结束"<<endl;exit(0);}
else
return zhen;}
}