example.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """
  2. 扑克
  3. """
  4. import enum
  5. import random
  6. @enum.unique
  7. class Suite(enum.Enum):
  8. """花色(枚举)"""
  9. SPADE, HEART, CLUB, DIAMOND = range(4)
  10. class Card:
  11. """牌"""
  12. def __init__(self, suite, face):
  13. self.suite = suite
  14. self.face = face
  15. def __repr__(self):
  16. suites = '♠♥♣♦'
  17. faces = ['', 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
  18. return f'{suites[self.suite.value]}{faces[self.face]}'
  19. class Poker:
  20. """扑克"""
  21. def __init__(self):
  22. self.cards = [Card(suite, face) for suite in Suite
  23. for face in range(1, 14)]
  24. self.current = 0
  25. def shuffle(self):
  26. """洗牌"""
  27. self.current = 0
  28. random.shuffle(self.cards)
  29. def deal(self):
  30. """发牌"""
  31. card = self.cards[self.current]
  32. self.current += 1
  33. return card
  34. @property
  35. def has_next(self):
  36. """还有没有牌可以发"""
  37. return self.current < len(self.cards)
  38. def main():
  39. """主函数(程序入口)"""
  40. poker = Poker()
  41. poker.shuffle()
  42. print(poker.cards)
  43. if __name__ == '__main__':
  44. main()