python写斗地主游戏_基于python的简单斗地主实现-Go语言中文社区
import random
class Poke:
pokes=[] #初始牌堆
player1=[]#三個玩家的牌
player2=[]
player3=[]
last_poke=None #底牌
def __init__(self,flower,num):
self.flower=flower
self.num=num
def __str__(self):
return "%s%s"%(self.flower,self.num)
#1.初始化牌
@classmethod
def init_poke(cls):
flowers=("?","?","?","?") #四種花色
nums=("2","3","4","5","6","7","8","9","10","J","Q","K","A") #14個數字牌
kings={"big":"大王","small":"小王"} #大小王
for flower_ in flowers:#四種花色每一種分別與14個數字進行組合
for num_ in nums:
p=Poke(flower_,num_) #產生撲克牌對象
cls.pokes.append(p) #將撲克牌對象添加到撲克牌列表中
cls.pokes.append(Poke(kings["big"],"")) #將大小王添加到撲克牌列表中
cls.pokes.append(Poke(kings["small"],""))
#2.洗牌
@classmethod
def wash_poke(cls):
for idx in range(0,54): #54張牌,列表的索引從0到53
idxx=random.randint(0,53)#調用隨機函數,隨機產生0~53的數字
cls.pokes[idx],cls.pokes[idxx]=cls.pokes[idxx],cls.pokes[idx] #進行交換牌
#3.發牌
@classmethod
def send_poke(cls):
for _ in range(0,17): #產生三個玩家的牌
cls.player1.append(cls.pokes.pop(0)) #每個玩家獲得17張牌
cls.player2.append(cls.pokes.pop(0))
cls.player3.append(cls.pokes.pop(0))
cls.last_poke=tuple(cls.pokes) #三張底牌
#4.展示牌
@classmethod
def show(cls,i,p):
print(p,end=": ")
for poke in i: #遍歷撲克牌
print(poke,end="")
print()
#展示玩家的牌
@classmethod
def show_player(cls): #調用show()方法展示三個玩家和底牌
cls.show(cls.player1,"玩家1")
cls.show(cls.player2,"玩家2")
cls.show(cls.player3,"玩家3")
cls.show(cls.last_poke,"底牌")
Poke.init_poke()
Poke.wash_poke()
Poke.send_poke()
Poke.show_player()
代碼運行結果:
總結
以上是生活随笔為你收集整理的python写斗地主游戏_基于python的简单斗地主实现-Go语言中文社区的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 动态规划 数塔_数塔问题,
- 下一篇: Andriod开发 --插件安装、环境配