Last active
August 19, 2018 04:59
-
-
Save wesky93/fb435c961049169153e138a4073d33a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from random import choice | |
from typing import Dict, List, Set | |
from .core.cards import Card | |
from .player import Other | |
logger = logging.getLogger(__name__) | |
merge_cards = lambda my, community: my + community | |
def analysis_cards(all: List[Card]) -> Dict[int, Set[int]]: | |
cards = dict() | |
for card in all: | |
if card.suit.value in cards.keys(): | |
cards[card.suit.value].add(card.rank.value) | |
else: | |
cards[card.suit.value] = {card.rank.value} | |
return cards | |
all_rank_index = { | |
'same': { | |
}, | |
'different': { | |
} | |
} | |
hold_rank_index = { | |
'same': { | |
1: [{14, 13}], | |
2: [{14, 12}, {14, 11}, {13, 12}], | |
3: [{14, 10}, {13, 11}, {12, 11}, {11, 10}], | |
4: [{13, 10}, {12, 10}, {11, 9}, {10, 9}, {9, 8}], | |
5: [{14, n} for n in range(2, 10)] + [{12, 9}, {10, 8}, {9, 7}, {8, 7}, {7, 6}], | |
6: [{13, 9}, {11, 8}, {8, 6}, {7, 5}, {5, 4}], | |
7: [{13, n} for n in range(2, 9)] + [{12, 8}, {10, 7}, {6, 4}, {5, 3}, {4, 3}], | |
8: [{11, 7}, {9, 6}, {8, 5}, {7, 4}, {4, 2}, {3, 2}], | |
}, | |
'different': { | |
1: [(14, 14), (13, 13), (12, 12), (11, 11)], | |
2: [(10, 10), (14, 13)], | |
3: [(9, 9), (14, 12), (14, 12)], | |
4: [(8, 8), (14, 11), (13, 12)], | |
5: [(7, 7), (13, 11), (12, 11), (11, 10)], | |
6: [(6, 6), (5, 5), (14, 10), (13, 10), (12, 10)], | |
7: [(4, 4), (3, 3), (2, 2), (11, 9), (10, 9), (9, 8)], | |
8: [(12, 9), (11, 8), (10, 8), (8, 7), (7, 6), (6, 5), (5, 4)], | |
} | |
} | |
def hold_rank(cards: dict): | |
if len(cards.keys()) == 1: | |
same_cards = list(cards.values())[0] | |
indexes = hold_rank_index['same'] | |
for rank, indxs in zip(indexes.keys(), indexes.values()): | |
for i in indxs: | |
if same_cards == i: | |
return rank | |
# 다른 카드 | |
else: | |
c1, c2 = [list(i)[0] for i in cards.values()] | |
indexes = hold_rank_index['same'] | |
for rank, indxs in zip(indexes.keys(), indexes.values()): | |
for i in indxs: | |
if (c1, c2) == i or (c2, c1) == i: | |
return rank | |
# 내가 가진 패의 등급 | |
def check_round(community): | |
c = len(community) | |
if c == 0: | |
return 1 | |
elif c == 3: | |
return 2 | |
elif c == 4: | |
return 3 | |
else: | |
return 4 | |
def racord_rank(): | |
result = {} | |
while True: | |
in_rank = yield result | |
if result.get(str(in_rank)): | |
result[str(in_rank)] += 1 | |
else: | |
result[str(in_rank)] = 1 | |
status_same = { | |
1: [{10, 11, 12, 13, 14}], | |
2: [{3, 4, 5, 6, 7}], | |
5: [{2, 5, 6, 8, 11}], | |
} | |
status_different = { | |
3: [[{n}, {n}, {n}, {n}] for n in range(2, 10)], | |
7: [[{n}, {n}, {n}, ] for n in range(2, 15)], | |
} | |
def check_in(diff, cards): | |
for k, v in zip(cards.keys(), cards.values()): | |
if diff <= v: | |
return True, k | |
return False, None | |
def check_status(cards): | |
for v in cards.values(): | |
for rank, sames in zip(status_same.keys(), status_same.values()): | |
for same in sames: | |
if same in v: | |
return rank | |
for rank, differents in zip(status_different.keys(), status_different.values()): | |
for diff in differents: | |
already = [] | |
if len(cards.keys()) >= len(diff): | |
check = diff.copy() | |
for d in diff: | |
result, i = check_in(d, cards) | |
if result and i not in already: | |
check.pop() | |
already.append(i) | |
if len(check) == 0: | |
print('랭크', rank) | |
return rank | |
racord = racord_rank() | |
next(racord) | |
def process(my_chips: int, my_cards: List[Card], bet_players: List[Other], betting_players: List[Other], | |
community_cards: List[Card], min_bet_amt: int, max_bet_amt: int, total_bet_amt: int): | |
all_in = min(my_chips, max_bet_amt) | |
rounds = check_round(community_cards) | |
my = analysis_cards(my_cards) | |
if my_chips >= min_bet_amt: | |
my_rank = hold_rank(my) | |
all_cards = analysis_cards(merge_cards(my_cards, community_cards)) | |
status = check_status(all_cards) | |
if status is not None and status == 1: | |
print(status) | |
return all_in | |
if (status is None or my_rank is None) and min_bet_amt >= 40: | |
return choice([0, min_bet_amt, min_bet_amt, min_bet_amt]) | |
if rounds == 1: | |
return min_bet_amt | |
elif rounds == 2: | |
if my_rank is not None: | |
if status is not None and status <= 2: | |
chips = sorted([p.chips for p in betting_players] + [my_chips], reverse=True) | |
i = chips.index(my_chips) | |
if i <= 2: | |
return max(all_in / 2, min_bet_amt) | |
elif my_rank <= 2: | |
return max(int(max_bet_amt / 4), min_bet_amt) | |
elif my_rank <= 4: | |
return max(int(max_bet_amt / 8), min_bet_amt) | |
return min_bet_amt | |
return min_bet_amt | |
elif rounds == 3: | |
if (status is not None and status <= 5) or (my_rank is not None and my_rank <= 3): | |
chips = sorted([p.chips for p in betting_players] + [my_chips], reverse=True) | |
if chips.index(my_chips) <= 2: | |
return max(all_in / 2, min_bet_amt) | |
return min_bet_amt | |
else: | |
if status is not None: | |
if status <= 2: | |
return max(min_bet_amt, all_in / 2) | |
return min_bet_amt | |
else: | |
return 0 | |
def bet(*args, **kwargs) -> int: | |
try: | |
result = process(*args, **kwargs) | |
except Exception as e: | |
print(e) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment