def pull_card_from_pile_top(self): self.turn_output['pull_source'] = 'pile' n_cards = len(self.pile_top_cards) accessible_cards = self.pile_top_cards if 1 == n_cards: self.chosen_from_pile_top = self.pile_top_cards[0] return self.pile_top_cards[0] elif 2 == n_cards: # both card are accessible pass else: accessible_cards = pile_top_accessible_cards(self.pile_top_cards) # ======== here we will need to introduce strategy of best card to choose ============ player_cards = self.turn_player.cards_in_hand cards_plus_player_collective_points_ = pile_cards_plus_player_collective_hypothetical_points(accessible_cards, player_cards) if bool(cards_plus_player_collective_points_): this_card = next(iter(cards_plus_player_collective_points_)) else: if len(accessible_cards) == 1: this_card = accessible_cards[0] else: this_card = sorted(accessible_cards, key=lambda card: card_to_value(card), reverse=False)[0] # print(self.number, self.turn_number, self.pile_top_cards, accessible_cards, player_cards, cards_plus_player_collective_points_, this_card) # ===================================== self.pile_top_cards = list(set(self.pile_top_cards) - set([this_card])) self.chosen_from_pile_top = this_card return this_card
def sum_hand_points(self): '''Calculates the sum of point in a hand ''' self.hand_points = 0 for card in self.cards_in_hand: self.hand_points += card_to_value(card)
def pull_card(self, name, pick_from_deck=None): player = self.players[name] self.turn_player = player self.chosen_from_pile_top = None if 'human' == player.agent: options = {'1': 'deck'} #, '2': 'pile': } accessible_cards = pile_top_accessible_cards(self.pile_top_cards) for option_id, card in enumerate(accessible_cards, 2): options[f'{option_id}'] = card option_id = self.io_options(options, player, option_type='pull') if '1' == option_id: this_card = self.pull_card_from_deck() source = 'deck' else: this_card = options[option_id] source = 'pile' print(f'You chose {this_card} from the {source}') else: if pick_from_deck is not None: if pick_from_deck == True: pull_card_function = self.pull_card_from_deck else: pull_card_function = self.pull_card_from_pile_top else: # ======== need to devise better strategy =========== #pull_card_function = np.random.choice([self.pull_card_from_deck, self.pull_card_from_pile_top]) card_values = [card_to_value(card) for card in self.pile_top_cards] idx_lowest = np.array(card_values).argmin() if player.strategy["pile_pull"]["highest_card_value_to_pull"] >= card_values[idx_lowest]: pull_card_function = self.pull_card_from_pile_top else: pull_card_function = self.pull_card_from_deck # overriding previous decision, because the deck is empty ... if len(self.round_deck) == 0: pull_card_function = self.pull_card_from_pile_top this_card = pull_card_function() # ================================================== self.turn_output['pulls'] = this_card player.cards_in_hand.append(this_card) player.sum_hand_points() player.remove_cards_from_unknown([this_card])
def basic_policy(observables, turn_number, seed=None, yaniv_thresh=None, throw_out='highest_combination', pickup='random', deck_prob=0.5): assert throw_out in ['highest_card', 'highest_combination', 'random_card'] assert pickup in ['random', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] if yaniv_thresh is None: yaniv_thresh = YANIV_LIMIT n_deck = observables[0] top_accessible_cards = observables[1] cards_in_hand = observables[2] n_cards_opponent = observables[3] hand_sum = cards_to_value_sum(cards_in_hand) yaniv_call = 0 if hand_sum <= yaniv_thresh: # always call Yaniv yaniv_call = 1 # continuing just in case the call was wrong ... # throwing out highest value card (not getting rid of cards yet ...) cards_in_hand_sorted = sort_cards(cards_in_hand, descending=True) np.random.seed(seed) if 'highest_card' == throw_out: cards_to_throw = [cards_in_hand_sorted[0]] elif 'highest_combination': cards_to_throw = cards_to_best_combination(cards_in_hand_sorted) elif 'random_card' == throw_out: cards_to_throw = [np.random.choice(cards_in_hand_sorted)] # picking from deck at random if 'random' == pickup: pick_from_deck = np.random.binomial(1, deck_prob) else: card_values = [card_to_value(card) for card in top_accessible_cards] idx_lowest = np.array(card_values).argmin() if pickup >= card_values[idx_lowest]: pick_from_deck = 0 else: pick_from_deck = 1 return (yaniv_call, cards_to_throw, pick_from_deck)
def observables_to_model_input(observables, turn_number, reshape=True): n_deck = observables[0] top_accessible_cards = observables[1] cards_in_hand = observables[2] n_cards_opponent = observables[3] card_values = [card_to_value(card) for card in top_accessible_cards] idx_lowest = np.array(card_values).argmin() card_value_lowest = card_values[idx_lowest] input_ = np.array((n_deck, card_value_lowest, turn_number, len(cards_in_hand), n_cards_opponent)) if reshape: input_ = input_.reshape(1, -1) return input_
def prob_lowest_hand(self, name): player_i = self.players[name] hand_sum_i = player_i.hand_points # was hand_points cards_unknown = player_i.unknown_cards cards_unknown_values = [card_to_value(card) for card in cards_unknown] #print(cards_unknown_values) prob_lowest = 1. for name_j, player_j in self.players.items(): if name_j != name: prob_Hi_lt_Hj = self.calculate_prob_Hi_lt_Hj(hand_sum_i, player_j, cards_unknown_values) prob_lowest *= prob_Hi_lt_Hj if self.verbose >= 3: print('~' * 10) print(hand_sum_i, player_i.cards_in_hand) print(f"The probability for {name} to make a successful Yaniv decleration is: {100. * prob_lowest:0.1f}%") return prob_lowest
def test(self): for suite in SUITE_CHAR_TO_SYMBOL.keys(): for rank in RANKS: card = '{}{}'.format(rank, suite) self.assertEqual(card_to_value(card), rank_to_value(rank))