feat(score): Test all hands

parent 8cff0217
......@@ -2,7 +2,7 @@ from collections import defaultdict, Counter
from dataclasses import dataclass
from enum import Enum
from random import shuffle, randrange
from typing import List, Dict
from typing import List, Dict, Optional
from model.animals import random_animal_name
......@@ -33,7 +33,7 @@ class Color(Enum):
@dataclass(frozen=True)
class Card:
value: Value
color: Color
color: Optional[Color] = None
def __cmp__(self, other: "Card"):
my = self.score()
......@@ -41,16 +41,16 @@ class Card:
return (my > their) - (my < their)
def __lt__(self, other: "Card"):
return (self.score() < other.score())
return self.score() < other.score()
def __gt__(self, other: "Card"):
return (self.score() > other.score())
return self.score() > other.score()
def __eq__(self, other: "Card"):
return (self.score() == other.score())
return self.score() == other.score()
def __str__(self) -> str:
return f"{self.value.value} {self.color.value}"
return f"{self.value.name} of {self.color.value if self.color else '?'}"
def score(self) -> int:
return int(self.value.value)
......@@ -88,8 +88,6 @@ class Hand:
pair = None
double_pair = None
brelan = None
full_des = None
full_par = None
carre = None
for element, count in counter.items():
......@@ -110,8 +108,6 @@ class Hand:
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
......@@ -119,7 +115,7 @@ class Hand:
print(" | ".join([
f"ANALYSIS",
f"Carre[{carre}]" if has_carre else "no carre",
f"Full[{full_des}|{full_par}]" if has_full else "no full",
f"Full[{brelan}|{pair}]" 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",
......@@ -129,7 +125,7 @@ class Hand:
if has_carre:
score = (20 ** 5) * carre.score()
elif has_full:
score = (20 ** 4) * full_des.score() + full_par.score()
score = (20 ** 4) * brelan.score() + 20 * pair.score()
elif has_brelan:
score = (20 ** 3) * brelan.score()
elif has_double_pair:
......
......@@ -10,6 +10,17 @@ def carre(value) -> Hand:
])
def full(value_aux: Value,
value_par: Value) -> Hand:
return Hand([
Card(value_aux, Color.Hearts),
Card(value_aux, Color.Clubs),
Card(value_aux, Color.Spades),
Card(value_par, Color.Hearts),
Card(value_par, Color.Clubs)
])
def brelan(value) -> Hand:
return Hand([
Card(value, Color.Hearts),
......
from unittest import TestCase
from model.data import Deck, Player, Card, Value, Color, Hand
from model.hands import brelan, pair, single, double_pair
from model.hands import full, brelan, pair, single, double_pair, carre
ACE_OF_HEARTS = Card(Value.Ace, Color.Hearts)
ACE_OF_SPADES = Card(Value.Ace, Color.Spades)
......@@ -11,6 +11,8 @@ 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)
BRELAN_ACE = brelan(Value.Ace)
FULL_ACE = full(Value.Ace, Value.King)
class TestDeck(TestCase):
......@@ -51,7 +53,7 @@ class TestPlayer(TestCase):
pass
def lowest_card_and_rest():
def lowest_value_and_rest():
lowValue: Value = Value.Two
otherValues = list(Value)
otherValues.remove(lowValue)
......@@ -64,7 +66,7 @@ class TestHand(TestCase):
self.hand = Hand()
def testSimple(self):
low_value, other_values = lowest_card_and_rest()
low_value, other_values = lowest_value_and_rest()
for value in other_values:
high_hand = single(value)
......@@ -73,7 +75,7 @@ class TestHand(TestCase):
self.assertGreater(high_hand.value(), low_hand.value())
def testPair(self):
low_value, other_values = lowest_card_and_rest()
low_value, other_values = lowest_value_and_rest()
for value in other_values:
high_hand: Hand = pair(value)
......@@ -88,7 +90,7 @@ class TestHand(TestCase):
self.assertGreater(high_score, single_score, f"Pair[{high_hand}] > Ace")
def testDoublePair(self):
low_value, other_values = lowest_card_and_rest()
low_value, other_values = lowest_value_and_rest()
for value in other_values:
high_hand: Hand = double_pair(value)
......@@ -111,7 +113,7 @@ class TestHand(TestCase):
self.assertGreater(high_score, single_score, f"DoublePair[{high_hand}] > Ace")
def testBrelan(self):
low_value, other_values = lowest_card_and_rest()
low_value, other_values = lowest_value_and_rest()
for value in other_values:
high_hand: Hand = brelan(value)
......@@ -130,3 +132,82 @@ class TestHand(TestCase):
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")
def testFulls(self):
# AssertionError: 24060 not greater than 24060 :
# Full[Three of ♥|Three of ♣|Three of ♠|Four of ♥|Four of ♣]
# > Full[Two of ♥|Two of ♣|Two of ♠|Four of ♥|Four of ♣]]
full1 = Hand([Card(v) for v in [
Value.Three,
Value.Three,
Value.Three,
Value.Four,
Value.Four,
]])
full2 = Hand([Card(v) for v in [
Value.Two,
Value.Two,
Value.Two,
Value.Four,
Value.Four,
]])
self.assertGreater(full1.value(), full2.value(), "Full1 > full2 (threes >> twos)")
def testFull(self):
low_value, other_values = lowest_value_and_rest()
for full_aux in other_values: # Full_aux: brelan
full_options = other_values.copy()
full_options.remove(full_aux)
full_options.append(low_value)
for full_par in full_options: # Full_des: paire
high_hand: Hand = full(full_aux, full_par)
low_hand: Hand = full(low_value, full_par)
brelan_hand: Hand = BRELAN_ACE
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()
brelan_score = brelan_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"Full[{high_hand}] > Full[{low_hand}]]")
self.assertGreater(high_score, brelan_score, f"Full[{high_hand}] > Brelan[Ace]")
self.assertGreater(high_score, double_pair_score, f"Full[{high_hand}] > Pair[Ace]")
self.assertGreater(high_score, pair_score, f"Full[{high_hand}] > Pair[Ace]")
self.assertGreater(high_score, single_score, f"Full[{high_hand}] > Ace")
def testCarre(self):
low_value, other_values = lowest_value_and_rest()
for value in other_values:
high_hand: Hand = carre(value)
low_hand: Hand = carre(low_value)
full_hand: Hand = FULL_ACE
brelan_hand: Hand = BRELAN_ACE
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()
full_score = full_hand.value()
brelan_score = brelan_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"Carre[{high_hand}] > Carre[{low_hand}]]")
self.assertGreater(high_score, full_score, f"Carre[{high_hand}] > Full[Ace]]")
self.assertGreater(high_score, brelan_score, f"Carre[{high_hand}] > Brelan[Ace]")
self.assertGreater(high_score, double_pair_score, f"Carre[{high_hand}] > Pair[Ace]")
self.assertGreater(high_score, pair_score, f"Carre[{high_hand}] > Pair[Ace]")
self.assertGreater(high_score, single_score, f"Carre[{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