feat(Hand): base + score simple

parent 1b879a99
from collections import defaultdict from collections import defaultdict, Counter
from dataclasses import dataclass from dataclasses import dataclass
from enum import Enum from enum import Enum
from random import shuffle, randrange from random import shuffle, randrange
...@@ -8,19 +8,19 @@ from model.animals import random_animal_name ...@@ -8,19 +8,19 @@ from model.animals import random_animal_name
class Value(Enum): class Value(Enum):
Ace = 1 Two = 1
Two = 2 Three = 2
Three = 3 Four = 3
Four = 4 Five = 4
Five = 5 Six = 5
Six = 6 Seven = 6
Seven = 7 Eight = 7
Eight = 8 Nine = 8
Nine = 9 Ten = 9
Ten = 10 Jack = 10
Jack = 11 Queen = 11
Queen = 12 King = 12
King = 13 Ace = 13
class Color(Enum): class Color(Enum):
...@@ -39,6 +39,34 @@ class Card: ...@@ -39,6 +39,34 @@ class Card:
return f"{self.color.value} {self.value.name} {self.color.value}" return f"{self.color.value} {self.value.name} {self.color.value}"
class Hand:
def __init__(self, cards=None):
if cards is None:
cards = []
self.cards: List[Card] = cards
def __len__(self):
return len(self.cards)
def __getitem__(self, item):
return self.cards[item]
def give(self, other: Card):
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
class Deck: class Deck:
def __init__(self): def __init__(self):
self.cards = [Card(v, c) for v in Value for c in Color] self.cards = [Card(v, c) for v in Value for c in Color]
...@@ -63,10 +91,10 @@ class Deck: ...@@ -63,10 +91,10 @@ class Deck:
class Player: class Player:
name: str = random_animal_name() name: str = random_animal_name()
hand: List[Card] = [] hand: Hand = Hand()
def give(self, card: Card): def give(self, card: Card):
self.hand.append(card) self.hand.give(card)
class Game: class Game:
...@@ -96,7 +124,4 @@ class Game: ...@@ -96,7 +124,4 @@ class Game:
for p in self.players: for p in self.players:
announce = p.announce(current_bet) announce = p.announce(current_bet)
# TODO: Put next first player first of list # TODO: Put next first player first of list
from unittest import TestCase from unittest import TestCase
from model.data import Deck, Player, Card, Value, Color from model.data import Deck, Player, Card, Value, Color, Hand
ACE_OF_HEARTS = Card(Value.Ace, Color.Hearts)
class TestDeck(TestCase): class TestDeck(TestCase):
...@@ -31,7 +33,7 @@ class TestPlayer(TestCase): ...@@ -31,7 +33,7 @@ class TestPlayer(TestCase):
self.assertEqual(0, len(self.player.hand), "Begin no cards") self.assertEqual(0, len(self.player.hand), "Begin no cards")
self.assertTrue(len(self.player.name), "Has a name") self.assertTrue(len(self.player.name), "Has a name")
self.player.give(Card(Value.Ace, Color.Hearts)) self.player.give(ACE_OF_HEARTS)
self.assertEqual(1, len(self.player.hand), "Gave one card") self.assertEqual(1, len(self.player.hand), "Gave one card")
self.assertEqual(Value.Ace, self.player.hand[0].value, "Is Ace") self.assertEqual(Value.Ace, self.player.hand[0].value, "Is Ace")
...@@ -39,3 +41,29 @@ class TestPlayer(TestCase): ...@@ -39,3 +41,29 @@ class TestPlayer(TestCase):
def testDefeats(self): def testDefeats(self):
pass pass
class TestHand(TestCase):
def setUp(self) -> None:
self.hand = Hand()
def testSimple(self):
lowValue = 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([Card(value, Color.Hearts)])
lowHand = Hand([Card(lowValue, Color.Hearts)])
self.assertGreater(highHand.value(), lowHand.value())
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