我正在考虑中的扑克手(5张牌)评估Java。现在,我正在寻找简单明了而不是性能和效率。我可能可以写一个“天真的”算法,但是它需要很多代码。
Java
我还看到了一些使用散列和按位运算的扑克评估库,但是它们看起来相当复杂。
什么是最干净,最简单的扑克手评估算法?
这是Python(2.x)中非常简短但完整的基于直方图的5张纸牌扑克评分功能。如果转换为Java,它将变得更长。
def poker(hands): scores = [(i, score(hand.split())) for i, hand in enumerate(hands)] winner = sorted(scores , key=lambda x:x[1])[-1][0] return hands[winner] def score(hand): ranks = '23456789TJQKA' rcounts = {ranks.find(r): ''.join(hand).count(r) for r, _ in hand}.items() score, ranks = zip(*sorted((cnt, rank) for rank, cnt in rcounts)[::-1]) if len(score) == 5: if ranks[0:2] == (12, 3): #adjust if 5 high straight ranks = (3, 2, 1, 0, -1) straight = ranks[0] - ranks[4] == 4 flush = len({suit for _, suit in hand}) == 1 '''no pair, straight, flush, or straight flush''' score = ([1, (3,1,1,1)], [(3,1,1,2), (5,)])[flush][straight] return score, ranks >>> poker(['8C TS KC 9H 4S', '7D 2S 5D 3S AC', '8C AD 8D AC 9C', '7C 5H 8D TD KS']) '8C AD 8D AC 9C'