Python扑克程序的顺序列表

0 投票
2 回答
651 浏览
提问于 2025-04-18 01:46

我想为一个德州扑克程序写一个判断顺子的函数。我已经创建了一些测试值,希望这个函数能返回符合顺子条件的牌。

这是我目前的代码:

import cards

c1=cards.Card(1,1)
c2=cards.Card(2,1)
c3=cards.Card(3,2)
c4=cards.Card(4,2)
c5=cards.Card(5,2)
c6=cards.Card(6,4)
c7=cards.Card(3,4)
c8=cards.Card(7,3)
H1=[c7,c3,c2,c6,c5,c4,c1]
H2=[c1,c2,c3,c2,c3,c3,c8]


def build_rank_D(H):
    dict1={}
    for item in H:
        A=item.get_rank()
        if A not in dict1:
            dict1[A]=[item]


        else:
            dict1[A].append(item)





 return dict1

def straight(H):
    sequence=set()
    for item in H:
        A=item.get_rank()
        sequence.add(A)

    list_seq=list(sequence)
    n=list_seq[0]
    new_list=[]
    if list_seq[1]==n+1 and list_seq[2]==n+2 and list_seq[3]==n+3 and list_seq[4]==n+4
        print("you have a straight")
        return H

    else:
        print("no straight found")
    return []


print(straight(H1))

straight(H2)

现在这个函数打印的是所有的牌,而不是我想要的符合顺子条件的牌。

这是我导入的牌类程序的一个示例:

import random    # required for shuffle method of Deck

class Card(object):
    ''' Suit and rank are ints, and index into suit_list and rank_list.
        Value is different from rank: for example face cards are equal in value (all 10)
    '''
    # Use these lists to map the ints of suit and rank to nice words.
    # The 'x' is a place holder so that index-2 maps to '2', etc.
    suit_list = ['x','c','d','h','s']
    rank_list = ['x', 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10','J', 'Q', 'K']

    def __init__(self, rank=0, suit=0):
        ''' Rank and suit must be ints. This checks that they are in the correct range.
            Blank card has rank and suit set to 0.
        '''
        if type(suit) == int and type(rank) == int:
            # only good indicies work
            if suit in range(1,5) and rank in range(1,15):
                self.__suit = suit
                self.__rank = rank

            else:
                self.__suit = 0
                self.__rank = 0
        else:
            self.__suit = 0
            self.__rank = 0
    def get_rank(self):
        return self.__rank

    def get_suit(self):
        return self.__suit

2 个回答

0

正如@LieRyan所说,首先你得确保你的牌是有序的,然后才能继续找顺子。

为此,你可以在你的Card类里添加一个__lt__(self, other)方法,这样就能让一堆牌按照顺序排列。在这个方法里,你可以根据牌的等级来排序:

def __lt__(self, other):
    if self.__rank < other.__rank:
        return True
    else:
        return False

接下来,为了达到你想要的输出,我建议你添加一个__repr__(self)方法,这样可以控制你的牌是如何打印出来的:

def __repr__(self):
    return str(self.rank_list[self.__rank]) + str(self.suit_list[self.__suit]) 

最后,在你的stright(H)方法里,你只需要对牌进行排序,获取一个包含唯一等级的列表(我用一个列表mycards来控制哪些牌是我认为的唯一牌),检查你的顺子,然后打印出mycards里的前五张牌:

def straight(H):
    H.sort()
    list_seq=[]
    mycards = []
    for item in H:
        A=item.get_rank()
        if A not in list_seq:
            list_seq.append(A)
            mycards.append(item)

    n=list_seq[0]
    new_list=[]
    if list_seq[1]==n+1 and list_seq[2]==n+2 and list_seq[3]==n+3 and list_seq[4]==n+4:
        print("you have a straight")
        return mycards[0:5]

    else:
        print("no straight found")
        return []

对于straight(H1),在我的例子中,我得到了:

you have a straight
[Ac, 2c, 3s, 4d, 5d]

如果你想打印整个H,只需省略列表mycards,直接打印H(它已经是有序的了)

0

这个代码的作用是:

sequence = set()
...
list_seq=list(sequence)

生成一个随机顺序的整数列表(集合是无序的),在进行比较之前,你可能需要先对这个列表进行排序。

撰写回答