#include<stdio.h>
#include<conio.h>
#include<time.h>
void print_f() /*初始化子函数*/
{
printf("21 hits game\n");
printf("\n");
srand(time(0)); /*用时间作为随机子*/
printf("You have 20 dollars.\n"); /*预充钱币20美元*/
}
int f1(int a,int b) /*当点数不为10时候的扑克牌显示*/
{
printf("%c%c%c%c%c%c\n",201,205,205,205,205,187);
printf("%c%c%c%c%c%c\n",186,a,b,0,0,186);
printf("%c%c%c%c%c%c\n",186,0,0,0,0,186);
printf("%c%c%c%c%c%c\n",186,0,0,b,a,186);
printf("%c%c%c%c%c%c\n",200,205,205,205,205,188);
}
int f2(int a,int b) /*当点数为10的时候的扑克牌显示*/
{
printf("%c%c%c%c%c%c\n",201,205,205,205,205,187);
printf("%c%c%d%c%c\n",186,a,b,0,186);
printf("%c%c%c%c%c%c\n",186,0,0,0,0,186);
printf("%c%c%d%c%c\n",186,0,b,a,186);
printf("%c%c%c%c%c%c\n",200,205,205,205,205,188);
}
int f3(int p) /*点数显示转换*/
{ int n;
if(p==1) n=65;
if(p==10)n=10;
if(p==11)n=74;
if(p==12)n=81;
if(p==13)n=75;
else if(p>1&&p<10)
n=p+48;
return n;
}
int f4(int p) /*点数代表的实际数据*/
{
int n;
if(p==1) n=1;
if(p>10) n=10;
else if(p>1&&p<=10)
n=p;
return n;
}
void main() /*主函数*/
{ int j,a=20;
print_f();
for(j=0;j<1000;j++) /*for循环实现玩家是否继续下一盘*/
{
int p[10],q[10],h[10],m[10],n[10];
int b=0,c=0;
int t,i=2;
char k=0;
p[0]=rand()%13+1; /*输出玩家的前两张牌和计算庄家的两张牌的点数和*/
h[0]=rand()%4+3;
p[1]= rand()%13+1;
h[1]=rand()%4+3;
q[0]=rand()%13+1;
q[1]=rand()%13+1;
m[0]=f4(p[0]); /*随机数*/
m[1]=f4(p[1]);
n[0]=f4(q[0]);
n[1]=f4(q[1]);
if(f3(p[0])==10 && f3(p[1])!=10 ) /*实现牌的输出和依据条件转换*/
{f2(h[0],f3(p[0]));
f1(h[1],f3(p[1])); }
else if(f3(p[0])!=10 && f3(p[1])==10 ) /*实现牌的输出和依据条件转换*/
{f1(h[0],f3(p[0]));
f2(h[1],f3(p[1])); }
else if(f3(p[0])!=10 && f3(p[1])!=10 ) /*实现牌的输出和依据条件转换*/
{f1(h[0],f3(p[0]));
f1(h[1],f3(p[1])); }
else
{f2(h[0],f3(p[0])); /*实现牌的输出和依据条件转换*/
f2(h[1],f3(p[1])); }
b += m[0]+m[1]; /*点数累加*/
c+= n[0]+n[1];
printf("Want anther? 'y' or 'n'\n");
do /*继续要牌的循环体*/
{ k=getchar();
if(k=='n') break;
else if(k!='y')
{
do{printf("Errors.Please input another word.\n"); /*控制输入的循环*/
scanf(“%c”,&k); }while(k!='y'&& k!='n');
if(k=='n') break;
}
p[i]=rand()%13+1;
h[i]=rand()%4+3;
q[i]=rand()%13+1;
m[i]=f4(p[i]);
b+= m[i];
if(c<18) /*用if语句来控制专庄家的点数,使之不能无限增大,增加游戏的公平性*/
c+=n[1];
if(f3(p[i])==10)
f2 (h[i],f3 (p[i]));
else
f1(h[i],f3 (p[i]));
i++;
if(b>21) break;
}while(k=='y');
printf("Your count is %d\nThe computer is %d\n",b,c);
if(b>21) /*判断输赢*/
{ printf("you lose.\n");
a-=2;}
else
if(c>21)
{ printf("you win.\n");
a+=2;}
else
if(b>c )
{printf("you win.\n");
a+=2;}
else
{printf("you lose.\n");
a-=2;}
printf("Your money is %d.\n",a);
if(a<1) {printf("You don't have money.\n"); break;} /*当钱不够的时候就不能玩了*/
printf("Want again? '1' or '0'\n");
scanf("%d",&t); /*选择要不要再来一盘*/
if(t==0) break;
else if(t!=1)
{
do{printf("Mate Errors.\n"); /*控制输入的循环*/
scanf("%d",&t);}while(t!=0 && t!=1);
if(t==0) break;
}
}
getch();
}