mirror of
https://github.com/correl/euler.git
synced 2024-11-27 19:19:50 +00:00
Made private fields actually private
git-svn-id: file:///srv/svn/euler@7 e5f4c3ec-3c0c-11df-b522-21efaa4426b5
This commit is contained in:
parent
48f5524596
commit
de333d0940
1 changed files with 25 additions and 25 deletions
50
054/poker.py
50
054/poker.py
|
@ -52,23 +52,23 @@ class Hand:
|
||||||
'Royal Flush'
|
'Royal Flush'
|
||||||
]
|
]
|
||||||
def __init__(self, cards):
|
def __init__(self, cards):
|
||||||
self._rank = None
|
self.__rank = None
|
||||||
self._cards = sorted([Card(c) for c in cards], cmp=Card.compare, reverse=True)
|
self.__cards = sorted([Card(c) for c in cards], cmp=Card.compare, reverse=True)
|
||||||
self._values = []
|
self.__values = []
|
||||||
self.rank()
|
self.rank()
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str.format("Cards: {0} Rank: '{1}' Values: {2}",
|
return str.format("Cards: {0} Rank: '{1}' Values: {2}",
|
||||||
[str(c.value) + c.suit for c in self._cards],
|
[str(c.value) + c.suit for c in self.__cards],
|
||||||
Hand.RANKS[self.rank()],
|
Hand.RANKS[self.rank()],
|
||||||
self.values())
|
self.values())
|
||||||
def rank(self):
|
def rank(self):
|
||||||
if self._rank:
|
if self.__rank:
|
||||||
return self._rank
|
return self.__rank
|
||||||
flush = True
|
flush = True
|
||||||
straight = False
|
straight = False
|
||||||
last = None
|
last = None
|
||||||
merged = {}
|
merged = {}
|
||||||
for c in self._cards:
|
for c in self.__cards:
|
||||||
if last:
|
if last:
|
||||||
if flush and c.suit != last.suit:
|
if flush and c.suit != last.suit:
|
||||||
flush = False
|
flush = False
|
||||||
|
@ -79,43 +79,43 @@ class Hand:
|
||||||
merged[c.value] = 1
|
merged[c.value] = 1
|
||||||
if (len(merged)) == 5:
|
if (len(merged)) == 5:
|
||||||
# All unique cards, check for a straight
|
# All unique cards, check for a straight
|
||||||
if self._cards[0].value - self._cards[4].value == 4 or \
|
if self.__cards[0].value - self.__cards[4].value == 4 or \
|
||||||
(self._cards[4].value == 2 and self._cards[1].value == 5 and self._cards[0].value == 14):
|
(self.__cards[4].value == 2 and self.__cards[1].value == 5 and self.__cards[0].value == 14):
|
||||||
straight = True
|
straight = True
|
||||||
if straight and flush:
|
if straight and flush:
|
||||||
if self._cards[0].value == 14:
|
if self.__cards[0].value == 14:
|
||||||
self._rank = Hand.ROYAL_FLUSH
|
self.__rank = Hand.ROYAL_FLUSH
|
||||||
else:
|
else:
|
||||||
self._rank = Hand.STRAIGHT_FLUSH
|
self.__rank = Hand.STRAIGHT_FLUSH
|
||||||
elif flush:
|
elif flush:
|
||||||
self._rank = Hand.FLUSH
|
self.__rank = Hand.FLUSH
|
||||||
elif straight:
|
elif straight:
|
||||||
self._rank = Hand.STRAIGHT
|
self.__rank = Hand.STRAIGHT
|
||||||
else:
|
else:
|
||||||
self._rank = Hand.HIGH_CARD
|
self.__rank = Hand.HIGH_CARD
|
||||||
self._values = [c.value for c in self._cards]
|
self.__values = [c.value for c in self.__cards]
|
||||||
else:
|
else:
|
||||||
multiples = [m for m in sorted(merged.items(), key = operator.itemgetter(1), reverse = True) if m[1] > 1]
|
multiples = [m for m in sorted(merged.items(), key = operator.itemgetter(1), reverse = True) if m[1] > 1]
|
||||||
if len(multiples) > 1:
|
if len(multiples) > 1:
|
||||||
if multiples[0][1] == multiples[1][1]:
|
if multiples[0][1] == multiples[1][1]:
|
||||||
self._rank = Hand.TWO_PAIRS
|
self.__rank = Hand.TWO_PAIRS
|
||||||
else:
|
else:
|
||||||
self._rank = Hand.FULL_HOUSE
|
self.__rank = Hand.FULL_HOUSE
|
||||||
else:
|
else:
|
||||||
if multiples[0][1] > 3:
|
if multiples[0][1] > 3:
|
||||||
self._rank = Hand.FOUR_OF_A_KIND
|
self.__rank = Hand.FOUR_OF_A_KIND
|
||||||
elif multiples[0][1] == 3:
|
elif multiples[0][1] == 3:
|
||||||
self._rank = Hand.THREE_OF_A_KIND
|
self.__rank = Hand.THREE_OF_A_KIND
|
||||||
else:
|
else:
|
||||||
self._rank = Hand.ONE_PAIR
|
self.__rank = Hand.ONE_PAIR
|
||||||
mvalues = [m[0] for m in multiples]
|
mvalues = [m[0] for m in multiples]
|
||||||
self._values = mvalues + [c.value for c in self._cards if c.value not in mvalues]
|
self.__values = mvalues + [c.value for c in self.__cards if c.value not in mvalues]
|
||||||
|
|
||||||
return self._rank
|
return self.__rank
|
||||||
def values(self):
|
def values(self):
|
||||||
if not self._values:
|
if not self.__values:
|
||||||
self.rank()
|
self.rank()
|
||||||
return self._values
|
return self.__values
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def compare(a, b):
|
def compare(a, b):
|
||||||
# Compare hand rankings
|
# Compare hand rankings
|
||||||
|
|
Loading…
Reference in a new issue