feat: Score Pair vs single

parent f62a31a3
......@@ -35,17 +35,34 @@ class Card:
value: Value
color: Color
def __cmp__(self, other: "Card"):
my = self.score()
their = other.score()
return (my > their) - (my < their)
def __lt__(self, other: "Card"):
return (self.score() < other.score())
def __gt__(self, other: "Card"):
return (self.score() > other.score())
def __eq__(self, other: "Card"):
return (self.score() == other.score())
def __str__(self) -> str:
return f"{self.color.value} {self.value.name} {self.color.value}"
def score(self) -> int:
return int(self.value.value)
class Hand:
def __init__(self, cards=None):
def __init__(self, cards: List[Card] = None):
if cards is None:
cards = []
self.cards: List[Card] = cards
def __len__(self):
def __len__(self) -> int:
return len(self.cards)
def __getitem__(self, item):
......@@ -55,16 +72,75 @@ class Hand:
self.cards.append(other)
def value(self):
print(f"Looking at value of {len(self.cards)} cards")
counter = Counter(self.cards)
print(counter)
if len(self.cards) == 1: # Simple, v=x
return int(self.cards[0].value.value)
# elif len(self.cards) == 2: # Paire, v=20*x
# elif len(self.cards) == 3: # Brelan ou paire, v=20*x
# elif len(self.cards) == 2: # Paire, v=20*x
# elif len(self.cards) == 2: # Paire, v=20*x
counter = Counter([c.value for c in self.cards])
has_pair = False
has_double_pair = False
has_brelan = False
has_full = False
has_carre = False
highest_card = None
pair = None
double_pair = None
brelan = None
full_des = None
full_par = None
carre = None
for element, count in counter.items():
element_cards = [c for c in self.cards if c.value == element]
card = element_cards[0] # Note we take a random color
highest_card = max(highest_card, card) if highest_card else card
if count == 2:
if has_pair:
has_double_pair = True
double_pair = max(pair, card)
pair = min(pair, card)
else:
has_pair = True
pair = max(pair, card) if pair else card
if count == 3:
has_brelan = True
brelan = max(brelan, card) if brelan else card
if has_brelan and has_pair:
has_full = True
full_des = max(brelan, card) if brelan else card
full_par = max(pair, card) if pair else card
if count == 4:
has_carre = True
carre = max(carre, card) if carre else card
print("".join([
f"End of analysis: ",
f"Carre[{carre}]" if has_carre else "no carre | ",
f"Full[{full_des}|{full_par}]" if has_full else "no full | ",
f"Brelan[{brelan}]" if has_brelan else "no carre | ",
f"Double paire[{double_pair}|{pair}]" if has_double_pair else "no Dpaire | ",
f"Paire[{pair}]" if has_pair else "no paire | ",
f"Card[{highest_card}]"
]), end=" ")
# Finished counting, let's return scores
if has_carre:
print("LE CARRE, LE CARRE!")
score = (20 ^ 5) * carre.score()
elif has_full:
print("Fouloulou")
score = (20 ^ 4) * full_des.score() + full_par
elif has_brelan:
print("BRELAN!")
score = (20 ^ 3) * brelan.score()
elif has_double_pair:
print("Double Paire!")
score = (20 ^ 2) * double_pair.score() + pair.score()
elif has_pair:
print("Paire!")
score = (20 ^ 2) * pair.score()
else:
score = highest_card.score()
print("-> score=", score)
return score
class Deck:
......
......@@ -67,3 +67,31 @@ class TestHand(TestCase):
lowHand = Hand([Card(lowValue, Color.Hearts)])
self.assertGreater(highHand.value(), lowHand.value())
def testPair(self):
lowValue: Value = Value.Two
for value in [
Value.Three,
Value.Four,
Value.Five,
Value.Six,
Value.Seven,
Value.Eight,
Value.Nine,
Value.Ten,
Value.Jack,
Value.Queen,
Value.King,
Value.Ace,
]:
highHand: Hand = Hand([Card(value, Color.Hearts), Card(value, Color.Clubs)])
singleHand: Hand = Hand([ACE_OF_HEARTS])
lowHand: Hand = Hand([Card(lowValue, Color.Hearts), Card(lowValue, Color.Clubs)])
highScore = highHand.value()
singleScore = singleHand.value()
lowScore = lowHand.value()
self.assertGreater(highScore, singleScore, f"Pair[{highHand.cards}] > Ace")
self.assertGreater(highScore, lowScore, f"Pair[{highHand.cards}] > Pair[{lowHand.cards}]]")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment