-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcards.py
More file actions
51 lines (39 loc) · 1.48 KB
/
cards.py
File metadata and controls
51 lines (39 loc) · 1.48 KB
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
SUITS = ["Clubs", "Diamonds", "Hearts", "Spades"]
RANKS = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"]
class Card:
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
self.rank_index = RANKS.index(self.rank)
self.suit_index = SUITS.index(self.suit)
# Implemented card comparison using conditional branching
# def __eq__(self, other):
# if self.rank_index == other.rank_index and self.suit_index == other.suit_index:
# return True
# return False
#
# def __lt__(self, other):
# if self.rank_index < other.rank_index:
# return True
# elif self.rank_index == other.rank_index and self.suit_index < other.suit_index:
# return True
# return False
#
# def __gt__(self, other):
# if self.rank_index > other.rank_index:
# return True
# elif self.rank_index == other.rank_index and self.suit_index > other.suit_index:
# return True
# return False
# Implemented card comparison using tuple comparation
def _key(self):
return self.rank_index, self.suit_index
def __eq__(self, other):
return self._key() == other._key()
def __lt__(self, other):
return self._key() < other._key()
def __gt__(self, other):
return self._key() > other._key()
# don't touch below this line
def __str__(self):
return f"{self.rank} of {self.suit}"