#include<time.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void Wait_f(int);
void Pause_f();/*暂停,按任意键继续*/
int Random_f(long ,long );/*返回两个参数之间的随机数*/
void Replay_f(char *);/*询问是否重开一局*/
void Hit_f(int *);/*发一张牌*/
void Deal_f(int *,int *,int *,int *);/*双方各发两张牌*/
void Bet_f(int *,int *);/*接受下注的数目*/
void Print_f(int ,int ,int ,int );/*打印结果*/
void Rules_f();/*显示游戏规则*/
void Results_f(int ,int ,int ,int *,int *,int *,int *);/*统计结果(输赢平)*/
void Hit_f2(int * npTotal,int a);   /*作弊程序*/


int nnn;

int main()
{
     int nPlayer=0,nCpu=0,nWin=0,nLose=0,nDraw=0,nPlayerTurns=0,
        nCpuTurns=0,nMoney=0,nBet;
     /* nPlayer--玩家手中全部牌的点数和,nPlayerTurns--玩家手中牌的数量*/
     char chAnswer;
        char mmm;
        int a;
     system("cls");/* 执行系统命令"清屏" */
     Rules_f();
     printf("\t\t\tWOULD YOU LIKE TO PLAY?  ");
     scanf("%c",&chAnswer);
     if((chAnswer=='y')||(chAnswer=='Y'))
     {
         printf("\t\t\tI WILL LET YOU START WITH $100\n");
         nMoney=100;
         printf("\t\t\t");
         Pause_f();
     }
     else
    {
       return(0);
    }
do
{
        system("cls");
    if(nMoney<0)
    {
        printf("Your broke\n");
        return(0);
    }
    Bet_f(&nBet,&nMoney);/*接受下注的数目*/
    Deal_f(&nPlayer,&nCpu,&nPlayerTurns,&nCpuTurns);/*双方各发两张牌*/

     printf("\t\t\tWould you want to cheat? yes or no?");
        scanf("%c",&mmm);
        if((mmm=='y')||(mmm=='Y'))
        {

            printf("%d",nnn);
        printf("\t\t\twhich card do you want?");
        scanf("%d",&a);
        }

                system("cls");
                do
    {
                



        printf("\t\t\tWould You Like To Hit or Stay :");/*询问玩家是否接着要牌*/
        scanf(" %c",&chAnswer);
        if((chAnswer=='h')||(chAnswer=='H'))
        {
        nPlayerTurns++;/*计算总牌数*/
        if(nPlayerTurns>5)/*看是否允许再要牌*/
            {
                printf("\t\t\tYou Can't Have more than 5 cards");
            }
        }
        

             if((nPlayerTurns<6)&&(chAnswer=='h'))
    {
        printf("\n");

       if((mmm=='y')||(mmm=='Y'))
          Hit_f2(&nPlayer,a);

        else Hit_f(&nPlayer);/*为玩家发一张牌*/

    }
        
    }


    while((chAnswer=='h')||(chAnswer=='H'));

for(;(nCpu<16)&&(nCpuTurns<6);nCpuTurns++)/*计算机要牌的条件*/
{
    printf("\n");
    printf("\t\t\tThe Dealer Takes A Card");
    Hit_f(&nCpu);/*为计算机发一张牌*/
}
    printf("\n\n");
    printf("\t\t\tThe Dealer Has A Total: %d\n", nCpu);/*显示总点数*/
    printf("\t\t\tYou Have A Total Of: %d\n", nPlayer);
    printf("\n");
    Results_f(nPlayer,nCpu,nBet,&nMoney,&nDraw,&nWin,&nLose);/*计算最后的结果(统计输赢平的局数)*/
    Replay_f(&chAnswer);/*询问是否重开一局*/
}while((chAnswer=='y')||(chAnswer=='Y'));
Print_f(nWin,nLose,nDraw,nMoney);/*打印最后结果*/
printf("\n");
printf("\t\t\t\t");
return(0);
}



