五张牌的牌型比较,其实一百多行的代码就够了。

来源:互联网 发布:windows live注册页面 编辑:程序博客网 时间:2024/05/18 09:33

牌型分为9种,

杂牌,一对,两对,三条,顺子,同花,葫芦,四条,同花顺。

判断五张牌的牌型,其实代码页很简单,不需要很复杂。

#include<iostream>#include<functional>#include<algorithm>#include<string>using namespace std;struct PorkType{int type;int card[5];void setcard(int *p){for(int i=0;i<5;i++){card[i]=p[i];}}int operator -(PorkType & pt){if(type!=pt.type)return type-pt.type;for(int i=0;i<5;i++){if(card[i]!=pt.card[i])return card[i]-pt.card[i];}return 0;}};struct State{//high card 1, one pair 2, two pair 3,/*  1 High Card: Highest value card.2 One Pair: Two cards of the same value.3 Two Pairs: Two different pairs.4 Three of a Kind: Three cards of the same value.5 Straight: All cards are consecutive values.6 Flush: All cards of the same suit.7 Full House: Three of a kind and a pair.8 Four of a Kind: Four cards of the same value.9 Straight Flush: All cards are consecutive values of same suit.10 Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.*/static PorkType getType(int *pk,int *cl){int cardNum[15]={0};bool Straight;bool Flush;PorkType pt;for(int i=0;i<5;i++){cardNum[pk[i]]++;}Flush=true;for(int i=1;i<5;i++){if(cl[i]!=cl[i-1]){Flush=false;break;}}sort(pk,pk+5,greater<int>());Straight=true;for(int i=1;i<5;i++)if(pk[i]!=pk[i-1]-1){Straight=false;break;}pt.setcard(pk);if(Straight||Flush){if(Straight&&Flush){pt.type=9;}else if(Straight)pt.type=5;elsept.type=6;return pt;}// othersint pairs[5]={0};for(int i=2;i<=14;i++){pairs[cardNum[i]]++;}if(pairs[4]!=0)//四条{if(pk[0]!=pk[1]) // abbbb &&a>bswap(pk[0],pk[4]);pt.setcard(pk);pt.type=8;}else if(pairs[3]!=0){int i;for(i=0;i<3;i++){if(pk[i]==pk[i+1]&&pk[i+1]==pk[i+2]){break;}}// i,i+1,i+2int buf[5];if(i==0)pt.setcard(pk);else if(i==1){buf[0]=pk[1];buf[1]=pk[2];buf[2]=pk[3];buf[3]=pk[0];buf[4]=pk[4];pt.setcard(buf);}else{buf[0]=pk[2];buf[1]=pk[3];buf[2]=pk[4];buf[3]=pk[0];buf[4]=pk[1];pt.setcard(buf);}if(pairs[2]!=0)pt.type=7;elsept.type=4;}else if(pairs[2]!=0){int buf[5];if(pairs[2]==2){pt.type=3;if(pk[0]!=pk[1]){buf[0]=pk[1];buf[1]=pk[2];buf[2]=pk[3];buf[3]=pk[4];buf[4]=pk[0];pt.setcard(buf);}else if(pk[3]!=pk[4])//aabbc{pt.setcard(pk);}else// aabcc->aaccb{swap(pk[2],pk[4]);pt.setcard(pk);}}else{pt.type=2;int i;for(i=0;i<4;i++){if(pk[i]==pk[i+1])break;}buf[0]=pk[i];buf[1]=pk[i+1];int c=2;for(int j=0;j<i;j++)buf[c++]=pk[j];for(int j=i+2;j<5;j++)buf[c++]=pk[j];pt.setcard(buf);}}else //high card{pt.type=1;pt.setcard(pk);}return pt;}};

有了这个,我们就可以用计算机模拟计算一下,为啥牌型是按照这个顺序执行的,也就是各算一下各个的概率是多少。

我们随机生成100W的5张牌,然后分别看看是属于什么牌型的。

代码如下:

int main(){int types[10]={0};string typeName[10]={"","杂牌","一对","两对","三条","顺子","同花","葫芦","四条","同花顺",};for(int t=0;t<1000000;t++){int pk[5],color[5];int card[52];for(int i=0;i<52;i++)card[i]=i;//洗牌for(int i=51;i>0;i--){int j=rand()%(i+1);swap(card[i],card[j]);}//取前五张for(int i=0;i<5;i++){pk[i]=card[i]%13; //0-12是点数color[i]=card[i]/13;//0-3是花色}PorkType pt=State::getType(pk,color);types[pt.type]++;}for(int i=1;i<=9;i++){cout<<typeName[i]<<" 概率:"<<(double)types[i]/10000<<"%"<<endl;}return 0;}

结果如下:

杂牌 概率:57.0804%

一对 概率:37.0726%

两对 概率:3.3585%

三条 概率:1.8072%

顺子 概率:0.3534%(这里不包括A2345这样的顺子,如果包括的话概率会更大一些0.3931%左右)

同花 概率:0.2017%

葫芦 概率:0.1066%

四条 概率:0.0181%

同花顺 概率:0.0015%

请按任意键继续. . .

事实证明,顺子的概率要稍微大于同花,这也说明同花要比顺子大。

当然这个只是个大概数据,如果多个人从同一副牌里抓的话,每个人的牌型的概率会完全不同,打算概率的大小应该还是这个顺序的。

0 0
原创粉丝点击