21点游戏

#include
#include
#include 
#include 
#include 
#include 
#include 
using namespace std;
int gCard[52];
int e=1;
struct sCard
{
int naPip[5];           //一共5张牌
int nNumber;            //发了多少张牌
int nDollar;            //有多少钱
int nGamble;            //赌注
int nWin;               //赢局数
int nLose;              //输局数
int nDraw;              //平局数
};
int a[53];
void initcards(int a[]); //初始化牌:a[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,...}
void shuffle(int a[]);   //洗牌
void DisplayRule();      //printf游戏规则
void init(sCard &);      //初始化结构体sCard
int GetMoney(sCard sc);  //返回nDollar
int SetGamble(sCard &,int);    //设置赌本,赌本不够返回-1
void DisplayInfo(sCard);       //打印必要的信息
void FirstPlayTwo(sCard &cpu, sCard &player);  //最初两张牌
void DisplayPip(sCard);       //依次全部显示牌面点数
void DisplayPip_C(sCard);     //除了第一张牌,依次全部显示牌面点数(针对计算机牌的显示)
void PlayTurn(sCard &,sCard &);   //开始一局游戏
int GetNumber(sCard);        //返回牌数
void TurnPlay(sCard &cpu, sCard &player, int flag);  //要一张牌,flag为true时player要牌,flag为false时cpu要牌
int GetCurrentCard(sCard);   //返回当前牌点
void Draw(sCard &);         //平局
void Win(sCard &);          //嬴
void Lose(sCard &);         //输
int GetPip(sCard &);        //返回点数
void Judge(sCard &,sCard &);


int main()
{
    initcards(a);
    shuffle(a);
srand((unsigned)time(NULL));
sCard cpu, player;
init(cpu);
init(player);
int blLogic;
int nMoney;
DisplayRule();
char chChoice;
cout<<"是否现在开始游戏(Y/N)?\n";
cin>>chChoice;
while(chChoice=='Y'||chChoice=='y')
{
   e=1;
   memset(player.naPip,0,sizeof(player.naPip));
   memset(cpu.naPip,0,sizeof(cpu.naPip));
   shuffle(a);
do
{
cout<<"您现在有赌本:$"<>nMoney;
player.nGamble=nMoney;
blLogic = SetGamble(player, nMoney);
if(blLogic)
cout<<"您的赌本不够,请重新下注!\n";
}while(blLogic);
PlayTurn(cpu,player);
cout<<"是否继续21点游戏(Y/N)?\n";
cin>>chChoice;
}
DisplayInfo(player);
cout<<"\n\n您是明智的,赌博是不好的!回去好好学习去~!\n"<>chChoice;


getchar();
if((chChoice=='Y'||chChoice=='y'))
{
if(player.nNumber<=5)
{
                    TurnPlay(cpu,player,1);  //要一张牌,flag为true时player要牌,flag为false时cpu要牌
cout<<"您要的这张牌是:"<21)
                        {p=1;break;}
}
else
{
cout<<"对不起,您已经要了5次牌了。不能再要牌了!";
                    break;
}
}
}
if((chChoice=='N'||chChoice=='n'))
blPlayer=0;
if(GetPip(cpu)<16&&cpu.nNumber<5)   //cpu点数小于16点且牌数小于5张
{
            TurnPlay(cpu,player,0);  //要一张牌,flag为true时player要牌,flag为false时cpu要牌
cout<<"计算机要牌,牌点是:"<21)
                        {q=1;break;}
cpu.nNumber++;
}
else
blCpu=0;
if(blCpu&&GetNumber(player)<5&&GetPip(player)<21)
blPlayer=1;
}while(blCpu||blPlayer);
printf("您的牌为 ");
DisplayPip(player);
printf("您的牌点为 ");
printf("%d\n",GetPip(player));
printf("电脑的牌为 ");
DisplayPip(cpu);
printf("电脑牌点为 ");
printf("%d\n",GetPip(cpu));


if(p==1)
    {
        printf("您的牌爆掉,本局失败\n");
        Lose(player);
    }
    else
    if(q==1)
    {
        printf("电脑的牌爆掉,本局成功\n");
        Win(player);
    }
    else
  Judge(cpu,player);
}


void DisplayInfo(sCard sc)
{
cout<<"您一共玩了"<=10)
            sum+=10;
         else
            sum+=sc.naPip[i];
     }
     return sum;
}


int GetNumber(sCard sc)
{
     return sc.nNumber;
}


void TurnPlay(sCard &cpu, sCard &player,int flag)
{
     int i;
     if(flag==1)
     {
         for(i=0;i<5;i++)
         {
             if(player.naPip[i]==0)
             {
                 player.naPip[i]=a[e++];
                 break;
             }
         }
     }
     if(flag==0)
     {
         for(i=0;i<5;i++)
         {
             if(cpu.naPip[i]==0)
             {
                 cpu.naPip[i]=a[e++];
                 break;
             }
         }
     }
}


int GetCurrentCard(sCard sc)
{
    int n=0,i;
    for(i=4;i>=0;i--)
    {
        if(sc.naPip[i]!=0)
        {


                return sc.naPip[i];
        }
    }


}


void Judge(sCard &cpu,sCard &player)
{
if(GetPip(cpu)==GetPip(player))
{
Draw(player);
printf("您与电脑平局\n");
}
else if(GetPip(cpu)

 

你可能感兴趣的:(21点游戏)