hands.py 2.2 KB
Newer Older
1 2 3
from random import choice
from typing import List

4 5 6
from server.model.card import Card
from server.model.color import Color
from server.model.value import Value
7
from server.model.hand import Hand
PLN (Algolia) committed
8 9


10 11 12 13 14 15 16 17
def hand(cards: List[Card]) -> Hand:
    return Hand(cards=cards)


def card(value: Value, color: Color) -> Card:
    return Card(value=value, color=color)


PLN (Algolia) committed
18
def carre(value) -> Hand:
19 20 21 22 23
    return hand([
        card(value, Color.Hearts),
        card(value, Color.Clubs),
        card(value, Color.Diamonds),
        card(value, Color.Spades)
PLN (Algolia) committed
24 25 26
    ])


27 28
def full(value_aux: Value,
         value_par: Value) -> Hand:
29 30 31 32 33 34
    return hand([
        card(value_aux, Color.Hearts),
        card(value_aux, Color.Clubs),
        card(value_aux, Color.Diamonds),
        card(value_par, Color.Hearts),
        card(value_par, Color.Clubs)
35 36 37
    ])


PLN (Algolia) committed
38
def brelan(value) -> Hand:
39 40 41 42
    return hand([
        card(value, Color.Hearts),
        card(value, Color.Clubs),
        card(value, Color.Diamonds)
PLN (Algolia) committed
43 44 45 46
    ])


def pair(value) -> Hand:
47 48 49
    return hand([
        card(value, Color.Hearts),
        card(value, Color.Clubs)
PLN (Algolia) committed
50 51 52 53
    ])


def single(low_value) -> Hand:
54 55
    return hand([
        card(low_value, Color.Hearts)
PLN (Algolia) committed
56 57 58 59 60 61 62 63 64
    ])


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

65
    return hand([
PLN (Algolia) committed
66
        # Pair of value
67 68
        card(value, Color.Hearts),
        card(value, Color.Clubs),
PLN (Algolia) committed
69
        # And pair of twos or threes
70 71
        card(other, Color.Hearts),
        card(other, Color.Clubs)
72
    ])
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88


def all_options() -> List[Hand]:
    hands = []

    carres = [carre(value) for value in Value]
    brelans = [brelan(value) for value in Value]
    pairs = [pair(value) for value in Value]
    singles = [single(value) for value in Value]

    hands.extend(carres)
    hands.extend(brelans)
    hands.extend(pairs)
    hands.extend(singles)
    for b in brelans:
        for p in pairs:
89
            if not any([c in b.cards for c in p.cards]):  # Valid full
90
                hands.append(hand([*b.cards, *p.cards]))
91 92 93 94 95 96 97 98
    return hands


options = all_options()


def random_option() -> Hand:
    return choice(options)