随机斗地主
病情未结束,生活还要继续 在家里无聊的带着。生命苦短,我用python。
无聊的假期,想玩斗地主,可是在网上的那些,要么就是用豆豆,要么就是用钱,或者就是没人组房间一起玩 所以先暂时琢磨自己写一个简单的随机分配发牌的小python 只是初入阶段所以写的较小。
按照逻辑思维打斗地主游戏第一件事就是登录,可是这是我们自己写的还用登录吗,既然不用干嘛写登录。那就首先写一个class类把 把他们都写到一个类里面。
class Poke():
print('欢迎来到洋洋斗地主\n'
'规则一:被分配为地主时,请确定是否想要地主\n'
'规则二:地主先出牌,顺序是地主的下一位\n'
'规则三:没有分数,炸弹不翻倍,打完先赢')
poke = [] # 扑克牌牌堆
p1 = [] # 玩家一牌堆
p2 = [] # 玩家二牌堆
p3 = [] # 玩家三牌堆
last = None # 底牌牌堆
随机分配的话还得用到我上次写的博客猜拳游戏的一个函数 random函数所以我们要导入进去。
导入random函数
import random
class Poke():
print('欢迎来到洋洋斗地主\n'
'规则一:被分配为地主时,请确定是否想要地主\n'
'规则二:地主先出牌,顺序是地主的下一位\n'
'规则三:没有分数,炸弹不翻倍,打完先赢')
poke = [] # 扑克牌牌堆
p1 = [] # 玩家一牌堆
p2 = [] # 玩家二牌堆
p3 = [] # 玩家三牌堆
last = None # 底牌牌堆
接下来用函数分批写他的 poke牌,定义牌的花色,洗牌,发牌,展示牌
定义牌的花色和点数
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"], "")) # 装小王
这样一副牌就出来了 2-A 还有大王:big 小王:somall
牌已经做好了 接下来就是洗牌了 不洗牌就没法发牌
@classmethod
def wash(cls): # 洗牌
random.shuffle(cls.poke)
洗牌就这两行代码 利用到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) # 最后三张牌做底牌
发完了牌 就该自己看自己牌了,但是还有一件事就是地主牌,谁抽到地主谁领取三张牌,并且放到自己拍中
首先来看自己的牌和底牌
print('玩家一')
for pokes in cls.p1:
print(pokes, end=" ")
print()
# 玩家二
print('玩家二')
for pokes in cls.p2:
print(pokes, end=" ")
print()
# 玩家三
print('玩家三')
for pokes in cls.p3:
print(pokes, end=" ")
print()
# 底牌
print('底牌:')
for pokes in cls.last:
print(pokes, end=" ")
print()
各位玩家看到自己的牌后开始判断谁是地主,并且把底牌分发给地主
判断地主 而且 确定是否要地主
if cls.poke == 1:
cls.p1.insert(0, '地主:')
cls.p2.insert(0, '农民:')
cls.p3.insert(0, '农民:')
a = input('请输入是否要地主,yes或no:')
if a == 'yes':
for d in cls.last:
cls.p1.append(d)
# 玩家一+底牌
for pokes in cls.p1:
print(pokes, end=" ")
print()
# 玩家二
for pokes in cls.p2:
print(pokes, end=" ")
print()
# 玩家三
for pokes in cls.p3:
print(pokes, end=" ")
print()
else:
exit()
elif cls.poke == 2:
cls.p1.insert(0, '农民:')
cls.p2.insert(0, '地主:')
cls.p3.insert(0, '农民:')
# 玩家一
for pokes in cls.p1:
print(pokes, end=" ")
print()
# 玩家二
for pokes in cls.p2:
print(pokes, end=" ")
print()
# 玩家三
for pokes in cls.p3:
print(pokes, end=" ")
print()
# 底牌
print('底牌:')
for pokes in cls.last:
print(pokes, end=" ")
print()
a = input('请输入是否要地主,yes或no:')
if a == 'yes':
for d in cls.last:
cls.p2.append(d)
# 玩家一
for pokes in cls.p1:
print(pokes, end=" ")
print()
# 玩家二+底牌
for pokes in cls.p2:
print(pokes, end=" ")
print()
# 玩家三
for pokes in cls.p3:
print(pokes, end=" ")
print()
else:
exit()
elif cls.poke == 3:
cls.p1.insert(0, '农民:')
cls.p2.insert(0, '农民:')
cls.p3.insert(0, '地主:')
# 玩家一
for pokes in cls.p1:
print(pokes, end=" ")
print()
# 玩家二
for pokes in cls.p2:
print(pokes, end=" ")
print()
# 玩家三
for pokes in cls.p3:
print(pokes, end=" ")
print()
# 底牌
print('底牌:')
for pokes in cls.last:
print(pokes, end=" ")
print()
a = input('请输入是否要地主,yes或no:')
if a == 'yes':
for d in cls.last:
cls.p3.append(d)
# 玩家一
for pokes in cls.p1:
print(pokes, end=" ")
print()
# 玩家二
for pokes in cls.p2:
print(pokes, end=" ")
print()
# 玩家三+底牌
for pokes in cls.p3:
print(pokes, end=" ")
print()
else:
exit()
运行结果:
欢迎来到洋洋斗地主
规则一:被分配为地主时,请确定是否想要地主
规则二:地主先出牌,顺序是地主的下一位
规则三:没有分数,炸弹不翻倍,打完先赢
玩家一
♦10 ♠9 ♠3 ♣7 ♠J ♥Q ♥A ♣Q ♦9 ♣3 小王 ♥7 ♣2 ♦Q ♦3 ♥4 ♣9
玩家二
♠5 ♠10 ♦7 ♠8 ♥5 ♣6 ♥6 ♥2 ♠K ♠7 ♣5 ♣4 ♣J ♠4 ♦8 ♠6 ♠2
玩家三
♥9 ♥10 ♥J ♦J ♣8 ♥8 大王 ♦K ♦5 ♦4 ♥K ♣10 ♣A ♦6 ♠Q ♦A ♥3
底牌:
♣K ♠A ♦2
请输入是否要地主,yes或no:yes
地主: ♦10 ♠9 ♠3 ♣7 ♠J ♥Q ♥A ♣Q ♦9 ♣3 小王 ♥7 ♣2 ♦Q ♦3 ♥4 ♣9 ♣K ♠A ♦2
农民: ♠5 ♠10 ♦7 ♠8 ♥5 ♣6 ♥6 ♥2 ♠K ♠7 ♣5 ♣4 ♣J ♠4 ♦8 ♠6 ♠2
农民: ♥9 ♥10 ♥J ♦J ♣8 ♥8 大王 ♦K ♦5 ♦4 ♥K ♣10 ♣A ♦6 ♠Q ♦A ♥3
这只是初入写斗地主 当然肯定没写完 只是先写了打牌前的准备
用def 把展示牌和判断地主牌放在一起完整代码
@classmethod
def show(cls): # 展示牌
cls.poke = random.randint(1, 3)
# 玩家一
print('玩家一')
for pokes in cls.p1:
print(pokes, end=" ")
print()
# 玩家二
print('玩家二')
for pokes in cls.p2:
print(pokes, end=" ")
print()
# 玩家三
print('玩家三')
for pokes in cls.p3:
print(pokes, end=" ")
print()
# 底牌
print('底牌:')
for pokes in cls.last:
print(pokes, end=" ")
print()
if cls.poke == 1:
cls.p1.insert(0, '地主:')
cls.p2.insert(0, '农民:')
cls.p3.insert(0, '农民:')
a = input('请输入是否要地主,yes或no:')
if a == 'yes':
for d in cls.last:
cls.p1.append(d)
# 玩家一+底牌
for pokes in cls.p1:
print(pokes, end=" ")
print()
# 玩家二
for pokes in cls.p2:
print(pokes, end=" ")
print()
# 玩家三
for pokes in cls.p3:
print(pokes, end=" ")
print()
else:
exit()
elif cls.poke == 2:
cls.p1.insert(0, '农民:')
cls.p2.insert(0, '地主:')
cls.p3.insert(0, '农民:')
# 玩家一
for pokes in cls.p1:
print(pokes, end=" ")
print()
# 玩家二
for pokes in cls.p2:
print(pokes, end=" ")
print()
# 玩家三
for pokes in cls.p3:
print(pokes, end=" ")
print()
# 底牌
print('底牌:')
for pokes in cls.last:
print(pokes, end=" ")
print()
a = input('请输入是否要地主,yes或no:')
if a == 'yes':
for d in cls.last:
cls.p2.append(d)
# 玩家一
for pokes in cls.p1:
print(pokes, end=" ")
print()
# 玩家二+底牌
for pokes in cls.p2:
print(pokes, end=" ")
print()
# 玩家三
for pokes in cls.p3:
print(pokes, end=" ")
print()
else:
exit()
elif cls.poke == 3:
cls.p1.insert(0, '农民:')
cls.p2.insert(0, '农民:')
cls.p3.insert(0, '地主:')
# 玩家一
for pokes in cls.p1:
print(pokes, end=" ")
print()
# 玩家二
for pokes in cls.p2:
print(pokes, end=" ")
print()
# 玩家三
for pokes in cls.p3:
print(pokes, end=" ")
print()
# 底牌
print('底牌:')
for pokes in cls.last:
print(pokes, end=" ")
print()
a = input('请输入是否要地主,yes或no:')
if a == 'yes':
for d in cls.last:
cls.p3.append(d)
# 玩家一
for pokes in cls.p1:
print(pokes, end=" ")
print()
# 玩家二
for pokes in cls.p2:
print(pokes, end=" ")
print()
# 玩家三+底牌
for pokes in cls.p3:
print(pokes, end=" ")
print()
else:
exit()
最后别忘了运行调用函数
Poke.init() Poke.wash() Poke.send() Poke.show()