def __choose_player_with_lowest_card__(self): min_card = self.person_states[0].hand_cards[0] min_playerid = 0 for playerid in range(self.__params__["num_normal_players"]): for c in self.person_states[playerid].hand_cards: if SevenKingPokerCard.compare(min_card, c) > 0: min_card = c min_playerid = playerid return min_playerid, min_card
def __is_action_valid__(cls, action, public_state, person_state): license_action = public_state.license_action if len(action.cards) > 0 and license_action is not None and len(license_action.cards) > 0: max_action_card = action.cards[-1] max_previous_card = license_action.cards[-1] if SevenKingPokerCard.compare(max_action_card, max_previous_card) < 0: return False return True
def take_action(self): max_action = SevenKingAction.lookup("") max_pattern = 0 for a in self.available_actions.values(): if (a.pattern[1] > max_pattern): max_pattern = a.pattern[1] max_action = a elif (a.pattern[1] == max_pattern): if (a.pattern[0] != 'p_0' and (SevenKingPokerCard.compare( a.cards[-1], max_action.cards[-1]) > 0)): max_action = a return max_action
def take_action(self): min_card = None for a in self.available_actions.values(): if a.pattern[0] == "p_1": if min_card is None: min_card = a.cards[0] else: card = a.cards[0] if SevenKingPokerCard.compare(card, min_card) < 0: min_card = card if min_card is None: return list(self.available_actions.values())[0] else: return SevenKingAction.lookup(min_card.key)
def __add_card__(self, c): self.__hand_cards__.append(c) self.__hand_cards_keyset__.add(c.key) for j in range(len(self.__hand_cards)-1,0,-1): if SevenKingPokerCard.compare(self.__hand_cards__[j - 1], self.__hand_cards__[j]) > 0: tmp = self.__hand_cards__[j] self.__hand_cards__[j] = self.__hand_cards__[j-1] self.__hand_cards__[j-1] = tmp else: break self.__hand_cards_key = ",".join([c.key for c in self.__hand_cards__])
def choose_player_with_lowest_card(self): """ Returns: """ min_card = self.person_states[0].hand_cards[0] min_playerid = 0 for playerid in range(self.num_players): for c in self.person_states[playerid].hand_cards: if SevenKingPokerCard.compare(min_card, c) > 0: min_card = c min_playerid = playerid return min_playerid, min_card
def __add_card(self, c): self.__hand_cards.append(c) self.__hand_cards_set.add(c.key) for j in range(len(self.__hand_cards) - 1, 0, -1): if SevenKingPokerCard.compare(self.__hand_cards[j - 1], self.__hand_cards[j]) > 0: tmp = self.__hand_cards[j] self.__hand_cards[j] = self.__hand_cards[j - 1] self.__hand_cards[j - 1] = tmp else: break #self.__hand_cards.sort(cmp=SevenKingPokerCard.compare) self.__hand_cards_key = ",".join([c.key for c in self.__hand_cards])
def __add_cards__(self, cards): len1 = len(self.__hand_cards__) for c in cards: self.__hand_cards__.append(c) self.__hand_cards_keyset__.add(c.key) len2 = len(self.__hand_cards__) for i in range(len1,len2-1): for j in range(i,0,-1): if SevenKingPokerCard.compare(self.__hand_cards__[j-1], self.__hand_cards__[j]) > 0: tmp = self.__hand_cards__[j] self.__hand_cards__[j] = self.__hand_cards__[j-1] self.__hand_cards__[j-1] = tmp else: break #self.__hand_cards.sort(cmp=SevenKingPokerCard.compare) self.__hand_cards_key__ = ",".join([c.key for c in self.__hand_cards__])
def __gen_pointrank2cards__(self): if self.__hand_cards_key__ in AllPointRank2Cards: return AllPointRank2Cards[self.__hand_cards_key__] else: point2cards = dict() for c in self.hand_cards: pointrank = c.point_rank if pointrank not in point2cards: point2cards[pointrank] = [] point2cards[pointrank].append(c) for p in point2cards: for i in range(len(point2cards[p])-1): for j in range(i+1,len(point2cards[p])): if SevenKingPokerCard.compare(point2cards[p][i],point2cards[p][j]) > 0: tmp = point2cards[p][i] point2cards[p][i] = point2cards[p][j] point2cards[p][j] = tmp #point2cards[p].sort(cmp=SevenKingPokerCard.compare) AllPointRank2Cards[self.__hand_cards_key__] = point2cards return point2cards
def take_action(self): """ Returns: """ if "" not in self.available_actions: min_card = None for a in self.available_actions.values(): if a.pattern[0] == "p_0": if min_card is None: min_card = a.hand_card[0] else: card = a.hand_card[0] if SevenKingPokerCard.compare(card, min_card) < 0 : min_card = card if min_card is None: return self.available_actions.values()[0] else: return SevenKingAction.lookup(min_card.key) else: return SevenKingAction("")
def available_actions(cls, public_state, person_state): available_actions = dict() license_action = public_state.license_action if license_action is None: license_action = SevenKingAction("") hand_cards = person_state.hand_cards patterns = set() if license_action.pattern[0] == "p_0": for p in AllSevenKingPatterns.values(): if p[0] != "p_0": patterns.add(p) else: patterns.add(license_action.pattern) patterns.add(AllSevenKingPatterns["p_0"]) for pattern in patterns: if pattern[1] >= 2: point2cards = person_state.__gen_pointrank2cards__() if len(person_state.hand_cards) < pattern[1]: continue elif pattern[0] == "p_0": available_actions[""] = SevenKingAction.lookup("") elif pattern[0] == "p_1": license_pattern = license_action.pattern license_card = None if license_pattern[0] != "p_0": license_card = license_action.cards[-1] for c in person_state.hand_cards: if license_pattern[0] == "p_0" or SevenKingPokerCard.compare(c,license_card) >0: available_actions[c.key] = SevenKingAction.lookup(c.key) elif pattern[0] == "p_2": for p in point2cards: license_pattern = license_action.pattern license_card = None if license_pattern[0] != "p_0": #print license_action.key, license_action.pattern, license_pattern[0] != "p_0" license_card = license_action.cards[-1] len1 = len(point2cards[p]) if len1 == 2: if license_pattern[0] == "p_0" or SevenKingPokerCard.compare(point2cards[p][1], license_card) > 0: str = "%s,%s" % (point2cards[p][0].key, point2cards[p][1].key) available_actions[str] = SevenKingAction.lookup(str) if len1 == 3: if license_pattern[0] == "p_0" or SevenKingPokerCard.compare(point2cards[p][1], license_card) > 0: str = "%s,%s" % (point2cards[p][0].key, point2cards[p][1].key) available_actions[str] = (SevenKingAction.lookup(str)) if license_pattern[0] == "p_0" or SevenKingPokerCard.compare(point2cards[p][2], license_card) > 0: str = "%s,%s" % (point2cards[p][0].key, point2cards[p][2].key) available_actions[str] = (SevenKingAction.lookup(str)) str = "%s,%s" % (point2cards[p][1].key, point2cards[p][2].key) available_actions[str] = (SevenKingAction.lookup(str)) if len1 == 4: if license_pattern[0] == "p_0" or SevenKingPokerCard.compare(point2cards[p][1], license_card) > 0: str = "%s,%s" % (point2cards[p][0].key, point2cards[p][1].key) available_actions[str] = (SevenKingAction.lookup(str)) if license_pattern[0] == "p_0" or SevenKingPokerCard.compare(point2cards[p][2], license_card) > 0: str = "%s,%s" % (point2cards[p][0].key, point2cards[p][2].key) available_actions[str] = (SevenKingAction.lookup(str)) str = "%s,%s" % (point2cards[p][1].key, point2cards[p][2].key) available_actions[str] = (SevenKingAction.lookup(str)) if license_pattern[0] == "p_0" or SevenKingPokerCard.compare(point2cards[p][3], license_card) > 0: str = "%s,%s" % (point2cards[p][0].key, point2cards[p][3].key) available_actions[str] = (SevenKingAction.lookup(str)) str = "%s,%s" % (point2cards[p][1].key, point2cards[p][3].key) available_actions[str] = (SevenKingAction.lookup(str)) str = "%s,%s" % (point2cards[p][2].key, point2cards[p][3].key) available_actions[str] = (SevenKingAction.lookup(str)) elif pattern[0] == "p_3": for p in point2cards: license_pattern = license_action.pattern license_card = None if license_pattern[0] != "p_0" : license_card = license_action.cards[-1] len1 = len(point2cards[p]) if len1 == 3: if license_pattern[0] == "p_0" or SevenKingPokerCard.compare(point2cards[p][2], license_card) > 0: str = "%s,%s,%s" % (point2cards[p][0].key, point2cards[p][1].key, point2cards[p][2].key) available_actions[str] = (SevenKingAction.lookup(str)) if len1 == 4: if license_pattern[0] == "p_0" or SevenKingPokerCard.compare(point2cards[p][2], license_card) > 0: str = "%s,%s,%s" % (point2cards[p][0].key, point2cards[p][1].key, point2cards[p][2].key) available_actions[str] = (SevenKingAction.lookup(str)) if license_pattern[0] == "p_0" or SevenKingPokerCard.compare(point2cards[p][3], license_card) > 0: str = "%s,%s,%s" % (point2cards[p][0].key, point2cards[p][1].key, point2cards[p][3].key) available_actions[str]=(SevenKingAction.lookup(str)) str = "%s,%s,%s" % (point2cards[p][0].key, point2cards[p][2].key, point2cards[p][3].key) available_actions[str]=(SevenKingAction.lookup(str)) str = "%s,%s,%s" % (point2cards[p][1].key, point2cards[p][2].key, point2cards[p][3].key) available_actions[str]=(SevenKingAction.lookup(str)) elif pattern[0] == "p_4": for p in point2cards: license_pattern = license_action.pattern license_card = None if license_pattern[0] != "p_0" : license_card = license_action.cards[-1] len1 = len(point2cards[p]) if len1 >= 4: if license_pattern[0] == "p_0" or SevenKingPokerCard.compare(point2cards[p][3], license_card) > 0: str = "%s,%s,%s,%s" % ( point2cards[p][0].key, point2cards[p][1].key, point2cards[p][2].key, point2cards[p][3].key ) available_actions[str]=(SevenKingAction.lookup(str)) if pattern[0] != "p_0" and pattern[0] != "p_1" and\ pattern[0] != "p_2" and pattern[0] != "p_3" and pattern[0] != "p_4": raise ValueError("The %s pattern is invalid" % (pattern[0])) #for a in available_actions.values(): # if SevenKingEnv.__is_action_valid__(a,public_state,person_state) == False: # del available_actions[a.key] return available_actions