void Rules_f()  /*这个函数显示游戏规则*/
{
   printf("\t\t WELCOME TO ISAAC SHAFFERS BLACK JACK GAME\n");
   printf("\t\t\t HERE ARE SOME SIMPLE RULES\n");
   printf("\t\t\t1:You Can only have a max of 5 cards.\n");
   printf("\t\t\t2:If you bust you automatically lose.\n");
   printf("\t\t\t3:If you win you win double what you bet");
   printf("\t\t\t4:The Dealer stops at or after 16.\n");
   printf("\t\t\t\t");
   getchar();

}

void Bet_f(int* npBet,int* npMoney)  /*该函数接受玩家下注,并计算赌资的总数*/
{
    system("cls");
    printf("\t\t\tYOU HAVE:$%d\n",*npMoney);
    printf("\t\t\tHow Much Do You Wish To Bet:");
    scanf("%d",npBet);
    if(*npBet<0)
    {
       *npBet=*npBet*(-1);

    }
    *npMoney=*npMoney-*npBet;

}


void Deal_f(int* npPlayer,int*npCpu,int*npPlayeReturns,int *npCpuReturns)
/*该函数是给计算机与玩家各发两张牌*/
{  int d=rand()%4+3;
    int c=rand()%4+2;
  int nPlayerCard1,nPlayerCard2,nCpuCard1,nCpuCard2;
  *npPlayeReturns=2;
  *npCpuReturns=2;
  nPlayerCard1=Random_f(13,1);
  printf("\n\t\t\tDEALING HAND\n");
  getchar();
  Pause_f();
  nPlayerCard2=Random_f(13,1);
  Pause_f();
  nCpuCard1=Random_f(13,1);
Pause_f();
nCpuCard2=Random_f(13,1);
if (nPlayerCard1>=10)
{
    nPlayerCard1=10;
}
if (nPlayerCard2>=10)
{
    nPlayerCard2=10;
}
if (nCpuCard1>=10)
{
    nCpuCard1=10;
}
if (nCpuCard2>=10)
{
    nCpuCard2=10;
}
*npPlayer=nPlayerCard1+nPlayerCard2;
*npCpu=nCpuCard1+nCpuCard2;
printf("\t\t\tYOU HAVE a total of:%d\n",*npPlayer);
    printf("%c%c%c%c%c%c%c\n",201,205,205,205,205,205,187);
    printf("%c%c%d%c%c%c%c\n",186,d,nPlayerCard1,0,0,0,186);
    printf("%c%c%c%c%c%c%c\n",186,0,0,0,0,0,186);
    printf("%c%c%c%c%c%c%c\n",186,0,0,0,0,0,186);
    printf("%c%c%c%c%d%c%c\n",186,0,0,0,nPlayerCard1,d,186);
    printf("%c%c%c%c%c%c%c\n",200,205,205,205,205,205,188);


    printf("%c%c%c%c%c%c%c\n",201,205,205,205,205,205,187);
    printf("%c%c%d%c%c%c%c\n",186,c,nPlayerCard2,0,0,0,186);
    printf("%c%c%c%c%c%c%c\n",186,0,0,0,0,0,186);
    printf("%c%c%c%c%c%c%c\n",186,0,0,0,0,0,186);
    printf("%c%c%c%c%d%c%c\n",186,0,0,0,nPlayerCard2,c,186);
    printf("%c%c%c%c%c%c%c\n",200,205,205,205,205,205,188);

printf("\n");
printf("\t\t\tThe DEALER HAS A %d SHOWING",nCpuCard1);
printf("\n");
printf("[*][%d]",nCpuCard1);

}
void Hit_f(int * npTotal) /* 发一张牌,返回点数之和*/
{   int a=rand()%4+3;
    int nCard;
    nCard=Random_f(13,1);
    if(nCard>=10)
    {
        nCard=10;
    }
    * npTotal=* npTotal+nCard;
   
    printf("%c%c%c%c%c%c%c\n",201,205,205,205,205,205,187);
    printf("%c%c%d%c%c%c%c\n",186,a,nCard,0,0,0,186);
    printf("%c%c%c%c%c%c%c\n",186,0,0,0,0,0,186);
    printf("%c%c%c%c%c%c%c\n",186,0,0,0,0,0,186);
    printf("%c%c%c%c%d%c%c\n",186,0,0,0,nCard,a,186);
    printf("%c%c%c%c%c%c%c\n",200,205,205,205,205,205,188);

    printf("\t\t\tTotal is :%d\n", * npTotal);

}
void Results_f(int nPlayer,int nCpu,int nBet,int * npMoney,int * npDraw,int
               * npWin,int * npLose)/*统计结果(输赢平及输赢平的局数)*/
{
    if(nCpu==nPlayer)
    {
        printf("\t\t\tIT WAS A DRAW HOUSE WINS\n");
        ++* npDraw;
    }
    if(nPlayer>21)
    {
        printf("\t\t\tYou Bust\n");
            ++* npLose;
    }
    else
    {
        if(nCpu<nPlayer)
        {
            printf("\n\t\t\tYOU WIN");
            *npMoney=*npMoney+(nBet*2);
            ++* npWin;
        }
    }
    if(nCpu>21)
    {
        printf("\t\t\tDealer Bust\n");
        if(nPlayer<=21)
        {
            printf("\n\t\t\tYOU WIN");
            ++* npWin;
            *npMoney=*npMoney+(nBet*2);
        }
    }
    else
    {
        if(nCpu>nPlayer)
        {
            printf("\t\t\tYOU LOSE\n");
            ++* npLose;
        }
    }
}
void Replay_f(char *chpAns)  /*询问是否重开一局*/
{





  printf("\n\t\tWould You Like To Play This Game Again:");
    scanf("%c",chpAns);
}

