class RelativeRank: def __init__(self,config): self.table = PersistentDict(config.get('Search','relative_rank'),mode=0666) #convert hands to relative hands def relative_hands(self,game_state): hands = {} for player,hand in game_state.hands.iteritems(): hands[player] = [self.relative_rank(game_state,card) for card in hand] return hands #find the relative rank by applying the state_of_suit binary number generator def relative_rank(self,game_state, card): state_of_suit = game_state.get_state_of_suit(card.suit) return Card(card.suit,self.get_relative_rank(state_of_suit,card.value)) def get_relative_rank(self,state,card): return self.table[(state<<4)+(card-2)] def fillTable(self): for i in range(1,8192): for j in range(0,13): if i & (1<<j): rank = 0 for x in range(0,j): if i & (1<<x): rank += 1 self.table[(i<<4)+j] = rank def close(self): self.table.close()
def __init__(self, config, rank): self.rank = rank self.table = PersistentDict(config.get('Search', 'single_suit'), mode=0666)
class SingleSuit: def __init__(self, config, rank): self.rank = rank self.table = PersistentDict(config.get('Search', 'single_suit'), mode=0666) def single_suit_analysis(self, game_state): hands = self.rank.relative_hands(game_state) player = game_state.get_next_player() try: return self.table[(repr(hands), player)] except KeyError: value_ind = self.sure_tricks(hands, player) value_coop = self.sure_tricks_coop(hands, player) value = max(value_ind, value_coop) self.table[(repr(hands), player)] = value return value def sure_tricks(self, hands, player): """ this assumes that it is being called on the first player in a hand it also does not take into account a cooperative partner """ #my hand players_hand = hands[player] #everyone else's hand others = [x for x in Player.POSITION if x != player] other_hands = [hands[x] for x in others] def reduce_suit(suit): value = lambda x: x.value def max_suit(hand): try: return max(filter(lambda x: x.suit == suit, hand), key=value) except ValueError: return Card(suit=suit, value=0) player_suit = [x for x in players_hand if x.suit == suit] #gets a list of the max card for each other player that is in the desired suit other_max = map(lambda y: max_suit(y), other_hands) #counts the number of declarer cards which are larger than all other players max card return len( filter(lambda x: x == max(other_max + [x], key=value), player_suit)) return reduce(lambda x, y: x + reduce_suit(y), Suit.SUITS, 0) def sure_tricks_coop(self, hands, player): """ this assumes that it is being called on the first player in a hand it also does not take into account a cooperative partner """ #my hand players_hand = hands[player] #my partner's hand partners_hand = hands[Player.NEXT[Player.NEXT[player]]] #my opponent's hands others = [ x for x in Player.POSITION if x != player and x != Player.NEXT[Player.NEXT[player]] ] other_hands = [hands[x] for x in others] def reduce_suit(suit): value = lambda x: x.value def max_suit(hand): try: return max(filter(lambda x: x.suit == suit, hand), key=value) except ValueError: return Card(suit=suit, value=0) #gives me the cards in the player's suit player_suit = [x for x in players_hand if x.suit == suit] #gives me the cards in the partner's suit partner_suit = [x for x in partners_hand if x.suit == suit] #count my partner's cards, my cards playerNumCards = len(player_suit) partnerNumCards = len(partner_suit) #this is the theoretical max of sure tricks in this suit upperBound = min(partnerNumCards, playerNumCards) #gets the max card of all other players in the desired suit other_max = map(lambda y: max_suit(y), other_hands) #counts the number of declarer/partner cards which are larger than all opponent's max numBetterCards = len( filter(lambda x: x == max(other_max + [x], key=value), player_suit)) + len( filter( lambda x: x == max(other_max + [x], key=value), partner_suit)) return min(numBetterCards, upperBound) return reduce(lambda x, y: x + reduce_suit(y), Suit.SUITS, 0) def close(self): self.rank.close() self.table.close()
def __init__(self,config): self.table = PersistentDict(config.get('Search','relative_rank'),mode=0666)
def __init__(self,config,rank): self.rank = rank self.table = PersistentDict(config.get('Search','single_suit'),mode=0666)
class SingleSuit: def __init__(self,config,rank): self.rank = rank self.table = PersistentDict(config.get('Search','single_suit'),mode=0666) def single_suit_analysis(self,game_state): hands = self.rank.relative_hands(game_state) player = game_state.get_next_player() try: return self.table[(repr(hands),player)] except KeyError: value_ind = self.sure_tricks(hands,player) value_coop = self.sure_tricks_coop(hands,player) value = max(value_ind,value_coop) self.table[(repr(hands),player)] = value return value def sure_tricks(self,hands,player): """ this assumes that it is being called on the first player in a hand it also does not take into account a cooperative partner """ #my hand players_hand = hands[player] #everyone else's hand others = [x for x in Player.POSITION if x != player] other_hands = [hands[x] for x in others] def reduce_suit(suit): value = lambda x:x.value def max_suit(hand): try: return max(filter(lambda x:x.suit == suit,hand),key=value) except ValueError: return Card(suit=suit,value=0) player_suit = [x for x in players_hand if x.suit == suit] #gets a list of the max card for each other player that is in the desired suit other_max = map(lambda y: max_suit(y),other_hands) #counts the number of declarer cards which are larger than all other players max card return len(filter(lambda x: x == max(other_max + [x], key=value), player_suit)) return reduce(lambda x,y: x+reduce_suit(y), Suit.SUITS, 0) def sure_tricks_coop(self,hands,player): """ this assumes that it is being called on the first player in a hand it also does not take into account a cooperative partner """ #my hand players_hand = hands[player] #my partner's hand partners_hand = hands[Player.NEXT[Player.NEXT[player]]] #my opponent's hands others = [x for x in Player.POSITION if x != player and x !=Player.NEXT[Player.NEXT[player]]] other_hands = [hands[x] for x in others] def reduce_suit(suit): value = lambda x:x.value def max_suit(hand): try: return max(filter(lambda x:x.suit == suit,hand),key=value) except ValueError: return Card(suit=suit,value=0) #gives me the cards in the player's suit player_suit = [x for x in players_hand if x.suit == suit] #gives me the cards in the partner's suit partner_suit = [x for x in partners_hand if x.suit == suit] #count my partner's cards, my cards playerNumCards = len(player_suit) partnerNumCards = len(partner_suit) #this is the theoretical max of sure tricks in this suit upperBound = min(partnerNumCards, playerNumCards) #gets the max card of all other players in the desired suit other_max = map(lambda y: max_suit(y),other_hands) #counts the number of declarer/partner cards which are larger than all opponent's max numBetterCards = len(filter(lambda x: x == max(other_max + [x], key=value), player_suit))+len(filter(lambda x: x == max(other_max + [x], key=value), partner_suit)) return min(numBetterCards, upperBound) return reduce(lambda x,y: x+reduce_suit(y), Suit.SUITS, 0) def close(self): self.rank.close() self.table.close()