概述
德州扑克中比较重要的一个算法就是计算牌面的得分,不仅仅关乎概率计算的结果,同时也需要很高的性能。蒙特卡洛算法计算的次数越多结果越准确,因此算法的性能至关重要。下面是一个高性能的牌面java计算算法。
参数介绍:
1)cards 需要计算分值的牌
2) nCards 需要计算牌的张数public static int evaluate(long cards, int nCards) {
int retval = 0, four_mask, three_mask, two_mask;
//本函数支持1-7张牌的计算
//按照牌色分开
int sc = (int) ((cards >> (CLUB_OFFSET)) & 0x1fffL);
int sd = (int) ((cards >> (DIAMOND_OFFSET)) & 0x1fffL);
int sh = (int) ((cards >> (HEART_OFFSET)) & 0x1fffL);
int ss = (int) ((cards >> (SPADE_OFFSET)) & 0x1fffL);
int ranks = sc | sd | sh | ss;
int n_ranks = nBitsTable[ranks];
int n_dups = ((int) (nCards - n_ranks));
//检查顺子、同花、同花顺并返回最佳牌面得分
if (n_ranks >= 5) {
if (nBitsTable[ss] >= 5) {
if (straightTable[ss] != 0)
return HANDTYPE_VALUE_STRAIGHTFLUSH + (int) (straightTable[ss] <
else
retval = HANDTYPE_VALUE_FLUSH + topFiveCardsTable[ss];
} else if (nBitsTable[sc] >= 5) {
if (straightTable[sc] != 0)
return HANDTYPE_VALUE_STRAIGHTFLUSH + (int) (straightTable[sc] <
else
retval = HANDTYPE_VALUE_FLUSH + topFiveCardsTable[sc];
} else if (nBitsTable[sd] >= 5) {
if (straightTable[sd] != 0)
return HANDTYPE_VALUE_STRAIGHTFLUSH + (int) (straightTable[sd] <
else
retval = HANDTYPE_VALUE_FLUSH + topFiveCardsTable[sd];
} else if (nBitsTable[sh] >= 5) {
if (straightTable[sh] != 0)
return HANDTYPE_VALUE_STRAIGHTFLUSH + (int) (straightTable[sh] <
else
retval = HANDTYPE_VALUE_FLUSH + topFiveCardsTable[sh];
} else {
int st = straightTable[ranks];
if (st != 0)
retval = HANDTYPE_VALUE_STRAIGHT + (st <
}
//如果没有更好的牌(如:金刚、葫芦)直接返回得分
if (retval != 0 && n_dups
return retval;
}
//继续检查其他牌型
switch (n_dups) { //相同牌的数量
case 0:
//高牌
return HANDTYPE_VALUE_HIGHCARD + topFiveCardsTable[ranks];
case 1: {
//一对
int t, kickers;
two_mask = ranks ^ (sc ^ sd ^ sh ^ ss);
retval = (int) (HANDTYPE_VALUE_PAIR + (topCardTable[two_mask] <
t = ranks ^ two_mask;
//计算对牌的分数
kickers = (topFiveCardsTable[t] >> CARD_WIDTH) & ~FIFTH_CARD_MASK;
retval += kickers;
return retval;
}
case 2:
// 2对或者三条
two_mask = ranks ^ (sc ^ sd ^ sh ^ ss);
if (two_mask != 0) { //2对
int t = ranks ^ two_mask;
retval = (int) (HANDTYPE_VALUE_TWOPAIR
+ (topFiveCardsTable[two_mask] & (TOP_CARD_MASK | SECOND_CARD_MASK)) + (topCardTable[t] <
return retval;
} else { //三条
int t, second;
three_mask = ((sc & sd) | (sh & ss)) & ((sc & sh) | (sd & ss));
retval = (int) (HANDTYPE_VALUE_TRIPS + (topCardTable[three_mask] <
t = ranks ^ three_mask; /** Only one bit set in three_mask */
second = topCardTable[t];
retval += (second <
t ^= (1 <
retval += (int) (topCardTable[t] <
return retval;
}
default: //可能是葫芦、金刚、顺子、同花、或者2对
four_mask = sh & sd & sc & ss;
if (four_mask != 0) { //金刚
int tc = topCardTable[four_mask];
retval = (int) (HANDTYPE_VALUE_FOUR_OF_A_KIND + (tc <
^ (1 <
return retval;
}
/**
* Technically, three_mask as defined below is really the set of bits which are set in three or four of the
* suits, but since we've already eliminated quads, this is OK
*/
/**
* Similarly, two_mask is really two_or_four_mask, but since we've already eliminated quads, we can use this
* shortcut
*/
two_mask = ranks ^ (sc ^ sd ^ sh ^ ss);
if (nBitsTable[two_mask] != n_dups) {
//葫芦
int tc, t;
three_mask = ((sc & sd) | (sh & ss)) & ((sc & sh) | (sd & ss));
retval = HANDTYPE_VALUE_FULLHOUSE;
tc = topCardTable[three_mask];
retval += (tc <
t = (two_mask | three_mask) ^ (1 <
retval += (int) (topCardTable[t] <
return retval;
}
if (retval != 0) // 顺子或同花
return retval;
else {
// 2对
int top, second;
retval = HANDTYPE_VALUE_TWOPAIR;
top = topCardTable[two_mask];
retval += (top <
second = topCardTable[two_mask ^ (1 <
retval += (second <
retval += (int) ((topCardTable[ranks ^ (1 <
return retval;
}
}
}
最后
以上就是笨笨板凳为你收集整理的java中使用几率_Java中使用蒙特卡洛算法计算德州扑克成牌概率(二)- 计算牌面分值...的全部内容,希望文章能够帮你解决java中使用几率_Java中使用蒙特卡洛算法计算德州扑克成牌概率(二)- 计算牌面分值...所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复