Python 关于函数 classes 和 class 方法的问题

Python question about functions classes and class methods

我对中级编程还很陌生,但已经玩过一段时间的代码。 目前我正在制作一个简单的纸牌游戏。 这个问题是我不确定在哪里使用我的功能或何时制作它们 class 方法。

例如,这是一个简单的函数,它向每位玩家分发 5 张牌(从预定义的列表中),然后从牌组顶部翻开一张牌(实际上都是随机选择)。 卡片作为项目列表(3 个列表)返回。 我还制作了一个名为“Player”的 class。

p1_hand = []
p2_hand = []
flip_card = []

def deal_deck():
    count = 0
    for i in range(0, 10, 1):
        count += 1
        if count % 2 == 0:
            card = random.choice(deck)
            p2_hand.append(card)
            deck.remove(card)
        else:
            card = random.choice(deck)
            p1_hand.append(card)
            deck.remove(card)
        if count == 10:
            flip_card.append(random.choice(deck))
    return p2_hand, p1_hand, flip_card

在这个例子中,它只是一个交易,所以我想知道为什么它需要是“Player”的class方法? 可能 class “玩家”除了记分,记录谁在发牌,记分是多少,什么都不做?

简而言之,我无法将 class 理解为交互和执行操作的对象,我制作了其他 classes,它们最终像迷你数据库一样工作,而不是比在方法中使用更多的复杂性。

设计 classes 和对象是一门艺术。使用 classes 的根本目的是隐藏信息。程序的其余部分不必知道一副纸牌是如何实现的,这允许您更改实现而无需重新设计整个程序。因此,您创建了一个 Deck class ,其中所有数据都存储在内部,并且只向外界公开您想要使用套牌做的事情,例如 shuffledeal_card。一个 Player class 可能包含一个 hand 和一个 score,并具有添加另一张牌的功能,并且 Game 对象(可能)可以将发牌协调到手和触发戏剧。

您的代码混合了所有这些。它必须知道一副牌是如何实现的,一手牌是如何实现的,以及一张牌是如何翻转的。

顺便说一句,为了真实起见,您最好洗牌并从顶部发牌,而不是使用 random.choice

我最初认为这太笼统了,但在我为您写下笔记时改变了主意。 类 是一种编程工具,其实现在您要求的级别上没有得到太多处理。 Internet 上有许多 class 好的纸牌游戏示例……还有许多 糟糕的 。研究对你来说并不容易。

使用 class 来表示系统的一个单元(在本例中为纸牌游戏),该单元具有内聚性(一组具有容易理解的边界的数据和功能)并与其他单元交互,或与主程序一起使用。

在这种情况下,您有一个良好的开端:您已将 cardplayerhand 识别为游戏系统中的实体。您可能希望将牌组视为 hand 实例(只是另一张纸牌列表),或者您可能希望根据游戏中的不同功能对其进行特殊处理。

我认为有用的 classes 和函数包括:

Deck
    The impartial source of cards
data
    a list of cards
methods
    reset -- shuffle all 52 cards into a list
    deal(n) -- return a list of n cards

Hand
    cards held by a single player
data
    a list of cards
methods
    reset -- whatever is needed to return the hand to a game-start state
    draw(n) -- acquire n cards
    play(n) -- play n cards to the game area
    
Card
    A single card, containing all information needed to identify it to the game
data
    suit
    rank
methods
    none

Player
    Game information about each player
data
    hand -- see Hand class above
    game-role -- depending on the game, this could be "dealer", "idle", "active", ...
    ... other, depending on the game: points, money, etc.
methods
    ... very dependent on the game being played

Game
    the overall monitor for the game
data
    roster -- list of players
    deck -- see Deck class above
    ... other, depending on the game: round, player to play next, etc.

其中有些重叠,例如“Deck.deal”和“Hand.draw”。 您面临的设计决策之一是选择哪个实体将驱动两个对象之间的交互。

至于实现,我建议你把你的基本操作简化很多。 对于桌子,通过使用嵌套列表理解生成所有 52 张卡片来初始化。 shuffle 套牌,然后使用列表切片来发牌。例如,给每位玩家发 5 张牌:

def deal(n):
    take = deck[:n]
    deck = deck[n:]
    return take

# elsewhere, to drive the initial dealing ...
for player in roster:
    player.hand = deck.deal(5)

只需减少互动以匹配您谈论游戏的方式:“给每个玩家发五张牌”在代码中应该看起来像这样。将机制隐藏在每个 class.