# Python实现斗地主

#---------------第一种方法：初始化所有牌并随机分配法---------------------
import random
cardList = [('红桃','A'),('红桃','2'),('红桃','3'),('红桃','4'),('红桃','5'),('红桃','6'),('红桃','7'),
('红桃','8'),('红桃','9'),('红桃','10'),('红桃','J'),('红桃','Q'),('红桃','K'),
('方块','A'),('方块','2'),('方块','3'),('方块','4'),('方块','5'),('方块','6'),('方块','7'),
('方块','8'),('方块','9'),('方块','10'),('方块','J'),('方块','Q'),('方块','K'),
('黑桃','A'),('黑桃','2'),('黑桃','3'),('黑桃','4'),('黑桃','5'),('黑桃','6'),('黑桃','7'),
('黑桃','8'),('黑桃','9'),('黑桃','10'),('黑桃','J'),('黑桃','Q'),('黑桃','K'),
('梅花','A'),('梅花','2'),('梅花','3'),('梅花','4'),('梅花','5'),('梅花','6'),('梅花','7'),
('梅花','8'),('梅花','9'),('梅花','10'),('梅花','J'),('梅花','Q'),('梅花','K'),('大王','RedJoker'),('小王','BlackJoker')]
cardRand = random.sample(cardList,54)

HandCard = cardRand[0:3]  #3张底牌
player1 = cardRand[3:20]
player2 = cardRand[20:37]
player3 = cardRand[37:54]

print('底牌：')
print(HandCard)
print('玩家一的牌：')
print(player1)
print('玩家二的牌：')
print(player2)
print('玩家三的牌：')
print(player3)
底牌：
[('方块', 'K'), ('黑桃', '3'), ('黑桃', '8')]

[('梅花', '3'), ('黑桃', 'Q'), ('红桃', '10'), ('红桃', 'A'), ('方块', '10'), ('黑桃', '4'), ('大王', 'RedJoker'), ('方块', '9'), ('黑桃', '10'), ('方块', '3'), ('黑桃', '2'), ('梅花', '6'), ('红桃', 'J'), ('红桃', '7'), ('黑桃', '5'), ('方块', '7'), ('黑桃', 'J')]

[('红桃', '8'), ('梅花', 'Q'), ('梅花', 'J'), ('红桃', '6'), ('方块', 'Q'), ('方块', '8'), ('黑桃', '6'), ('方块', '5'), ('梅花', '9'), ('红桃', '9'), ('红桃', 'Q'), ('红桃', '4'), ('梅花', '7'), ('梅花', 'A'), ('梅花', '4'), ('黑桃', '9'), ('梅花', 'K')]

[('红桃', 'K'), ('黑桃', '7'), ('红桃', '5'), ('方块', '4'), ('方块', 'A'), ('红桃', '2'), ('小王', 'BlackJoker'), ('方块', '6'), ('黑桃', 'A'), ('梅花', '5'), ('梅花', '10'), ('红桃', '3'), ('梅花', '2'), ('方块', '2'), ('梅花', '8'), ('黑桃', 'K'), ('方块', 'J')]
# #---------------第二种方法：类方法和装饰器---------------------
import random
class Poke():
poke = []  # 扑克牌牌堆
p1 = []   # 玩家一牌堆
p2 = []   #  玩家二牌堆
p3 = []    #  玩家三牌堆
last = None   # 底牌牌堆
def __init__(self,f,num):      # 初始化牌堆
self.flower = f     # 花色
self.num = num    #  点数
def __str__(self):
return "%s%s" % (self.flower,self.num)     # 返回牌值
@classmethod
def init(cls):   # 定义牌堆
ph = ("♥","♠","♣","♦")                    # 花色元组
pnum = ("2","3","4","5","6","7","8","9","10","J","Q","K","A")  # 点数元组
king = {"big":"大王","small":"小王"}        # 大小王
for p in ph:                   # 循环遍历花色
for _nump in pnum:    #  循环遍历点数
cls.poke.append(Poke(p,_nump))  # 装牌
cls.poke.append(Poke(king["big"],""))   # 装大王
cls.poke.append(Poke(king["small"],""))  # 装小王
@classmethod
def wash(cls):     # 洗牌
random.shuffle(cls.poke)
@classmethod
def send(cls):    #  发牌
for _ in range(0,17): # 三个人每人发17张牌,_表示一个随机参数和i一样，实际用不到，循环17次
cls.p1.append(cls.poke.pop(0))   # 玩家一发牌，牌堆抛出第1个元素附加给p1
cls.p2.append(cls.poke.pop(0))  #剩下的牌堆抛出第1个元素附加给p2
cls.p3.append(cls.poke.pop(0)) #剩下的牌堆抛出第1个元素附加给p3
cls.last= tuple(cls.poke)            # 最后三张牌做底牌
@classmethod
def show(cls):    # 展示牌
print("玩家1:")
for pokes in cls.p1:
print(pokes,end = " ")
print()
print("玩家2:")
for pokes in cls.p2:
print(pokes, end=" ")
print()
print("玩家3:")
for pokes in cls.p3:
print(pokes, end=" ")
print()
print("底牌：")
for pokes in cls.last:
print(pokes, end=" ")
print()

Poke.init()
Poke.wash()
Poke.send()
Poke.show()

'''

'''
玩家1:
♠9 ♦8 小王 ♥A ♠A ♣7 ♣J ♠4 ♣9 ♦Q ♥2 ♥J ♣A ♣10 ♦2 ♠J ♠10

♦9 ♦4 大王 ♣5 ♦7 ♠2 ♥7 ♦K ♣6 ♥4 ♦J ♥5 ♣3 ♥Q ♠3 ♠8 ♣Q

♠6 ♣4 ♥10 ♦10 ♣2 ♣K ♥8 ♦3 ♣8 ♦5 ♥3 ♠K ♦6 ♠7 ♥9 ♠Q ♥6

♥K ♠5 ♦A 
• 点赞 2
• 评论 2
• 分享
x

海报分享

扫一扫，分享海报

• 收藏 11
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

南风有翼

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文

12-05 931
07-11 1万+
09-18
02-01
09-11
08-11