python面向对象基本知识二用类方法实现斗地主洗牌发牌

28次阅读

共计 966 个字符,预计需要花费 3 分钟才能阅读完成。

'''
    斗地主
    1. 牌堆,54 张牌,四种花色,2,3,4,5,6,7,8,9,10,J,Q,K,A,大小王
    2. 洗牌
    3. 发牌
'''
import random
class Poke():
    flowr=["黑桃","方片","梅花","红桃"]
    num=["2","3","4","5","6","7","8","9","10","J","Q","K","A"]
    kings={'big':'大王','small':'小王'}
    poker=[]
    player1=[]
    player2=[]
    player3=[]
    dipai=[]

    def __init__(self):
        for f in self.flowr:
            for n in self.num:
                self.poker.append(f+n+' ')
        self.poker.append(self.kings['big']+' ')
        self.poker.append(self.kings['small']+' ')

    #洗牌
    def wash(self):
        for i in range(54):
            index=random.randint(0,53)
            self.poker[index],self.poker[i]=self.poker[i],self.poker[index]

    #发牌
    def fa(self):
        for i in range(17):
            self.player1.append(self.poker.pop(0))
            self.player2.append(self.poker.pop(0))
            self.player3.append(self.poker.pop(0))
        self.dipai=tuple(self.poker)

    #展示
    def show(self):
        for i in self.player1:
            print(i,end='')
        print()
        print('玩家 1↑↑↑')
        for i in self.player2:
            print(i,end='')
        print()
        print('玩家 2↑↑↑')
        for i in self.player3:
            print(i,end='')
        print()
        print('玩家 3↑↑↑')
        for i in self.dipai:
            print(i,end='')
        print()
        print('底牌↑↑↑')

p=Poke()
p.wash()
p.fa()
p.show()

正文完
 0