void Print_f(int nWins,int nLose,int nDraw,int nMoney)
{
   printf("\t\t\t\tWINS:%d\n", nWins);
   printf("\t\t\t\tLOSE:%d\n", nLose);
   printf("\t\t\t\tDRAWS:%d\n",nDraw);
   printf("\t\t\t\tMONEY:%d\n",nMoney);
}
void Wait_f(int nMilli)
{
   int nStart1,nStart2;
   for(nStart1=0;nStart1<nMilli*100;nStart1++)
    for(nStart2=0;nStart2<nMilli*100;nStart2++)
    {;}
}

void Pause_f()
{
   printf("Press Any Key To Continue");
   getchar();
}

int Random_f(long lBigNumber, long lSmallNumber) /*选择两个参数之间的随机数*/
{
   int nRan;
   srand((unsigned)time(NULL));
   nRan=rand()%(lBigNumber-(lSmallNumber-1))+lSmallNumber;
   return(nRan);
}

   void Hit_f2(int * npTotal,int a) /* 发一张牌,返回点数之和*/
{    int c;
    int nCard;
    nCard=a;
    c=rand()%4+4;
    if(nCard>=10)
    {
        nCard=10;
    }
    * npTotal=* npTotal+nCard;
    printf("%c%c%c%c%c%c%c\n",201,205,205,205,205,205,187);
    printf("%c%c%d%c%c%c%c\n",186,c,nCard,0,0,0,186);
    printf("%c%c%c%c%c%c%c\n",186,0,0,0,0,0,186);
    printf("%c%c%c%c%c%c%c\n",186,0,0,0,0,0,186);
    printf("%c%c%c%c%d%c%c\n",186,0,0,0,nCard,c,186);
    printf("%c%c%c%c%c%c%c\n",200,205,205,205,205,205,188);

    printf("\t\t\tTotal is :%d\n", * npTotal);
    
}