feat(score): brelan

parent f759fb4b
...@@ -50,7 +50,7 @@ class Card: ...@@ -50,7 +50,7 @@ class Card:
return (self.score() == other.score()) return (self.score() == other.score())
def __str__(self) -> str: def __str__(self) -> str:
return f"{self.color.value} {self.value.name} {self.color.value}" return f"{self.value.value} {self.color.value}"
def score(self) -> int: def score(self) -> int:
return int(self.value.value) return int(self.value.value)
...@@ -68,11 +68,15 @@ class Hand: ...@@ -68,11 +68,15 @@ class Hand:
def __getitem__(self, item): def __getitem__(self, item):
return self.cards[item] return self.cards[item]
def __repr__(self):
return "|".join([str(c) for c in self.cards])
def give(self, other: Card): def give(self, other: Card):
self.cards.append(other) self.cards.append(other)
def value(self): def value(self):
counter = Counter([c.value for c in self.cards]) counter = Counter([c.value for c in self.cards])
print("Counter:", counter)
has_pair = False has_pair = False
has_double_pair = False has_double_pair = False
...@@ -92,6 +96,7 @@ class Hand: ...@@ -92,6 +96,7 @@ class Hand:
element_cards = [c for c in self.cards if c.value == element] element_cards = [c for c in self.cards if c.value == element]
card = element_cards[0] # Note we take a random color card = element_cards[0] # Note we take a random color
highest_card = max(highest_card, card) if highest_card else card highest_card = max(highest_card, card) if highest_card else card
if count == 2: if count == 2:
if has_pair: if has_pair:
has_double_pair = True has_double_pair = True
...@@ -111,35 +116,30 @@ class Hand: ...@@ -111,35 +116,30 @@ class Hand:
has_carre = True has_carre = True
carre = max(carre, card) if carre else card carre = max(carre, card) if carre else card
print("".join([ print(" | ".join([
f"End of analysis: ", f"ANALYSIS",
f"Carre[{carre}]" if has_carre else "no carre | ", f"Carre[{carre}]" if has_carre else "no carre",
f"Full[{full_des}|{full_par}]" if has_full else "no full | ", f"Full[{full_des}|{full_par}]" if has_full else "no full",
f"Brelan[{brelan}]" if has_brelan else "no carre | ", f"Brelan[{brelan}]" if has_brelan else "no carre",
f"Double paire[{double_pair}|{pair}]" if has_double_pair else "no Dpaire | ", f"Double paire[{double_pair}|{pair}]" if has_double_pair else "no Dpaire",
f"Paire[{pair}]" if has_pair else "no paire | ", f"Paire[{pair}]" if has_pair else "no paire",
f"Card[{highest_card}]" f"Card[{highest_card}]"
]), end=" ") ]), end=" ")
# Finished counting, let's return scores # Finished counting, let's return scores
if has_carre: if has_carre:
print("LE CARRE, LE CARRE!") score = (20 ** 5) * carre.score()
score = (20 ^ 5) * carre.score()
elif has_full: elif has_full:
print("Fouloulou") score = (20 ** 4) * full_des.score() + full_par.score()
score = (20 ^ 4) * full_des.score() + full_par
elif has_brelan: elif has_brelan:
print("BRELAN!") score = (20 ** 3) * brelan.score()
score = (20 ^ 3) * brelan.score()
elif has_double_pair: elif has_double_pair:
print("Double Paire!") score = (20 ** 2) * double_pair.score() + pair.score()
score = (20 ^ 2) * double_pair.score() + pair.score()
elif has_pair: elif has_pair:
print("Paire!") score = 20 * pair.score()
score = (20 ^ 2) * pair.score()
else: else:
score = highest_card.score() score = highest_card.score()
print("-> score=", score) print("\t-> score=\t", score)
return score return score
......
from model.data import Hand, Card, Color, Value
def carre(value) -> Hand:
return Hand([
Card(value, Color.Hearts),
Card(value, Color.Clubs),
Card(value, Color.Diamonds),
Card(value, Color.Spades)
])
def brelan(value) -> Hand:
return Hand([
Card(value, Color.Hearts),
Card(value, Color.Clubs),
Card(value, Color.Diamonds)
])
def pair(value) -> Hand:
return Hand([
Card(value, Color.Hearts),
Card(value, Color.Clubs)
])
def single(low_value) -> Hand:
return Hand([
Card(low_value, Color.Hearts)
])
def double_pair(value: Value, other: Value = None):
# Ensure no carre
if not other or other == value:
other = Value.Two if value == Value.Three else Value.Three
assert other != value
return Hand([
# Pair of value
Card(value, Color.Hearts),
Card(value, Color.Clubs),
# And pair of twos or threes
Card(other, Color.Hearts),
Card(other, Color.Clubs)
])
\ No newline at end of file
from unittest import TestCase from unittest import TestCase
from model.data import Deck, Player, Card, Value, Color, Hand from model.data import Deck, Player, Card, Value, Color, Hand
from model.hands import brelan, pair, single, double_pair
ACE_OF_HEARTS = Card(Value.Ace, Color.Hearts) ACE_OF_HEARTS = Card(Value.Ace, Color.Hearts)
ACE_OF_SPADES = Card(Value.Ace, Color.Spades)
ACE_OF_DIAMONDS = Card(Value.Ace, Color.Diamonds)
ACE_OF_CLUBS = Card(Value.Ace, Color.Clubs)
PAIR_ACE = pair(Value.Ace)
SINGLE_ACE = single(Value.Ace)
DOUBLE_PAIR_ACE = double_pair(Value.Ace)
class TestDeck(TestCase): class TestDeck(TestCase):
...@@ -43,55 +51,82 @@ class TestPlayer(TestCase): ...@@ -43,55 +51,82 @@ class TestPlayer(TestCase):
pass pass
def lowest_card_and_rest():
lowValue: Value = Value.Two
otherValues = list(Value)
otherValues.remove(lowValue)
return lowValue, otherValues
class TestHand(TestCase): class TestHand(TestCase):
def setUp(self) -> None: def setUp(self) -> None:
self.hand = Hand() self.hand = Hand()
def testSimple(self): def testSimple(self):
lowValue = Value.Two low_value, other_values = lowest_card_and_rest()
for value in [
Value.Three, for value in other_values:
Value.Four, high_hand = single(value)
Value.Five, low_hand = single(low_value)
Value.Six,
Value.Seven, self.assertGreater(high_hand.value(), low_hand.value())
Value.Eight,
Value.Nine,
Value.Ten,
Value.Jack,
Value.Queen,
Value.King,
Value.Ace,
]:
highHand = Hand([Card(value, Color.Hearts)])
lowHand = Hand([Card(lowValue, Color.Hearts)])
self.assertGreater(highHand.value(), lowHand.value())
def testPair(self): def testPair(self):
lowValue: Value = Value.Two low_value, other_values = lowest_card_and_rest()
for value in [
Value.Three, for value in other_values:
Value.Four, high_hand: Hand = pair(value)
Value.Five, low_hand: Hand = pair(low_value)
Value.Six, single_hand: Hand = single(Value.Ace)
Value.Seven,
Value.Eight, high_score = high_hand.value()
Value.Nine, low_score = low_hand.value()
Value.Ten, single_score = single_hand.value()
Value.Jack,
Value.Queen, self.assertGreater(high_score, low_score, f"Pair[{high_hand}] > Pair[{low_hand}]]")
Value.King, self.assertGreater(high_score, single_score, f"Pair[{high_hand}] > Ace")
Value.Ace,
]: def testDoublePair(self):
highHand: Hand = Hand([Card(value, Color.Hearts), Card(value, Color.Clubs)]) low_value, other_values = lowest_card_and_rest()
singleHand: Hand = Hand([ACE_OF_HEARTS])
lowHand: Hand = Hand([Card(lowValue, Color.Hearts), Card(lowValue, Color.Clubs)]) for value in other_values:
high_hand: Hand = double_pair(value)
highScore = highHand.value() low_hand: Hand = double_pair(low_value)
singleScore = singleHand.value() pair_hand: Hand = PAIR_ACE
lowScore = lowHand.value() single_hand: Hand = SINGLE_ACE
self.assertGreater(highScore, singleScore, f"Pair[{highHand.cards}] > Ace") high_score = high_hand.value()
low_score = low_hand.value()
self.assertGreater(highScore, lowScore, f"Pair[{highHand.cards}] > Pair[{lowHand.cards}]]") pair_score = pair_hand.value()
single_score = single_hand.value()
# Consider case when we compare equal double pairs
low_cards = [c.value for c in low_hand.cards]
if all([card.value in low_cards for card in high_hand.cards]):
self.assertEqual(high_score, low_score, f"DoublePair[{high_hand}] == DoublePair[{low_hand}]")
else:
self.assertGreater(high_score, low_score, f"DoublePair[{high_hand}] > DoublePair[{low_hand}]")
self.assertGreater(high_score, pair_score, f"DoublePair[{high_hand}] > Pair[Ace]")
self.assertGreater(high_score, single_score, f"DoublePair[{high_hand}] > Ace")
def testBrelan(self):
low_value, other_values = lowest_card_and_rest()
for value in other_values:
high_hand: Hand = brelan(value)
low_hand: Hand = brelan(low_value)
double_pair_hand: Hand = DOUBLE_PAIR_ACE
pair_hand: Hand = PAIR_ACE
single_hand: Hand = SINGLE_ACE
high_score = high_hand.value()
low_score = low_hand.value()
double_pair_score = double_pair_hand.value()
pair_score = pair_hand.value()
single_score = single_hand.value()
self.assertGreater(high_score, low_score, f"Brelan[{high_hand}] > Brelan[{low_hand}]]")
self.assertGreater(high_score, double_pair_score, f"Brelan[{high_hand}] > Pair[Ace]")
self.assertGreater(high_score, pair_score, f"Brelan[{high_hand}] > Pair[Ace]")
self.assertGreater(high_score, single_score, f"Brelan[{high_hand}] > Ace")
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