def _sample(self, player_num, sim_cycle, cards): start = time.time() common = cards[2:] sampled_common_count = 5 - len(common) deck_cards = model.Deck(*cards).cards win, tie, lose = 0, 0, 0 others_count = player_num - 1 sampled_count = sampled_common_count + others_count * 2 win_by, beaten_by = [0] * len(model.Hand), [0] * len(model.Hand) while time.time() - start < sim_cycle: sampled_cards = tuple(random.sample(deck_cards, sampled_count)) sampled_common = sampled_cards[:sampled_common_count] my_cards = cards + sampled_common my_hand = self._manager.find_best_hand(my_cards) others_cards = sampled_cards[sampled_common_count:] result, hand = self._eval_showdown(my_hand, common + sampled_common, others_cards) if result == -1: beaten_by[hand] += 1 lose += 1 elif result == 0: tie += 1 else: win_by[my_hand.hand] += 1 win += 1 return SimulationResult(win, tie, lose, win_by, beaten_by)
def all_cards(): deck = model.Deck() while True: card = deck.pop() if card: yield card else: break
def simulate(self, player_num, *cards): assert isinstance(player_num, int) if player_num != 2: raise ValueError('Only 2 players are supported') unknown_count = 7 - len(cards) deck = model.Deck(*cards) deck_cards = deck.cards if unknown_count: fc = functools.partial(self._process, cards) combinations = model.Card.all_combinations(deck_cards, unknown_count) return self._simulate_parallel(fc, combinations) else: return self._simulate_river(cards)
def _simulate_river(self, cards): common = cards[2:] deck = model.Deck(*cards) deck_cards = deck.cards best_hand = self._manager.find_best_hand(cards) beaten_by, win_by = [0] * len(model.Hand), [0] * len(model.Hand) win, tie, lose = 0, 0, 0 for opponent in model.Card.all_combinations(deck_cards, 2): opponent_cards = opponent + common opponent_best = self._manager.find_best_hand( opponent_cards, min_hand=best_hand.hand) if not opponent_best or best_hand > opponent_best: win += 1 elif best_hand < opponent_best: beaten_by[opponent_best.hand] += 1 lose += 1 elif best_hand == opponent_best: tie += 1 win_by[best_hand.hand] = win return SimulationResult(win, tie, lose, win_by, beaten_by)
def test_pickle(self): orig = model.Deck() orig.shuffle() dump = pickle.dumps(orig) loaded = pickle.loads(dump) self.assertEqual(orig, loaded)
def test_shuffle(self): deck = model.Deck() deck.shuffle() print(deck)