61 lines
1.1 KiB
Python
61 lines
1.1 KiB
Python
"""
|
|
扑克
|
|
"""
|
|
import enum
|
|
import random
|
|
|
|
|
|
@enum.unique
|
|
class Suite(enum.Enum):
|
|
"""花色(枚举)"""
|
|
SPADE, HEART, CLUB, DIAMOND = range(4)
|
|
|
|
|
|
class Card:
|
|
"""牌"""
|
|
|
|
def __init__(self, suite, face):
|
|
self.suite = suite
|
|
self.face = face
|
|
|
|
def __repr__(self):
|
|
suites = '♠♥♣♦'
|
|
faces = ['', 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
|
|
return f'{suites[self.suite.value]}{faces[self.face]}'
|
|
|
|
|
|
class Poker:
|
|
"""扑克"""
|
|
|
|
def __init__(self):
|
|
self.cards = [Card(suite, face) for suite in Suite
|
|
for face in range(1, 14)]
|
|
self.current = 0
|
|
|
|
def shuffle(self):
|
|
"""洗牌"""
|
|
self.current = 0
|
|
random.shuffle(self.cards)
|
|
|
|
def deal(self):
|
|
"""发牌"""
|
|
card = self.cards[self.current]
|
|
self.current += 1
|
|
return card
|
|
|
|
@property
|
|
def has_next(self):
|
|
"""还有没有牌可以发"""
|
|
return self.current < len(self.cards)
|
|
|
|
|
|
def main():
|
|
"""主函数(程序入口)"""
|
|
poker = Poker()
|
|
poker.shuffle()
|
|
print(poker.cards)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|