繁体   English   中英

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

[英]Python question about functions classes and class methods

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

例如,这是一个简单的 function,它向每个玩家发 5 张牌(来自预定义的列表),然后从牌组顶部翻出一张牌(实际上都是随机选择)。 卡片作为项目列表(3 个列表)返回。 我还制作了一个名为“播放器”的 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

在这个例子中,它只是一个交易,所以我想知道为什么它需要是“播放器”的 class 方法? 可能 class “玩家”除了保持得分、跟踪谁在发牌以及得分是多少外,根本没有做太多事情?

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

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

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

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

我最初认为这太宽泛了,但是当我为你写笔记时改变了主意。 类是一种编程工具,其实现在您所要求的级别上没有得到太多处理。 互联网上有很多好的纸牌游戏课程的例子……也有很多不好的。 研究对你来说并不容易。

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

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

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

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 中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM