Ejemplo n.º 1
0
 def test_contains(self):
     h2 = Card('h2')
     h3 = Card('h3')
     h4 = Card('h4')
     h5 = Card('h5')
     group1 = CardSet([h2, h3])
     group2 = CardSet([h4, h5])
     table = Table([group1, group2])
     self.assertTrue(table.contains(group2))
     self.assertFalse(table.contains(h2))
Ejemplo n.º 2
0
 def test_append(self):
     table = Table()
     h2 = Card('h2')
     h3 = Card('h3')
     h4 = Card('h4')
     h5 = Card('h5')
     group = CardSet([h2, h3, h4])
     table.add_cards(group)
     table.add_cards(CardSet(h5))
     comparison_table = Table([CardSet([h2, h3, h4]), CardSet(h5)])
     self.assertTrue(table == comparison_table)
Ejemplo n.º 3
0
 def test_contains(self):
     h3 = Card('h3')
     h4 = Card('h4')
     h5 = Card('h5')
     h6 = Card('h6')
     child_group = CardSet([h3, h4, h5])
     parent_group = CardSet([h3, h4, h5, h6])
     self.assertTrue(parent_group.contains(child_group))
     self.assertTrue(child_group.isin(parent_group))
     self.assertFalse(child_group.contains(parent_group))
     self.assertTrue(child_group.contains(h3))
     self.assertTrue(child_group.contains(CardSet(h3)))
Ejemplo n.º 4
0
 def test_add_to_target(self):
     h2 = Card('h2')
     h3 = Card('h3')
     h4 = Card('h4')
     h5 = Card('h5')
     h6 = Card('h6')
     h7 = Card('h7')
     group1 = CardSet([h2, h3])
     group2 = CardSet([h4, h5])
     group3 = CardSet([h6, h7])
     table = Table([group1, group2])
     table.add_cards_to_target_cards(added_set=group3, target_set=group2)
     self.assertTrue(table == Table([group1, CardSet([h4, h5, h6, h7])]))
Ejemplo n.º 5
0
def make_files():
    all_sets_json = AllSetsReader.read(ALL_SETS_JSON_PATH)

    for card_set_dict in AllSetsReader.split_sets(all_sets_json):
        card_set = CardSet(card_set_dict)

        path = "{sets_directory}/{set_name}.json".format(
            sets_directory=SETS_DIRECTORY, set_name=''.join([char for char in card_set.name if char.isalnum()])
        )

        card_set.to_file(path)

        yield path
Ejemplo n.º 6
0
    def test_append_extend(self):
        # case 1: appending a card object:
        h3 = Card('h3')
        group = CardSet(h3)
        h4 = Card('h4')
        group.append(h4)
        self.assertTrue(group == CardSet([h3, h4]))

        # case 2: appending a card_set object
        card_set = CardSet([h3, h4])
        group.extend(card_set)
        self.assertTrue(group == CardSet([h3, h3, h4, h4]))
Ejemplo n.º 7
0
 def test_most_common_rank(self):
     sample_set = CardSet()
     sample_set.add_card(Card('5s'))
     sample_set.add_card(Card('5c'))
     sample_set.add_card(Card('6h'))
     sample_set.add_card(Card('Jh'))
     result = sample_set.most_common_rank()
     print(sample_set.to_string())
     self.assertEqual(result, Card.FIVE)
Ejemplo n.º 8
0
 def test_most_common_suit(self):
     sample_set = CardSet()
     sample_set.add_card(Card('5s'))
     sample_set.add_card(Card('6s'))
     sample_set.add_card(Card('7h'))
     sample_set.add_card(Card('Jc'))
     result = sample_set.most_common_suit()
     print(sample_set.to_string())
     self.assertEqual(result, Card.SPADES)
Ejemplo n.º 9
0
 def test_sort(self):
     s2 = Card('s2')
     s2.hidden_suit = 'h'
     s2.hidden_int_value = 5
     s2.hidden_rank = '5'
     h4 = Card('h4')
     h6 = Card('h6')
     d2 = Card('d2')
     d2 = Card('d2')
     d2.hidden_suit = 'd'
     d2.hidden_int_value = 7
     d2.hidden_rank = '7'
     group = CardSet([h4, s2, d2, h6])
     group.sort_cards()
     comparison_group = CardSet([h4, s2, h6,
                                 d2])  # what the cards should look like
     self.assertEqual(group.card_ids, comparison_group.card_ids)
Ejemplo n.º 10
0
def input_to_card_set(input: str):
    '''Transforms user input string into a cardset object'''
    if input == 'finished':
        return('finished')
    card_strings = input.strip().split(', ')
    card_lst = [Card(cs) for cs in card_strings]
    card_set = CardSet(card_lst)
    return(card_set)
Ejemplo n.º 11
0
def write_table():
    table = {}
    t = time.time()
    for c1, c2, c3, c4, c5 in combinations(
            product(STR_RANKS.keys(), STR_SUITS), 5):
        cset = CardSet([
            Card("".join(c1)),
            Card("".join(c2)),
            Card("".join(c3)),
            Card("".join(c4)),
            Card("".join(c5))
        ])
        flop_eval = FlopEvaluator(cset)
        made = flop_eval.get_made()
        table[cset.get_bin()] = made.keys()[made.values().index(True)]
    with open('./lut/test.lut', 'w') as f:
        json.dump(table, f)
    print t, time.time(), time.time() - t
Ejemplo n.º 12
0
def main() -> None:
    with open('cah-cards-full.json', 'r') as f:
        sets = [CardSet(**_set) for _set in json.load(f)]

    deck = Deck(*sets)
    game = Game(
        deck=deck,
        players=[Player('Tom'),
                 Player('Beatrice'),
                 Player('Beatrice 2')])

    game.play()
Ejemplo n.º 13
0
    def test_consecutive(self):
        s2 = Card('s2')
        s2.hidden_int_value = 5
        s2.hidden_suit = 'h'
        s2.hidden_rank = '5'
        h4 = Card('h4')
        h6 = Card('h6')

        # case 1
        group = CardSet([h4, s2, h6])
        self.assertTrue(group.consecutive)
        self.assertFalse(group.same_rank)
        self.assertTrue(group.wc_is_in_cards)

        # case 2
        d2 = Card('d2')
        d2.hidden_suit = 'd'
        d2.hidden_int_value = 7
        d2.hidden_rank = '7'
        group = CardSet([h4, s2, h6, d2])
        self.assertTrue(group.consecutive)
        self.assertFalse(group.same_rank)

        # case 3
        group = CardSet([s2, h4, h6])
        self.assertFalse(group.consecutive)
        self.assertFalse(group.same_rank)
        self.assertTrue(group.wc_is_in_cards)

        # case 4
        d5 = Card('d5')
        c5 = Card('c5')
        group = CardSet([s2, d5, c5])
        self.assertFalse(group.consecutive)
        self.assertTrue(group.same_rank)
        self.assertTrue(group.wc_is_in_cards)
Ejemplo n.º 14
0
    def test_isin(self):
        # case 1
        h3 = Card('h3')
        h4 = Card('h4')
        h5 = Card('h5')
        group = CardSet([h3, h4, h5])
        self.assertTrue(h3.isin(group))

        # case 2: wildcard
        c2 = Card('c2')
        c2.hidden_suit = 'H'
        c2.hidden_int_value = 3
        c2.hidden_rank = '3'
        self.assertTrue(c2.isin(group))
        self.assertTrue(
            c2 == h3
        )  # c2 is considered to be in the group since c2 == h3 after assigning new attrs
        c2.reset_wildcard_attrs()
        self.assertFalse(
            c2.isin(group)
        )  # c2 is no longer in the group after resetting its attributes
        self.assertFalse(c2 == h3)
Ejemplo n.º 15
0
def input_to_card_set(input):
    card_strings = input.strip().split(', ')
    card_lst = [Card(cs) for cs in card_strings]
    card_set = CardSet(card_lst)
    return (card_set)
Ejemplo n.º 16
0
def _combo_to_hand(combo):
    return CardSet([Card(combo[:2]), Card(combo[2:])])
Ejemplo n.º 17
0
        cset = CardSet([
            Card("".join(c1)),
            Card("".join(c2)),
            Card("".join(c3)),
            Card("".join(c4)),
            Card("".join(c5))
        ])
        flop_eval = FlopEvaluator(cset)
        made = flop_eval.get_made()
        table[cset.get_bin()] = made.keys()[made.values().index(True)]
    with open('./lut/test.lut', 'w') as f:
        json.dump(table, f)
    print t, time.time(), time.time() - t


def read_table():
    with open('./lut/test.lut', 'r') as f:
        return json.load(f)


if __name__ == '__main__':
    c1 = Card('7c')
    c2 = Card('5c')
    c3 = Card('4c')
    c4 = Card('3c')
    c5 = Card('2s')

    cset = CardSet([c4, c2, c3, c1, c5])
    table = read_table()
    print table[u'%d' % cset.get_bin()]
Ejemplo n.º 18
0
    def test_eq(self):
        h3 = Card('h3')
        group = CardSet(h3)
        self.assertFalse(h3 == group)

        h2 = Card('h2')
        h4 = Card('h4')
        h5 = Card('h5')
        h6 = Card('h6')

        h2.assign_wildcard_attrs()  # assign 'h' and 4 as hidden attrs
        group1 = CardSet([h2, h5, h6])
        group2 = CardSet([h4, h5, h6])
        self.assertTrue(h2 == h4)
        self.assertTrue(group1 == group2)
        self.assertTrue(group1.contains(group2))

        group2.remove(
            h2
        )  # since h2 and h4 are equal, removing h2 should in effect remove h4
        self.assertTrue(group2 == CardSet([h5, h6]))

        group2 = CardSet([h4, h5, h6])
        group2.remove(CardSet([h2, h5]))
        print(group2)
        self.assertTrue(group2 == CardSet([h6]))
