Python中的同花顺和皇家同花顺

问题描述 投票:1回答:1

这里是我到目前为止用于扑克游戏的一些代码,我让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()
python poker
1个回答
0
投票

考虑将手条件放在函数中

这将帮助您提高代码的可读性,并使其更具可测试性。例如:

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

确保低价值的手不会使高价值的手短路

如果重新排列手条件的顺序,首先进行更高价值的测试,则将首先捕获更具体的情况,并且不会意外返回。

您可能认为手属于两种非排他性类型:基于西装和基于面部。套装条件是“齐平”或“不齐平”,因此将其提前标记可能很好读。然后,您将获得一个面部组合,其中一些与flush_flag“相交”以增加其值。

或者...您可以按照自己的方式安排手测的顺序,由于您早些时候要测试更频繁的手,因此它可能是最佳选择。如果这样做,请确保您的条件是“完整的”。即如果这只手是两种,则还必须确保它不是两对,也不能是其他任何两只手。您正在使用faces.count(2)== 1和faces.count(1)== 3进行此操作。对于STRAIGHT和FLUSH,您需要遵循相同的“完整性”。

© www.soinside.com 2019 - 2024. All rights reserved.