| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1481 人关注过本帖
标题:[原创]玩扑克的游戏
只看楼主 加入收藏
kingarden
Rank: 2
等 级:论坛游民
威 望:1
帖 子:517
专家分:40
注 册:2004-12-8
收藏
 问题点数:0 回复次数:3 
[原创]玩扑克的游戏

/*本程序说明:该程序图形显示需要驱动,用它把*.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*/ }



搜索更多相关主题的帖子: 扑克 游戏 
2005-01-19 02:48
kingarden
Rank: 2
等 级:论坛游民
威 望:1
帖 子:517
专家分:40
注 册:2004-12-8
收藏
得分:0 
/*这是个简单的动画程序*/

#include &lt;graphics.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;alloc.h&gt;
#include &lt;math.h&gt;
main()
{void * ptr;
int gd=DETECT,gm;
float i;
initgraph(&amp;gd,&amp;gm,"d:\\tc");
setactivepage(1);
setcolor(RED);
setfillstyle(1,14);
fillellipse(10,10,10,10);
ptr=malloc(imagesize(0,0,20,20));
getimage(0,0,20,20,ptr);
randomize();
setactivepage(0);
for(i=0;i&lt;3000;i++)
putpixel(random(640),random(480),random(15));
for(i=0;i&lt;6.28*4;i=i+0.005)
{putimage(300+8*(cos(i)+i*sin(i)),240+6*(sin(i)-i*cos(i)),ptr,COPY_PUT);
putimage(300+8*(cos(i)+(i)*sin(i)),240+6*(sin(i)-(i)*cos(i)),ptr,COPY_PUT);
if(kbhit()) break;
delay(100);
}
getch();
closegraph();
}

〖ΦγЙ┅给梦一把梯子,现实与梦想之间的距离即可忽略,不可跨越的迢迢银河举步便可迈过Й┅Φ〗
2005-01-19 02:49
grub2linux
Rank: 1
等 级:新手上路
帖 子:335
专家分:0
注 册:2004-12-25
收藏
得分:0 
其实,可以把自己C程序目录里的自带显示驱动整合到程序中,寄给别人就可玩了.一般的机器配置都差不多.

int gdriver=DETECT,gmode;
registerbgidriver(EGAVGA_driver);
initgraph(&amp;gdriver,&amp;gmode,"");

有缘相识!我中文系的 3月拿下C/C++/VC 5天写游戏 2月写游戏系统 有空去http://mykool.go2.
2005-01-22 12:31
grub2linux
Rank: 1
等 级:新手上路
帖 子:335
专家分:0
注 册:2004-12-25
收藏
得分:0 
我玩过了,我按下S后,什么也没给我做,就显示 SIDE A IS THE WINNER ! 玩不起来呀! 玩一玩我的吧: http://bbs.bc-cn.net/bbs/dispbbs.asp?boardid=99&id=11319&star=1#11319

[此贴子已经被作者于2005-1-22 12:41:02编辑过]


有缘相识!我中文系的 3月拿下C/C++/VC 5天写游戏 2月写游戏系统 有空去http://mykool.go2.
2005-01-22 12:37
快速回复:[原创]玩扑克的游戏
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018198 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved