- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我迄今为止的扑克游戏代码,我得到了 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()
最佳答案
这将提高代码的可读性并使其更易于测试。例如
def is_flush(suits):
return suits.count(5) == 1
如果您重新排列手牌条件的顺序,首先放置更高值的测试,您将首先捕获更具体的情况,并且您不会意外地过早返回。
您可以将手视为两种非专有类型:基于套装和基于面。套装条件是“同花”或“非同花”,因此提前标记这一点可能会很好读。然后你就有了一个面部组合,其中一些与flush_flag“相交”以增加它们的值。
或者...您可以按照现有的方式保持您的牌局测试顺序,这可能会更理想,因为您正在更早地测试更频繁的牌局。如果您这样做,请确保您的条件是“完整的”。 IE。如果这手牌是二同牌,您还必须确保它不是两对或任何其他包含二同牌的牌。您正在使用 faces.count(2) == 1 和 faces.count(1) == 3 执行此操作。您需要遵循 STRAIGHT 和 FLUSH 相同的“完整性”。
关于python - python 扑克中的同花顺和皇家同花顺,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59025431/
一副扑克牌是 52 张牌 13阶4花色 致力于高效的手部表示和评估 A K Q J T 9 8 7 6 5 4 3 2 scdh
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我遇到了一个真正令人困惑的错误,在过去的几个小时里我一直试图解决这个错误,但没有成功。我正在研究扑克实现。最初,我通过迭代循环生成卡片。 const suits = ['Heart', 'Spade'
我是一名优秀的程序员,十分优秀!