python 斗地主最优解法,python模拟斗地主发牌

大家好,本文将围绕python 斗地主最优解法展开说明,python模拟斗地主发牌是一个很多人都想弄明白的事情,想搞清楚python斗地主游戏源码需要先了解以下几个事情。

 

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()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
21点游戏是一种非常流行的纸牌游戏,也被称为Blackjack。以下是一个简单的Python实现,其核心算是通过计算每个玩家手中的牌的点数来决定胜利者。 ```python import random # 初始化一副牌 suits = ['Spades', 'Hearts', 'Diamonds', 'Clubs'] ranks = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'] values = {'Ace': 11, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, 'Jack': 10, 'Queen': 10, 'King': 10} deck = [(s,r) for s in suits for r in ranks] # 洗牌 random.shuffle(deck) # 玩家手中的牌 player_cards = [] player_points = 0 # 庄家手中的牌 dealer_cards = [] dealer_points = 0 # 发牌 player_cards.append(deck.pop()) dealer_cards.append(deck.pop()) player_cards.append(deck.pop()) dealer_cards.append(deck.pop()) # 计算点数 for card in player_cards: rank = card[1] player_points += values[rank] for card in dealer_cards: rank = card[1] dealer_points += values[rank] # 玩家操作 while True: print("Player's cards: ", player_cards) print("Player's points: ", player_points) if player_points > 21: print("Player busts!") break action = input("Hit or Stand? ") if action == "Hit": player_cards.append(deck.pop()) rank = player_cards[-1][1] player_points += values[rank] else: break # 庄家操作 while dealer_points < 17: dealer_cards.append(deck.pop()) rank = dealer_cards[-1][1] dealer_points += values[rank] # 判断胜负 if dealer_points > 21: print("Dealer busts! Player wins!") elif dealer_points > player_points: print("Dealer wins!") elif dealer_points < player_points: print("Player wins!") else: print("Tie!") ``` 这个实现并不是最优解,因为它没有考虑到更高级的策略,例如在什么情况下应该拿牌或停牌。但是,这个实现可以作为一个简单的起点,可以帮助你了解21点游戏的基本规则和算

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值