面向对象实例练习

面向对象实例练习

# -*- coding: utf-8 -*-
"""
@Time : 2024/1/18 15:42
@Auth : Of Course I Still Love You
@File :面向对象.py
@IDE :PyCharm
@note:Love is all the reasons and answers
"""
""" card 类"""


class Card:
    Rank = ['A', "2", "3", '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']  # 设置牌面数字1-13
    SUITs = ['梅花', '方块', '红桃', '黑桃']

    def __init__(self, rank, suit, face_up=True):
        self.rank = rank  # 指的是牌面数字1-13
        self.suit = suit  # 指的是花色
        self.is_face_up = face_up  # 是否显示牌的正面,True为正面False为正面

    def __str__(self):  # 重写print方法,打印一张牌的信息
        if self.is_face_up:
            rep = self.suit + self.rank
        else:
            rep = "xx"
        return rep

    def pic_order(self):  # 牌的顺序号
        if self.rank == 'A':
            Face_num = 1
        elif self.rank == 'J':
            Face_num = 11
        elif self.rank == 'Q':
            Face_num = 12
        elif self.rank == 'K':
            Face_num = 13
        else:
            Face_num = int(self.rank)
        if self.suit == '梅花':
            Suit = 1
        elif self.suit == "方块":
            Suit = 2
        elif self.suit == '红桃':
            Suit = 3
        else:
            Suit = 4
        return (Suit - 1) * 13 + Face_num

    def flip(self):  # 翻牌方法
        self.is_face_up = not self.is_face_up


class Hand:
    """代表一个玩家的手牌"""

    def __init__(self):
        self.cards = []  # card l列表变量存储牌手的牌

    def __str__(self):  # 重写 print( )方法,打印出牌手 的所有牌
        if self.cards:
            rep = ""
            for card in self.cards:
                rep += str(card) + "\t"
        else:
            rep = "无牌"
        return rep

    def clear(self):  # 清空手里的牌
        self.cards = []

    def add(self, card):  # 增加牌
        self.cards.append(card)

    def give(self, card, other_hand):  # 把一张牌给其它牌手
        self.cards.remove(card)
        other_hand.add(card)


"""Poke 类代表一 副牌 ,可以 看作是有 52 张牌的牌手 ,
所以继承Hand 类 。 
由千其中cards列表变量要存储52张牌 ,
而且要发牌、洗牌 ,
所以增加如下方法:"""


class Poke(Hand):  # 继承Hand类
    def populate(self):
        for suit in Card.SUITs:
            for rank in Card.Rank:
                self.add(Card(rank, suit))

    def shuffle(self):
        import random
        random.shuffle(self.cards)  # 打乱牌的顺序

    def deal(self, hands, per_hand=13):  # 发牌,发给玩家,每个人默认13张牌
        for rounds in range(per_hand):
            for hand in hands:
                if self.cards:
                    top_card = self.cards[0]
                    self.cards.remove(top_card)
                    hand.add(top_card)

                else:
                    print('不能进行发牌了,牌已经发完了')


"""主程序"""

if __name__ == "__main__":
    print('This is a module wi th classes for playing cards')
    # 生成4个玩家
    players = [Hand(), Hand(), Hand(), Hand()]
    pokel = Poke()
    pokel.populate()  # 生成一份牌
    pokel.shuffle()  # 洗牌
    pokel.deal(players, 13)  # 每人发牌13张

    # 显示4 位牌手的牌
    n = 1
    for hand in players:
        print("牌手", n, end=":")
        print(hand)
        n += 1
    input('\n 请按回车键退出')
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值