我想为德州扑克计划建立一个直接的功能。我已经创建了一些测试值,并希望函数返回满足直线的卡片列表。
这是我到目前为止所做的:
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
答案 0 :(得分:0)
这是做什么的:
sequence = set()
...
list_seq=list(sequence)
以随机顺序生成一个整数列表(set是无序的),你需要先进行排序,然后再进行比较。
答案 1 :(得分: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
的5张第一张牌:
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
(已订购)