您好,欢迎访问三七文档
#includestdio.h#includestdlib.h#includetime.h#defineNULL0intcompare(charsum1,charsum2){if(sum1=sum2)return1;elsereturn0;}intbreakal(intsum){if(sum21)return0;elsereturn1;}charchange(intcard){charch;intn,m,x;switch(card){case1:ch='A';break;case10:{n=1+(int)(rand()/(RAND_MAX+1.0));if(n==1)ch='K';else{m=1+(int)(rand()/(RAND_MAX+1.0));if(m==1)ch='Q';else{x=1+(int)(rand()/(RAND_MAX+1.0));if(x==1)ch='J';}}break;}}//以上switch语句用于将数字转换成字符returnch;}voidgame(){inti,k,n,host_card[15],player_card[15];charhost_ch,player_ch;inthost_sum=0,player_sum=0;staticinthost_point=1700,player_point=1700;srand(time(NULL));for(i=0;i2;i++){player_card[i]=(int)(11.0*rand()/(RAND_MAX+1.0));player_ch=change(player_card[i]);if(player_card[i]1&&player_card[i]10)//输出玩家抽到的牌的点数printf(您抽到的第%d张牌是%d\n,i+1,player_card[i]);elseif(player_card[i]==10||player_card[i]==11||player_card[i]==12||player_card[i]==13)printf(您要到的第%d张牌是%c\n,i+1,player_ch);elseprintf(您要到的第%d张牌是A\n,i+1);if(player_ch=='A')//询问判断玩家的A的点值为1或11{printf(您有一张A,您想要A作1还是11?\n);printf(1.1\n);printf(2.11\n);if(scanf(%d,&n)&&n==1)player_card[i]=1;elseplayer_card[i]=11;}host_card[i]=1+(int)(10.0*rand()/(RAND_MAX+1.0));host_ch=change(host_card[i]);if(host_ch=='A')//电脑自动判断庄家的A的点值为1或11{k=1+(int)(rand()/(RAND_MAX+1.0));if(k==1)host_card[i]=11;elsehost_card[i]=1;}player_sum+=player_card[i];//分别对庄家和玩家的点数求和host_sum+=host_card[i];}printf(您现在的总面值为%d\n,player_sum);//输出玩家的总面值for(i=2;i5;i++){if(host_sum18&&host_sum0)//电脑自动判断庄家是否要牌{host_card[i]=1+(int)(10.0*rand()/(RAND_MAX+1.0));host_ch=change(host_card[i]);if(host_ch=='A')//电脑自动判断庄家的A的点值为1或11{k=1+(int)(rand()/(RAND_MAX+1.0));if(k==1)host_card[i]=11;elsehost_card[i]=1;}host_sum+=host_card[i];}printf(您还要继续要牌吗?\n);printf(1.要\n);printf(2.不要\n);scanf(%d,&k);if(k==1){player_card[i]=1+(int)(10.0*rand()/(RAND_MAX+1.0));player_ch=change(player_card[i]);if(player_ch=='A')//询问判断玩家的A的点值为1或11{printf(您有一张A,您想要A作1还是11?\n);printf(1.1\n);printf(2.11\n);if(scanf(%d,&n)&&n==1)player_card[i]=1;elseplayer_card[i]=11;printf(您要到的第%d张牌是A\n,i+1);}elseprintf(您要到的第%d张牌是%d\n,i+1,player_card[i]);player_sum+=player_card[i];if(i==4){printf(您手中已有5张牌,一决胜负的时刻到啦!\n);if(breakal(host_sum)==0){if(breakal(player_sum)==0){printf(庄家爆牌了,您也爆牌了!庄家的牌面值是%d,您的牌面值是%d\n,host_sum,player_sum);printf(\nYoulose..\n);player_point-=20;host_point+=20;printf(庄家的分数是%d\n您的分数是%d\n,host_point,player_point);break;}printf(庄家爆牌了!庄家的牌面值是%d\n,host_sum);printf(\nCongratulations!Youwin!\n);player_point+=20;host_point-=20;printf(庄家的分数是%d\n您的分数是%d\n,host_point,player_point);break;}elseif(breakal(player_sum)==0){printf(您爆牌了!您的牌面值是%d\n\nYoulose..\n,player_sum);player_point-=20;host_point+=20;printf(庄家的分数是%d\n您的分数是%d\n,host_point,player_point);break;}else{printf(庄家的牌面值是%d\n您的牌面值是%d\n,host_sum,player_sum);if(compare(host_sum,player_sum)==1){printf(\nYoulose..\n);player_point-=20;host_point+=20;printf(庄家的分数是%d\n您的分数是%d\n,host_point,player_point);break;}elseprintf(\nCongratulations!Youwin!\n);host_point-=20;player_point+=20;printf(庄家的分数是%d\n您的分数是%d\n,host_point,player_point);break;}}if(breakal(host_sum)==0){if(breakal(player_sum)==0){printf(庄家爆牌了,您也爆牌了!庄家的牌面值是%d,您的牌面值是%d\n,host_sum,player_sum);printf(\nYoulose..\n);player_point-=20;host_point+=20;printf(庄家的分数是%d\n您的分数是%d\n,host_point,player_point);break;}printf(庄家爆牌了!庄家的牌面值是%d\n,host_sum);printf(\nCongratulations!Youwin!\n);player_point+=20;host_point-=20;printf(庄家的分数是%d\n您的分数是%d\n,host_point,player_point);break;}elseif(breakal(player_sum)==0){printf(您爆牌了!您的牌面值是%d\n\nYoulose..\n,player_sum);player_point-=20;host_point+=20;printf(庄家的分数是%d\n您的分数是%d\n,host_point,player_point);break;}}else{if(breakal(host_sum)==0){printf(庄家爆牌了!庄家的牌面值是%d\n,host_sum);printf(\nCongratulations!Youwin!\n);player_point+=20;host_point-=20;printf(庄家的分数是%d\n您的分数是%d\n,host_point,player_point);break;}printf(庄家的牌面值是%d\n您的牌面值是%d\n,host_sum,player_sum);if(compare(host_sum,player_sum)==1){printf(\nYoulose..\n);player_point-=20;host_point+=20;printf(庄家的分数是%d\n您的分数是%d\n,host_point,player_point);break;}elseprintf(\nCongratulations!Youwin!\n);host_point-=20;player_point+=20;printf(庄家的分数是%d\n您的分数是%d\n,host_point,player_point);break;}}}intmain(){intn;kaishi:printf(欢迎进入21点游戏!\n);printf(************************************\n);printf(1.开始游戏\n);printf(2.退出游戏\n);printf(************************************\n);printf(请输入1或2:);if(scanf(%d,&n)&&n==1)game();elseif(n==2){printf(******************************\n);printf(游戏结束!祝您愉快!\n);printf(\n\n);printf(再见!\n);printf(******************************\n);return0;}gotokaishi;}
本文标题:21点小游戏C程序
链接地址:https://www.777doc.com/doc-7311727 .html