def compare_cards(cls, cards1, cards2): """ Args: cards1: cards2: Returns: """ if len(cards1) == len(cards2) and len(cards1) == 4: pattern1 = cls.fourcards2pattern(cards1) pattern2 = cls.fourcards2pattern(cards2) if pattern1[5] != pattern2[5]: return pattern1[5] - pattern2[5] else: cards1.sort(key=cmp_to_key(FiveCardStudPokerCard.compare)) cards2.sort(key=cmp_to_key(FiveCardStudPokerCard.compare)) return FiveCardStudPokerCard.compare(cards1[-1], cards2[-1]) elif len(cards1) == len(cards2) and len(cards1) == 5: pattern1 = cls.cards2pattern(cards1) pattern2 = cls.cards2pattern(cards2) if pattern1[5] != pattern2[5]: return pattern1[5] - pattern2[5] else: cards1.sort(key=cmp_to_key(FiveCardStudPokerCard.compare)) cards2.sort(key=cmp_to_key(FiveCardStudPokerCard.compare)) return FiveCardStudPokerCard.compare(cards1[-1], cards2[-1]) else: raise ValueError( "len(cards1)%d, and len(cards2)%d are same and are 4 or 5 " % (len(cards1), len(cards2)))
def testCase(self): cards =[FiveCardStudPokerCard("3_Spade"), FiveCardStudPokerCard("4_Spade"),FiveCardStudPokerCard("5_Spade"),FiveCardStudPokerCard("6_Spade")] pattern = FiveCardStudEnv.fourcards2pattern(cards) assert(pattern[0] =="Straight_SameSuit") cards = [FiveCardStudPokerCard("3_Spade"), FiveCardStudPokerCard("4_Spade"), FiveCardStudPokerCard("5_Spade"), FiveCardStudPokerCard("6_Spade")]
def choose_player_at_begining_of_round(cls, public_state): """ Args: public_state: Returns: """ round = public_state.round if round in [1, 2, 3]: public_cards = None if round == 1: public_cards = public_state.second_hand_cards elif round == 2: public_cards = public_state.third_hand_cards elif round == 3: public_cards = public_state.fourth_hand_cards max_id = 0 for i in range(public_state.num_normal_players): if public_state.is_quit[i] == False: max_id = i break max_card = public_cards[max_id] for i in range(1, public_state.num_normal_players): if FiveCardStudPokerCard.compare( max_card, public_cards[i] ) < 0 and public_state.is_quit[i] == False: max_card = public_cards[i] max_id = i return max_id elif round == 4: max_cards = [public_state.second_hand_cards[0], public_state.third_hand_cards[0],\ public_state.fourth_hand_cards[0], public_state.fifth_hand_cards[0]] max_id = 0 for i in range(1, public_state.num_normal_players): tmp = [public_state.second_hand_cards[i], public_state.third_hand_cards[i], \ public_state.fourth_hand_cards[i], public_state.fifth_hand_cards[i]] if FiveCardStudEnv.compare_cards(max_cards, tmp) < 0: max_cards = tmp max_id = i return max_id else: raise ValueError("pulic_state.round(%d) not in [1,2,3,4]" % (public_state.turn))
def init(self, params=dict()): ''' Initialize FiveCardStud game enviroment with the params. The params are as follows:\n 1. param_num_normal_players denotes how many players join in this game, default 3\n 2. param_backward_enable denotes whether the environment will stores all history information. If you need call the backward function, please set it to bet True. default False\n 3. param_start_turn: The param_start_turn is the id of a normal player, who is the first to take an action. \n 3. param_initialization_chips denotes the initialization chips of players, default [500,500,500]\n 4. param_floor_bet denotes the minimal bet, default 10\n An example of params is {"param_num_normal_players":3,"param_initialization_chips":[500,500,500]}\n :param params: initialization param :return: infos, public_state, person_states, private_state ''' self.logger = roomai.get_logger() self.public_state = FiveCardStudPublicState() self.private_state = FiveCardStudPrivateState() if "param_num_normal_players" in params: self.public_state.__param_num_normal_players__ = params[ "param_num_normal_players"] else: self.public_state.__param_num_normal_players__ = 3 self.logger.info("param_num_normal_players is %d" % (self.public_state.__param_num_normal_players__)) if "param_backward_enable" in params: self.public_state.__param_backward_enable__ = params[ "param_backward_enable"] else: self.public_state.__param_backward_enable__ = False self.logger.info("param_backward_enable is " + str(self.public_state.param_backward_enable)) if "param_start_turn" in params: self.public_state.__param_start_turn__ = params["param_start_turn"] else: self.public_state.__param_start_turn__ = int( random.random() * self.public_state.param_num_normal_players) self.logger.info("param_start_turn is %d" % (self.public_state.param_start_turn)) if "param_initialization_chips" in params: self.public_state.__param_initialization_chips__ = params[ "param_initialization_chips"] else: self.public_state.__param_initialization_chips__ = [ 500 for i in range(self.public_state.param_num_normal_players) ] self.logger.info("param_initialization_chips is [%s]" % (",".join( str(i) for i in self.public_state.__param_initialization_chips__))) if "param_floor_bet" in params: self.public_state.__param_floor_bet__ = params["param_floor_bet"] else: self.public_state.__param_floor_bet__ = 10 allcards = [] for i in range(13): for j in range(4): allcards.append(FiveCardStudPokerCard(i, j)) random.shuffle(allcards) self.person_states = [ FiveCardStudPersonState() for i in range(self.public_state.param_num_normal_players + 1) ] ## private_state self.private_state.all_hand_cards = allcards[0:5 * self.public_state. param_num_normal_players] ## public_state self.public_state.chips = list( self.public_state.param_initialization_chips) self.public_state.second_hand_cards = self.private_state.all_hand_cards[ 1 * self.public_state.param_num_normal_players:2 * self.public_state.param_num_normal_players] self.public_state.floor_bet = self.public_state.param_floor_bet self.public_state.upper_bet = min(self.public_state.chips) #print "public_state.upper_bet", self.public_state.upper_bet,"chips", self.public_state.chips self.public_state.bets = [ self.public_state.floor_bet for i in range(self.public_state.param_num_normal_players) ] self.public_state.chips = [ self.public_state.chips[i] - self.public_state.floor_bet for i in range(self.public_state.param_num_normal_players) ] self.public_state.max_bet_sofar = self.public_state.floor_bet self.public_state.is_quit = [ False for i in range(self.public_state.param_num_normal_players) ] self.public_state.num_quit = 0 self.public_state.is_needed_to_action = [ True for i in range(self.public_state.param_num_normal_players) ] self.public_state.num_needed_to_action = self.public_state.param_num_normal_players self.public_state.is_raise = [ False for i in range(self.public_state.param_num_normal_players) ] self.public_state.num_raise = 0 self.public_state.round = 1 self.public_state.__turn__ = FiveCardStudEnv.__choose_player_at_begining_of_round__( self.public_state) self.public_state.__is_terminal__ = False self.public_state.__scores__ = None ## person_state for i in range(self.public_state.param_num_normal_players): self.person_states[i].__id__ = i self.person_states[ i].first_hand_card = self.private_state.all_hand_cards[i] self.person_states[ i].second_hand_card = self.private_state.all_hand_cards[ self.public_state.param_num_normal_players + i] self.person_states[ self.public_state. param_num_normal_players].__id__ = self.public_state.param_num_normal_players turn = self.public_state.turn self.person_states[ turn].__available_actions__ = FiveCardStudEnv.available_actions( self.public_state, self.person_states[turn]) self.__gen_state_history_list__() infos = self.__gen_infos__() FiveCardStudEnv.__valid_initialization_params__(self) return infos, self.public_state, self.person_states, self.private_state
def init(self, params=dict()): ''' Initialize FiveCardStud game enviroment with the params. The params are as follows: 1) num_normal_players denotes how many players join in this game, default 3 2) chips denotes the initialization chips of players, default [500,500,500] 3) floor_bet denotes the minimal bet, default 10 4) backward_enable denotes whether the environment will stores all history information. If you need call the backward function, please set it to bet True. default False An example of params is {"num_normal_players":3,"chips":[500,500,500]} :param params: initialization param :return: infos, public_state, person_states, private_state ''' self.logger = roomai.get_logger() self.__params__ = dict() if "num_normal_players" in params: self.__params__["num_normal_players"] = params[ "num_normal_players"] else: self.__params__["num_normal_players"] = 3 if "chips" in params: self.__params__["chips"] = params["chips"] else: self.__params__["chips"] = [ 500 for i in range(self.__params__["num_normal_players"]) ] if "floor_bet" in params: self.__params__["floor_bet"] = params["floor_bet"] else: self.__params__["floor_bet"] = 10 if "backward_enable" in params: self.__params__["backward_enable"] = params["backward_enable"] else: self.__params__["backward_enable"] = False allcards = [] for i in range(13): for j in range(4): allcards.append(FiveCardStudPokerCard(i, j)) random.shuffle(allcards) FiveCardStudEnv.__valid_initialization_params__(self) self.public_state = FiveCardStudPublicState() self.private_state = FiveCardStudPrivateState() self.person_states = [ FiveCardStudPersonState() for i in range(self.__params__["num_normal_players"] + 1) ] self.public_state_history = [] self.private_state_history = [] self.person_states_history = [] ## private_state self.private_state.all_hand_cards = allcards[ 0:5 * self.__params__["num_normal_players"]] ## public_state self.public_state.num_normal_players = self.__params__[ "num_normal_players"] self.public_state.chips = self.__params__["chips"] self.public_state.second_hand_cards = self.private_state.all_hand_cards[ 1 * self.__params__["num_normal_players"]:2 * self.__params__["num_normal_players"]] self.public_state.floor_bet = self.__params__["floor_bet"] self.public_state.upper_bet = min(self.public_state.chips) #print "public_state.upper_bet", self.public_state.upper_bet,"chips", self.public_state.chips self.public_state.bets = [ self.public_state.floor_bet for i in range(self.__params__["num_normal_players"]) ] self.public_state.chips = [ self.public_state.chips[i] - self.public_state.floor_bet for i in range(self.__params__["num_normal_players"]) ] self.public_state.max_bet_sofar = self.public_state.floor_bet self.public_state.is_quit = [ False for i in range(self.__params__["num_normal_players"]) ] self.public_state.num_quit = 0 self.public_state.is_needed_to_action = [ True for i in range(self.__params__["num_normal_players"]) ] self.public_state.num_needed_to_action = self.__params__[ "num_normal_players"] self.public_state.is_raise = [ False for i in range(self.__params__["num_normal_players"]) ] self.public_state.num_raise = 0 self.public_state.round = 1 self.public_state.__turn__ = FiveCardStudEnv.__choose_player_at_begining_of_round__( self.public_state) self.public_state.__is_terminal__ = False self.public_state.__scores__ = None ## person_state for i in range(self.__params__["num_normal_players"]): self.person_states[i].__id__ = i self.person_states[ i].first_hand_card = self.private_state.all_hand_cards[i] self.person_states[ i].second_hand_card = self.private_state.all_hand_cards[ self.__params__["num_normal_players"] + i] self.person_states[ self.__params__["num_normal_players"]].__id__ = self.__params__[ "num_normal_players"] turn = self.public_state.turn self.person_states[ turn].__available_actions__ = FiveCardStudEnv.available_actions( self.public_state, self.person_states[turn]) self.__gen_state_history_list__() infos = self.__gen_infos__() return infos, self.public_state, self.person_states, self.private_state