【问题标题】:straight flush and royal flush for poker in pythonpython中扑克的同花顺和皇家同花顺
【发布时间】:2019-11-25 04:54:08
【问题描述】:

这是我迄今为止的一些扑克游戏代码,我得到了 def main 来展示一些牌,但我不知道得到同花顺和皇家同花顺。还有,我该怎么做高牌?

卡号值 0-12 是一个花色,13-25 是下一个花色,26-38 和 39-51。

另外,如果有人能告诉我如何将这个 PokerHand 类实现到另一个 def 主窗口中。

class PokerHand:

"""class for representing a poker hand"""

# Poker value of hands in increasing order so they can be compared
HIGH_CARD = 0
TWO_OF_A_KIND = 1
TWO_PAIRS = 2
THREE_OF_A_KIND = 3
STRAIGHT = 4
FLUSH = 5
FULL_HOUSE = 6
FOUR_OF_A_KIND = 7
STRAIGHT_FLUSH = 8

# hand names for printing the card names
HAND_NAMES = ('High Card', 'Two of a Kind', 'Two Pairs', 'Three of a Kind',
             'Straight', 'Flush', 'Full House', 'Four of a Kind',
             'Straight Flush')


#------------------------------------------------------------------

def __init__(self):

    """initialize empty hand"""

    self.cards = []

#------------------------------------------------------------------

def addCard(self, cardNumber):

    """add cardNumber to the hand"""

    self.cards.append(cardNumber)

#------------------------------------------------------------------

def evalHand(self):

    """determine the value of the hand and return a tuple; the
    first value in the tuple is an integer corresponding to the
    hand value using the constants HIGH_CARD, TWO_OF_A_KIND, etc.;
    the remaining values in the tuple depend on the type of hand
    and are described below to break ties based on the face values
    of the cards

    for HIGH_CARD, it is five values: the face values sorted in
    descending order

    for TWO_OF_A_KIND, it is four values: the face value for the
    pair, followed by the face values of the other cards in
    descending order

    for TWO_PAIRS, it is three values: the face value of the
    higher pair, the face value of the lower pair, followed by the
    face value of the other card

    for THREE_OF_A_KIND, it is three values: the face value of the
    three of a kind, followed by the face value of the other two
    cards in descending order

    for STRAIGHT, it is one value: the face value of the lowest
    card in the straight

    for FLUSH, it is five values: the face values sorted in
    descending order

    for FULL_HOUSE, it is two values: the face value of the three
    of a kind, followed by the face value of the pair

    for FOUR_OF_A_KIND, it is two values: the face value that
    there are four of followed by the face value that there is one
    of

    for STRAIGHT_FLUSH, it is one value: the face value of the
    lowest card in the straight"""

    faces = [0,0,0,0,0,0,0,0,0,0,0,0,0]
    for value in self.cards:
        face = value % 13
        faces[face] += 1

    suits = [0,0,0,0]
    for value in self.cards:
        suit = value // 13
        suits[suit] += 1

    if faces.count(2) == 1 and faces.count(1) == 3:
        return self.TWO_OF_A_KIND
    elif faces.count(2) == 2 and faces.count(1) == 1:
        return self.TWO_PAIRS
    elif faces.count(3) == 1 and faces.count(1) == 2:
        return self.THREE_OF_A_KIND
    elif faces.count(3) == 1 and faces.count(2) == 1:
        return self.FULL_HOUSE
    elif faces.count(4) == 1 and faces.count(1) == 1:
        return self.FOUR_OF_A_KIND
    elif faces.count(1) == 5:
        pos = faces.index(1)
        if faces[pos:pos+5] == [1,1,1,1,1] or faces[pos:pos+13] == [1,0,0,0,0,0,0,0,0,1,1,1,1]:
            return self.STRAIGHT
    if suits.count(5) == 1 and suits.count(1) == 0:
        return self.FLUSH
    if suits.count(5) == 1 and faces.count(1) == 5:
        pos = faces.index(1)
        if faces[pos:pos + 5] == [1, 1, 1, 1, 1] or faces[pos:pos + 13] == [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1]:
            return self.STRAIGHT_FLUSH
    return self.STRAIGHT_FLUSH

#----------------------------------------------------------------------

def main():
   hand = PokerHand()
   hand.addCard(9)
   hand.addCard(10)
   hand.addCard(8)
   hand.addCard(11)
   hand.addCard(7)

   r = hand.evalHand()
   print(r)
   print(PokerHand.HAND_NAMES[r])


if __name__ == '__main__':
  main()

【问题讨论】:

  • 您能澄清一下“将这个 PokerHand 类实现到另一个 def 主窗口中”的意思吗?
  • 与其用数字来表示A234..JQKA234...,我会用AAAA22223333...这样,你消除了除以13(现在它只是一个移位),而且更容易在检查每种手型之前按等级对手牌进行排序。

标签: python poker


【解决方案1】:

考虑将你的手条件放在函数中

这将有助于您的代码可读性并使其更具可测试性。 例如

def is_flush(suits):
    return suits.count(5) == 1

确保低价值的牌不会短路高价值的牌

如果你重新排列你的手条件的顺序,首先放置更高价值的测试,你将首先捕捉到更具体的情况,你不会意外地过早返回。

您可以将手视为两种非排他性类型:基于西装和基于脸。花色条件是“同花顺”或“非同花顺”,因此预先标记它可能会很好读。 然后你有一个面部组合,其中一些与flush_flag“相交”以增加它们的值。

或者...您可以按照自己的方式保持手牌测试的顺序,这可能会更优化,因为您更早地测试了更频繁的手牌。如果你这样做,请确保你的条件是“完整的”。 IE。如果这手牌是同类二,您还必须确保它不是两对或任何其他包含同类二的牌。您正在使用 faces.count(2) == 1 和 faces.count(1) == 3 执行此操作。对于 STRAIGHT 和 FLUSH,您需要遵循相同的“完整性”。

【讨论】:

  • 是的,总是从上到下测试手牌类型:首先是同花顺,然后是四边形,然后是葫芦,等等,在所有这些都失败之后,你只有高牌。在进行任何测试之前,按等级对手牌进行排序,并检查所有花色是否相等。这将使后面的测试更容易。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-30
  • 1970-01-01
  • 1970-01-01
  • 2015-04-11
  • 2021-10-24
  • 1970-01-01
相关资源
最近更新 更多