Ejemplo n.º 19
0
class FlopEvaluator:

    STFL = None  # straight flush
    FL = None  # flush
    ST = None  # straight

    QUADS = None
    FH = None
    TRIPS = None
    TWOPAIR = None
    PAIR = None
    HIGH = None

    LUT = None

    def __init__(self, card_set=None):
        if card_set is None:
            self.card_set = CardSet()
        else:
            self.card_set = card_set

    def use_table(self, table):
        self.LUT = table

    def get_made(self):
        if self.LUT is not None:
            try:
                return self.LUT[u'%d' % self.card_set.get_bin()]
            except KeyError:
                pass

        self._calc_flush_or_straight()
        self._calc_multiples()

        made = {
            "Straight Flush": self.STFL,
            "Flush": self.FL,
            "Straight": self.ST,
            "Quads": self.QUADS,
            "Full House": self.FH,
            "Trips": self.TRIPS,
            "Two Pair": self.TWOPAIR,
            "Pair": self.PAIR,
            "High Card": self.HIGH,
        }
        return made.keys()[made.values().index(True)]

    def _calc_flush_or_straight(self):
        if self.STFL is not None:
            return
        if len(self.card_set) != 5:
            return

        self.card_set.sort_by_rank()

        straight_cur = STR_RANKS[self.card_set[0].rank]
        first_suit = self.card_set[0].suit
        for c in self.card_set[1:]:
            if self.ST is None and straight_cur == STR_RANKS[c.rank] + 1:
                straight_cur = STR_RANKS[c.rank]
            else:
                self.ST = False
            if c.suit != first_suit:
                self.FL = False

        if self.ST is None:
            self.ST = True
        if self.FL is None:
            self.FL = True
        if self.ST and self.FL:
            self.STFL = True
            self.ST = False
            self.FL = False
        else:
            self.STFL = False

    def _calc_multiples(self):
        if any((self.STFL, self.FL, self.ST)):
            self.QUADS = False
            self.FH = False
            self.TRIPS = False
            self.TWOPAIR = False
            self.PAIR = False
            self.HIGH = False
            return
        elif self.QUADS is not None:
            return

        ranks = self._rank_count()

        self.QUADS = ranks.values().count(4) == 1
        self.TRIPS = ranks.values().count(3) == 1
        self.TWOPAIR = ranks.values().count(2) == 2
        self.PAIR = ranks.values().count(2) == 1
        self.HIGH = ranks.values().count(1) == 5

        if self.TRIPS and self.PAIR:
            self.FH = True
            self.TRIPS = False
            self.PAIR = False
        else:
            self.FH = False

    def _rank_count(self):
        ranks = {}
        for c in self.card_set:
            if c.rank in ranks:
                ranks[c.rank] += 1
            else:
                ranks[c.rank] = 1
        return ranks
Ejemplo n.º 20
0
 def __init__(self, card_set=None):
     if card_set is None:
         self.card_set = CardSet()
     else:
         self.card_set = card_set
Ejemplo n.º 21
0
    def test_remove(self):
        # case 1: removing a card object
        h3 = Card('h3')
        h4 = Card('h4')
        h5 = Card('h5')
        group = CardSet([h3, h4, h5])
        card2remove = h3
        group.remove(card2remove)
        self.assertTrue(group == CardSet([h4, h5]))
        self.assertFalse(group.contains(h3))
        self.assertFalse(h3.isin(group))

        # case 2: removing a card_set object
        group = CardSet([h3, h4, h5])
        to_remove = CardSet([h3, h4])
        group.remove(to_remove)
        self.assertTrue(group == CardSet(h5))
Ejemplo n.º 22
0
import time
from itertools import product, combinations

import basic_lut
from card import Card
from cardset import CardSet
from evaluator import FlopEvaluator
from tools import STR_RANKS, STR_SUITS

c1 = Card('Ah')
c2 = Card('Kh')
c3 = Card('Qh')
c4 = Card('Jh')
c5 = Card('Th')

cset = CardSet([c4, c2, c3, c1, c5])
print "{0:b}".format(cset.get_bin())
flop_eval = FlopEvaluator(cset)

print cset

print flop_eval.get_made()

m = {
    "Straight Flush": 0,
    "Flush": 0,
    "Straight": 0,
    "Quads": 0,
    "Full House": 0,
    "Trips": 0,
    "Two Pair": 0,