-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard.py
98 lines (72 loc) · 2.04 KB
/
card.py
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
91
92
93
94
95
96
97
98
"""
This module contains the definition of types fundamental to card games,
most notably the type Card.
"""
import sys
from random import shuffle
from enum import Enum
class OrderedEnum(Enum):
def __ge__(self, other):
if self.__class__ is other.__class__:
return self.value >= other.value
return NotImplemented
def __gt__(self, other):
if self.__class__ is other.__class__:
return self.value > other.value
return NotImplemented
def __le__(self, other):
if self.__class__ is other.__class__:
return self.value <= other.value
return NotImplemented
def __lt__(self, other):
if self.__class__ is other.__class__:
return self.value < other.value
return NotImplemented
class Suit(OrderedEnum):
clubs = 0
diamonds = 1
spades = 2
hearts = 3
def __repr__(self):
return ['C', 'D', 'S', 'H'][self.value - 0]
class Rank(OrderedEnum):
two = 2
three = 3
four = 4
five = 5
six = 6
seven = 7
eight = 8
nine = 9
ten = 10
jack = 11
queen = 12
king = 13
ace = 14
def __repr__(self):
if self.value < 10:
return str(self.value)
else:
return ['T', 'J', 'Q', 'K', 'A'][self.value - 10]
class Card:
def __init__(self, suit, rank):
self.suit = suit
self.rank = rank
def __repr__(self):
return repr(self.rank) + repr(self.suit)
def __lt__(self, other):
return (self.suit, self.rank) < (other.suit, other.rank)
def __eq__(self, other):
return (self.suit, self.rank) == (other.suit, other.rank)
def __hash__(self):
return hash(self.__str__())
class Deck:
def __init__(self):
self.cards = [Card(suit, rank) for suit in Suit for rank in Rank]
def deal(self):
"""
Shuffles the cards and returns 4 lists of 13 cards.
"""
shuffle(self.cards)
for i in range(0, 52, 13):
yield sorted(self.cards[i:i + 13])