用python语言实现斗地主基础版-案例

Talk is cheap, show me code!(屁话少说,放码过来)

"""

案例斗地主分析:

1.扑克牌作为对象呈现

2.创建未发牌的牌堆的列表

3.创建三个玩家牌堆的列表

4.创建底牌的元组

5.最原始的牌堆初始化,将54张牌加入到牌堆

6.创建洗牌操作

7.创建发牌操作

"""

import random

class Poke:

pokes = []
player1 = []
player2 = []
player3 = []
last = []

def __init__(self, flower, num):
    self.flower = flower
    self.num = num

def __str__(self):
    return "%s%s" % (self.flower, self.num)

@classmethod
def init_pokes(cls):#初始化牌
    print("初始化牌堆:")
    flowers = ("♠", "♥", "♣", "♦")
    nums = ("2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A")
    kings = {"big": "大王", "small": "小王"}

    for flowers_ in flowers:
        for num_ in nums:
            cls.pokes.append(Poke(flowers_, num_))
    cls.pokes.append(Poke(kings["big"], ""))
    cls.pokes.append(Poke(kings["small"], ""))

@classmethod
def wash(cls):#洗牌
    print("洗牌: ")
    for idx in range(54):
        idxx = random.randint(0, 53)
        Poke.pokes[idx], Poke.pokes[idxx] = Poke.pokes[idxx], Poke.pokes[idx]

@classmethod
def send(cls):#发牌
    for _ in range(0, 17):
        Poke.player1.append(cls.pokes.pop(0))
        Poke.player2.append(cls.pokes.pop(0))
        Poke.player3.append(cls.pokes.pop(0))
    Poke.last = tuple(Poke.pokes)

@classmethod#展示牌堆
def show(cls):
    for poke in Poke.pokes:
        print(poke, end=" ")
    print()

@classmethod
def show_player(cls):#展示玩家手里的牌和底牌
    print("玩家1的牌: ", end="")
    for poke in Poke.player1:
        print(poke, end=" ")
    print()

    print("玩家2的牌: ", end="")
    for poke in Poke.player2:
        print(poke, end=" ")
    print()

    print("玩家3的牌: ", end="")
    for poke in Poke.player3:
        print(poke, end=" ")
    print()

    print("底牌: ", end="")
    for poke in Poke.last:
        print(poke, end=" ")
    print()

Poke.init_pokes()

Poke.show()

Poke.wash()

Poke.show()

Poke.wash()

Poke.show()

Poke.send()

Poke.show()

Poke.show_player()

你可能感兴趣的:(用python语言实现斗地主基础版-案例)