/*本程序说明:该程序图形显示需要驱动,用它把*.BGI编译成目标文件*.OBJ。如对C:\>BGIOBJ CGA,这样可生成一个CGA.OBJ. 并且要将生成的文件包含到库文件中,图形才能正常显示。*/
/*要求:简单扑克牌游戏,在此程序中写了扑克牌游戏的比较规则: 先把每一方牌的类型确定出来,如果类型不同,则输赢可以确定. 如果牌的类型是一样的,则对应每一类牌,都有对应的函数来处理.*/ #include <stdlib.h> #include <graphics.h> #include <stdio.h> #include <conio.h>
enum poker{ hong,fang,mei,hei}; /*define enum type*/ typedef struct tpoke /*define struct */ { enum poker p; int num; }poke; int pandan(poke p1,poke p2,poke p3,poke p4,poke p5,poke p6)/*judge A and B is equal or not ,yes->repeat start*/ { if((p1.p==p2.p&&p1.num==p2.num)||(p1.p==p3.p&&p1.num==p3.num)||(p1.p==p4.p&&p1.num==p4.num)||(p1.p==p5.p&&p1.num==p5.num)||(p1.p==p6.p&&p1.num==p6.num)) return 1; else if((p2.p==p3.p&&p2.num==p3.num)||(p2.p==p4.p&&p2.num==p4.num)||(p2.p==p5.p&&p2.num==p5.num)||(p2.p==p6.p&&p2.num==p6.num)) return 1; else if((p3.p==p4.p&&p3.num==p4.num)||(p3.p==p5.p&&p3.num==p5.num)||(p3.p==p6.p&&p3.num==p6.num)) return 1; else if((p4.p==p5.p&&p4.num==p5.num)||(p4.p==p6.p&&p4.num==p6.num)) return 1; else if(p5.p==p6.p&&p5.num==p6.num) return 1; else return 0; } beginp(poke pka[3],poke pkb[3]) /*Start playing*/ { int i,k; printf("\t\t Side A \t\tSide B\n"); do{ randomize(); /*initiate randomize function*/ for(i=0;i<3;i++) { k=random(4); switch(k) { case 0: pka[i].p=0; break; case 1: pka[i].p=1; break; case 2: pka[i].p=2; break; case 3: pka[i].p=3; break; } k=random(4); switch(k) { case 0: pkb[i].p=0; break; case 1: pkb[i].p=1; break; case 2: pkb[i].p=2; break; case 3: pkb[i].p=3; break; } k=random(13); switch(k) { case 0: pka[i].num=14; break; case 1: pka[i].num=13; break; case 2: pka[i].num=2; break; case 3: pka[i].num=3; break; case 4: pka[i].num=4; break; case 5: pka[i].num=5; break; case 6: pka[i].num=6; break; case 7: pka[i].num=7; break; case 8: pka[i].num=8; break; case 9: pka[i].num=9; break; case 10: pka[i].num=10; break; case 11: pka[i].num=11; break; case 12: pka[i].num=12; break; } k=random(13); switch(k) { case 0: pkb[i].num=14; break; case 1: pkb[i].num=13; break; case 2: pkb[i].num=2; break; case 3: pkb[i].num=3; break; case 4: pkb[i].num=4; break; case 5: pkb[i].num=5; break; case 6: pkb[i].num=6;break; case 7: pkb[i].num=7; break; case 8: pkb[i].num=8; break; case 9: pkb[i].num=9; break; case 10: pkb[i].num=10; break; case 11: pkb[i].num=11; break; case 12: pkb[i].num=12; break; }}}while(pandan(pka[0],pka[1],pka[2],pkb[0],pkb[1],pkb[2])); /*when A and B is equal,repeat it*/ for(i=0;i<3;i++) { switch(pka[i].p) { case 0: printf("\t\t fang");break; case 1: printf("\t\t mei");break; case 2: printf("\t\t hong");break; case 3: printf("\t\t hei");break; } switch(pkb[i].p) { case 0: printf("\t\t fang");break; case 1: printf("\t\t hei");break; case 2: printf("\t\t hong");break; case 3: printf("\t\t hei");break; } delay(10); switch(pka[i].num) { case 14: printf("\t\t %c",65);break; case 13: printf("\t\t %c",75);break; case 2: printf("\t\t %d",2);break; case 3: printf("\t\t %d",3);break; case 4: printf("\t\t %d",4);break; case 5: printf("\t\t %d",5);break; case 6: printf("\t\t %d",6);break; case 7: printf("\t\t %d",7);break; case 8: printf("\t\t %d",8);break; case 9: printf("\t\t %d",9);break; case 10: printf("\t\t %d",10);break; case 11: printf("\t\t %c",74);break; case 12: printf("\t\t %c",81);break; } switch(pkb[i].num) { case 14: printf("\t %c\n",65);break; case 13: printf("\t %c\n",75);break; case 2: printf("\t %d\n",2);break; case 3: printf("\t %d\n",3);break; case 4: printf("\t %d\n",4);break; case 5: printf("\t %d\n",5);break; case 6: printf("\t %d\n",6);break; case 7: printf("\t %d\n",7);break; case 8: printf("\t %d\n",8);break; case 9: printf("\t %d\n",9);break; case 10: printf("\t %d\n",10);break; case 11: printf("\t %c\n",74);break; case 12: printf("\t %c\n",81);break; } } }
int add(int a,int b,int c) { return (a+b+c); } int shunzi(int a,int b,int c) /*判断三个数是否为连续三个数的函数,是则返回1,不是则返回0*/ { int t; if(a<b) {t=a;a=b;b=t;} if(a<c) {t=a;a=c;c=t;} if(b<c) {t=b;b=c;c=t;} if((a==(b+1))&&(b==(c+1))) return 1; else return 0; }
int duizi(poke pk[3]) /*判断发出的牌是不是对子,是返回1,不是返回0*/ { int flag; if((pk[0].num==pk[1].num)||(pk[0].num==pk[2].num)||(pk[1].num==pk[2].num)) flag=1; else flag=0; return flag; } int ptype(poke pk[3]) /*定义发出牌的五种类型,同花最大返回5,顺子返回4,同点返回3,对子返回2,杂牌返回1*/ { int flag; if(pk[0].p==pk[1].p&&pk[1].p==pk[2].p) flag=5; else if(shunzi(pk[0].num,pk[1].num,pk[2].num)==1) flag=4; else if(pk[0].num==pk[1].num&&pk[0].num==pk[2].num) flag=3; else if(duizi(pk)==1) flag=2; else flag=1; return flag; } int tonghua(poke pukea[3],poke pukeb[3]) /*如果A方与B方的牌都为同花的比较过程*/ { if(add(pukea[0].num,pukea[1].num,pukea[2].num)>add(pukeb[0].num,pukeb[1].num,pukeb[2].num)) return 1; else if(add(pukea[0].num,pukea[1].num,pukea[2].num)==add(pukeb[0].num,pukeb[1].num,pukeb[2].num)) return 0; else return -1; } int pdshui(poke pukea[3],poke pukeb[3]) /*如果A方与B方的牌都是顺子的比较规则*/ { int flag; flag=-1; if((pukea[0].p!=pukea[1].p&&pukea[1].p!=pukea[2].p&&pukea[0].p!=pukea[2].p)&&(add(pukea[0].num,pukea[1].num,pukea[2].num)==39)) /*A为不同花色的QKA*/ { if((pukeb[0].p!=pukeb[1].p&&pukeb[1].p!=pukeb[2].p&&pukeb[0].p!=pukeb[2].p)&&(add(pukeb[0].num,pukeb[1].num,pukeb[2].num)==39)) /*B为不同花色的QKA*/ flag=0;/*平局*/ else flag=1;/*A胜*/ } else if(!((pukeb[0].p!=pukeb[1].p&&pukeb[1].p!=pukeb[2].p&&pukeb[0].p!=pukeb[2].p)&&(add(pukeb[0].num,pukeb[1].num,pukeb[2].num)==39))) /*B不为不同花色的QKA*/ { if(add(pukea[0].num,pukea[1].num,pukea[2].num)>add(pukeb[0].num,pukeb[1].num,pukeb[2].num)) flag=1; /*在B不为不同花色的QKA的情况下,若A中牌的点数和大于B,则A胜*/ else if(add(pukea[0].num,pukea[1].num,pukea[2].num)==add(pukeb[0].num,pukeb[1].num,pukeb[2].num)) flag=0; /*A中牌的点数和等于B,平局*/ } return flag; } int tongdian(poke pukea[3],poke pukeb[3])/*A,B都为同点时的处理情况*/ { int flag; flag=-1; if(add(pukea[0].num,pukea[1].num,pukea[2].num)>add(pukeb[0].num,pukeb[1].num,pukeb[2].num)) flag=1; else if(add(pukea[0].num,pukea[1].num,pukea[2].num)==add(pukeb[0].num,pukeb[1].num,pukeb[2].num)) flag=1; return flag; } int pdduizi(poke pukea[3],poke pukeb[3]) /*A,B都为对子时的处理情况*/ { int flag; flag=-1; if(add(pukea[0].num,pukea[1].num,pukea[2].num)>add(pukeb[0].num,pukeb[1].num,pukeb[2].num)) flag=1; else if(add(pukea[0].num,pukea[1].num,pukea[2].num)==add(pukeb[0].num,pukeb[1].num,pukeb[2].num)) flag=1; return flag; } int zapai(poke pukea[3],poke pukeb[3]) /*A,B都为杂牌时的处理情况*/ { int flag; flag=-1; if((pukea[0].p!=pukea[1].p&&pukea[0].p!=pukea[2].p&&pukea[1].p!=pukea[2].p)&&(add(pukea[0].num,pukea[1].num,pukea[2].num)==38)) /*A为不同花色的JKA*/ { if((pukeb[0].p!=pukeb[1].p&&pukea[0].p!=pukeb[2].p&&pukeb[1].p!=pukeb[2].p)&&(add(pukeb[0].num,pukeb[1].num,pukeb[2].num)==38)) /*B也为不同花色的JKA*/ flag=0; else flag=1; } else if(!((pukeb[0].p!=pukeb[1].p&&pukeb[0].p!=pukeb[2].p&&pukeb[1].p!=pukeb[2].p)&&(add(pukeb[0].num,pukeb[1].num,pukeb[2].num)==38))) /*B不为不同花色的JKA*/ { if(add(pukea[0].num,pukea[1].num,pukea[2].num)>add(pukeb[0].num,pukeb[1].num,pukeb[2].num)) flag=1; else if(add(pukea[0].num,pukea[1].num,pukea[2].num)==add(pukeb[0].num,pukeb[1].num,pukeb[2].num)) flag=0; } return flag; } char proceed(poke pukea[3],poke pukeb[3]) /*if(A>B) show '>' else if(A=B) show '=' else show '<';*/ { char result; int flaga,flagb; result='<'; flaga=ptype(pukea); flagb=ptype(pukeb); if(flaga>flagb) result='>'; else if(flaga==flagb) { switch(flaga) { case 5:if(tonghua(pukea,pukeb)==1) result='>'; else if(tonghua(pukea,pukeb)==0) result='='; else result='<'; break;/*A B is tonghua*/ case 4: if(pdshui(pukea,pukeb)==1) result='>'; else if(pdshui(pukea,pukeb)==0) result='='; else result='<'; break;/*A B is shuzi*/ case 3: if(tongdian(pukea,pukeb)==1) result='>'; else if(tongdian(pukea,pukeb)==0) result='='; else result='<'; break; case 2: if(pdduizi(pukea,pukeb)==1) result='>'; else if(pdduizi(pukea,pukeb)==0) result='='; else result='<';break; case 1: if(zapai(pukea,pukeb)==1) result='>'; else if(zapai(pukea,pukeb)==0) result='='; else result='<';break; } } return result; } void result(poke pka[3],poke pkb[3])/*show the result*/ { if(proceed(pka,pkb)=='>') printf("\n\t\tSide A is the winner\n\n\n"); if(proceed(pka,pkb)=='=') printf("\n\t\tSide A is euqal to Side B\n"); if(proceed(pka,pkb)=='<') printf("\n\t\tSide B is the winner\n\n\n"); } void clearbuffer() /*clear buffer*/ { while(getchar()!='\n'); } /*#include <stdio.h> #include <graphics.h> #include <stdlib.h> #include <conio.h>*/ main() { int gd=DETECT,gm; /*initiate graphics driver*/ poke pka[3],pkb[3]; /*define three poke array*/ char ch; initgraph(&gd,&gm,""); setbkcolor(BLUE); /*set text background setcolor(4); and set font color*/
while(1) { printf("\n\t *******Welcome To Use Playing Card Program******"); printf("\n\t *****Start********Quit*****\n"); printf("\t Please input the character S or Q:\n\n"); printf("\t S-->Start Q-->quit\n\n"); ch=toupper(getchar()); if(ch!='\n') clearbuffer(); switch(ch) { case 'S': beginp(pka,pkb);result(pka,pkb);break; case 'Q': exit(0); } } getch(); closegraph(); /*close graphic*/ }