1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| from enum import Enum,unique import random
@unique class Suite(Enum): """docstring for suite"""
SPADE = 0 HEART = 1 CLUB = 2 DIAMOND = 3
class Card(object): """docstring for ClassName""" def __init__(self, suite, face): self.suite =suite self.face = face
def show(self): '显示牌面' suites = ['黑桃', '红心', '梅花', '方块'] face = ['', 'A','2','3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q','k'] return f'{suites[self.suite.value]} {face[self.face]}' def __str__(self): return self.show()
def __repr__(self): return self.show()
class Poker(object): """扑克"""
def __init__(self): self.index = 0 self.cards = [Card(suite,face) for suite in Suite for face in range(1,14)]
def shuffle(self): '''洗牌(随机乱序)''' random.shuffle(self.cards) self.index = 0 def deal(self): '''发牌''' card = self.cards[self.index] self.index += 1 return card
@property def has_more(self): return self.index < len(self.cards)
class Player(object):
def __init__(self,name): self.name = name self.card = []
def get_one(self,card): '''摸一张牌''' self.card.append(card)
def sort(self, comp=lambda card: card.face): self.card.sort(key= comp)
def main(): poker = Poker() print(poker.cards) poker.shuffle() players= [Player('东邪'),Player('西毒'), Player('南帝'), Player('北丐')] while poker.has_more: for player in players: player.get_one(poker.deal()) for player in players: player.sort() print(player.name, end='') print(player.card)
if __name__ == '__main__': main()
|