【问题标题】:Checking for specific cases when turning over 52 cards in shuffled order52张洗牌翻牌时的具体情况检查
【发布时间】:2019-10-02 22:16:01
【问题描述】:

一副牌中有 52 张牌我试图让它通过每张随机的牌,所以一副 52 张牌的洗牌套牌,然后在第一种情况下,一旦它发现它会找到 2 个 A循环并打印这样做需要多少张卡片。 然后是另一个检查下一个是否有 5 个黑桃的功能来做同样的事情 然后是 13 颗心的另一个功能

所以这将是 3 个单独的函数,其中第一个函数将是 def 2aces() 检查 2 个 ace def 5spades 检查 5 黑桃 def 13hearts 检查 13 颗红心

所以通过更改 len(set(card.suit for card in hand)) == 1) to equal the conditions of either 2 aces5 spades13 hearts 只需将其制成 3 个单独的功能。

这里的问题是我需要在洗好的一副 52 张牌的地方做。因此,我会以使用 for 循环并检查语句为例,遍历整个卡片组。但我不知道如何处理西装和卡片。

至于 13 颗心的条件,这就是为什么我想洗一副 52 张牌然后使用 for 语句检查列表中的每个元素,如果是一颗心,则将其拉出,直到所有 13 颗心都被拉出或翻了个身。然后,一旦你找到 13 张红心牌,即所有红心 A 到王牌,它就会打破并说明达到该点需要多少张牌。

2 aces 
5 spades
13 hearts 

那么不管你用什么面向对象,分类西装然后编号呢? 我想看看解决这个问题的最佳方法是什么

一旦我能做到这一点,我只需要打印预期的卡片数量即可翻转以获得这些选项之一。

我有一个程序可以打印出它是否是使用面向对象的刷新,但我不确定如何对其进行调整以使其适用于其他特定选项。我可以编辑它把它放在这里看看我是否可以修改它。

这是我用来判断牌是否同花的示例

from collections import namedtuple
from random import shuffle

Card = namedtuple("Card", "suit, rank")

class Deck:
    suits = '♦♥♠♣'
    ranks = '23456789JQKA'

    def __init__(self):
        self.cards = [Card(suit, rank) for suit in self.suits for rank in self.ranks]
        shuffle(self.cards)

    def deal(self, amount):
        return tuple(self.cards.pop() for _ in range(amount))
flush = False
count = 0
while not flush:

    deck = Deck()
    while len(deck.cards) > 52:
        hand = deck.deal(52)
        # (Card(suit='♣', rank='7'), Card(suit='♠', rank='2'), Card(suit='♥', rank='4'), Card(suit='♥', rank='K'), Card(suit='♣', rank='3'))

        if len(set(card.suit for card in hand)) == 1:
            print(f"Yay, it's a Flush: {hand}")
            flush = True
            break
        else:
            #print(f"No Flush: {hand}")
            count +=1
print(f'Count is {count}')

但这是为了抽到 5 张牌。我想让它遍历 52 张经过洗牌的牌,然后检查每张牌,如果找到 2 张 A,它将循环显示从洗好的牌组中获得 2 张 A 需要多少张牌。上述其他选项也一样。

【问题讨论】:

    标签: python


    【解决方案1】:

    这些应该可以解决问题:

    def _2_aces():
        deck, counter, draws = Deck(), 0, 0
        while counter < 2:
            draw = deck.deal(1)[0]
            draws += 1
            if draw.rank == 'A':
                counter += 1
        return draws
    
    def _5_spades():
        deck, counter, draws = Deck(), 0, 0
        while counter < 5:
            draw = deck.deal(1)[0]
            draws += 1
            if draw.suit == '♠':
                counter += 1
        return draws
    
    def _13_hearts():
        deck, counter, draws = Deck(), 0, 0
        while counter < 13:
            draw = deck.deal(1)[0]
            draws += 1
            if draw.suit == '♥':
                counter += 1
        return draws
    

    顺便说一句,10 / T 是不是有意不在你的行列中?

    【讨论】:

    • 10/t是什么意思
    • 我在你的代码中添加了交易方法并删除了刷新部分。然后这个错误代码来了return tuple(self.cards.pop() for _ in range(amount)) IndexError: pop from empty list
    • @l.m.m 您在Deck 类中定义的排名不包括十位。它从 9 点跳到 Jack。
    • 所以我假设的返回方法有问题
    • 哇哦!!!成功了,这实际上是你们俩说的。我在 10 中添加,突然错误消失了!!!
    猜你喜欢
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 2017-05-16
    • 1970-01-01
    相关资源
    最近更新 更多