cards.py 959 B

1234567891011121314151617181920212223242526272829303132333435
  1. import random
  2. suits = ["harts", "spades", "clubs", "diamonds"]
  3. denoms = ["ace", "king", "queen", "jack", "10", "9", "8", "7", "6", "5", "4", "3", "2"]
  4. class Card(object):
  5. def __init__(self, suit, denom):
  6. self.suit = suit
  7. self.denom = denom
  8. def __eq__(self, other):
  9. if isinstance(other, self.__class__):
  10. return self.__dict__ == other.__dict__
  11. elif isinstance(other, dict):
  12. return self.__dict__ == other
  13. else:
  14. return False
  15. def __ne__(self, other):
  16. return not self.__eq__(other)
  17. def shuffle(numberOfDecks):
  18. deck = [Card(x, y) for x in suits for y in denoms] + [Card(None, "joker") for _ in range(3)]
  19. stock = deck * numberOfDecks
  20. for n in range(len(stock)):
  21. x = random.randint(0, len(stock) - n)
  22. value = stock[x]
  23. stock[x] = stock[n]
  24. stock[n] = value
  25. return stock
  26. if __name__ == "__main__":
  27. print(shuffle(2))