def game_start(self): self.set_game_env_test() #self.human_agent_battle() order_player_ids = self.get_player_order() env = Env() k = 0 with tf.Session() as sess: o_saver = tf.train.import_meta_graph(config.O_NET_MODEL_META_PATH) o_model_file = tf.train.latest_checkpoint(config.O_NET_MODEL_PATH) l_saver = tf.train.import_meta_graph(config.L_NET_MODEL_META_PATH) l_model_file = tf.train.latest_checkpoint(config.L_NET_MODEL_PATH) u_saver = tf.train.import_meta_graph(config.U_NET_MODEL_META_PATH) u_model_file = tf.train.latest_checkpoint(config.U_NET_MODEL_PATH) net_out = tf.get_collection('net_out') lcts = None last_action = None # 当前获有牌权的玩家ID curr_master_id = order_player_ids[k] while not self._game_is_over: curr_player_id = order_player_ids[k] curr_player_role = self._players[curr_player_id].player_role # 进行主动出牌 if curr_player_id == curr_master_id: lcts = None last_action = None # 基于玩家角色选择不同的模型 if curr_player_role == PlayerRoleEnum.LAND_OWNER: o_saver.restore(sess, o_model_file) elif curr_player_role == PlayerRoleEnum.LOW_LAND_OWNER: l_saver.restore(sess, l_model_file) else: u_saver.restore(sess, u_model_file) # 当前玩家出牌 action, put_card, primary_item = self.put_card_process( sess, env, curr_player_id, net_out, lcts, last_action) if len(put_card) > 0: put_card_temp = [] time.sleep(self._sleep_time) for x in put_card: if x in CARD_MAP: put_card_temp.append(CARD_MAP[x]) else: put_card_temp.append(x) print('玩家[ID=%s]出牌 -> %s \n' % (curr_player_id, put_card_temp)) curr_master_id = order_player_ids[k] lcts = CardTypeStruct() lcts.card_type = action lcts.card_count = len(put_card) lcts.primary_item = primary_item last_action = action else: time.sleep(self._sleep_time) print('玩家[ID=%s]要不起 \n' % (curr_player_id)) # 轮到下一个玩家 k = (k + 1) % 3 time.sleep(self._sleep_time) print('Game over \n')
def game_start(self): self.set_game_env() order_player_ids = self.get_player_order() env = Env() k = 0 with tf.Session() as sess: saver = tf.train.import_meta_graph(config.MODEL_META_PATH) model_file = tf.train.latest_checkpoint(config.MODEL_PATH) saver.restore(sess, model_file) net_out = tf.get_collection('net_out') lcts = None last_action = None # 当前获有牌权的玩家ID curr_master_id = order_player_ids[k] t = 1 while not self._game_is_over: curr_player_id = order_player_ids[k] # 进行主动出牌 if curr_player_id == curr_master_id: lcts = None last_action = None # 当前玩家出牌 action, put_card, primary_item = self.put_card_process( sess, env, curr_player_id, net_out, lcts, last_action) if len(put_card) > 0: print('玩家[ID=%s]出牌 -> %s' % (curr_player_id, put_card)) curr_master_id = order_player_ids[k] lcts = CardTypeStruct() lcts.card_type = action lcts.card_count = len(put_card) lcts.primary_item = primary_item last_action = action else: print('玩家[ID=%s]要不起' % (curr_player_id)) # 轮到下一个玩家 time.sleep(5) k = (k + 1) % 3 t += 1 #if t == 10: # break print('Game over')
def __init__(self, player_id): # 玩家ID self._player_id = player_id # 玩家角色 self._player_role = PlayerRoleEnum.DEFAULT # 手牌结构 self._hand_card_struct = None # 要打出去的牌型 self._put_card_type = CardTypeStruct() # 要打出去的牌(无花色) self._put_card = list() # 要打出去的牌(有花色) self._put_color_card = list()
FOUR = "Four" BOMB = "Bomb" ROCKET = "Rocket" PASS = "******" """ Definition of the value network (O-Net, L-Net, U-Net) action """ POLICY_ACTION_DICT = dict() k = 0 # solo - kicker(No) - chain(No) - cardTypeStruct - card # quantity(15) for card_item in ALL_UNIQUE_CARD: POLICY_ACTION_DICT[k] = (Primal.SOLO, False, False, CardTypeStruct(ActionTypeEnum.ACTION_PUT_ONE, 1, card_item), [card_item]) k += 1 # solo - kicker(No) - chain(Yes) - cardTypeStruct - card # quantity(36) # Remove '2', 'S', 'B' all_card_tmp = ALL_UNIQUE_CARD[:-3] for card_count in range(5, 13, 1): action_type = ActionTypeEnum.ACTION_PUT_5_CONTINUE if card_count == 6: action_type = ActionTypeEnum.ACTION_PUT_6_CONTINUE elif card_count == 7: action_type = ActionTypeEnum.ACTION_PUT_7_CONTINUE elif card_count == 8: action_type = ActionTypeEnum.ACTION_PUT_8_CONTINUE elif card_count == 9: action_type = ActionTypeEnum.ACTION_PUT_9_CONTINUE
def is_one_hand(hand_card_seq): hand_card_status = HandCardUtils.obtain_hand_card_status(hand_card_seq) assert len( hand_card_status) == 18, 'the size of hand card status must be 18.' cts = CardTypeStruct() card_count = sum(hand_card_status) cts.card_count = card_count find_one = list( map(lambda x: x[0], filter(lambda x: x[1] == 1, enumerate(hand_card_status)))) find_two = list( map(lambda x: x[0], filter(lambda x: x[1] == 2, enumerate(hand_card_status)))) find_three = list( map(lambda x: x[0], filter(lambda x: x[1] == 3, enumerate(hand_card_status)))) find_four = list( map(lambda x: x[0], filter(lambda x: x[1] == 4, enumerate(hand_card_status)))) find_one = [find_one] if isinstance(find_one, int) else find_one find_two = [find_two] if isinstance(find_two, int) else find_two find_three = [find_three] if isinstance(find_three, int) else find_three find_four = [find_four] if isinstance(find_four, int) else find_four # 单牌 if card_count == 1: cts.card_type = ActionTypeEnum.ACTION_PUT_ONE.value cts.primary_item = hand_card_seq[0] return True, cts # 对子(包含连对) if card_count % 2 == 0 and card_count > 0 and len(find_two) >= 1: if card_count == 2 and len(find_two) == 1: cts.primary_item = find_two[-1] cts.card_type = ActionTypeEnum.ACTION_PUT_DOU.value return True, cts # 列表中只含对子且对子中不含2 if len(find_two ) * 2 == card_count and CardEnum.TW.value not in find_two: if HandCardUtils.is_find(find_two): cts.primary_item = find_two[-1] size = len(find_two) if size == 3: cts.card_type = ActionTypeEnum.ACTION_PUT_3_DOU.value elif size == 4: cts.card_type = ActionTypeEnum.ACTION_PUT_4_DOU.value elif size == 5: cts.card_type = ActionTypeEnum.ACTION_PUT_5_DOU.value elif size == 6: cts.card_type = ActionTypeEnum.ACTION_PUT_6_DOU.value elif size == 7: cts.card_type = ActionTypeEnum.ACTION_PUT_7_DOU.value elif size == 8: cts.card_type = ActionTypeEnum.ACTION_PUT_8_DOU.value elif size == 9: cts.card_type = ActionTypeEnum.ACTION_PUT_9_DOU.value elif size == 10: cts.card_type = ActionTypeEnum.ACTION_PUT_10_DOU.value return True, cts #三带一单(连三带一单) if card_count % 4 == 0 and card_count > 0 and len(find_three) >= 1: # 三带一单 if len(find_three) == 1 and len(find_one) == 1: cts.primary_item = find_three[-1] cts.card_type = ActionTypeEnum.ACTION_PUT_THREE_ONE.value return True, cts elif len(find_three) * 3 + len( find_one ) == card_count and CardEnum.TW.value not in find_three: if HandCardUtils.is_find(find_three): cts.primary_item = find_three[-1] size = len(find_three) if size == 2: cts.card_type == ActionTypeEnum.ACTION_PUT_2_THREE_ONE.value elif size == 3: cts.card_type == ActionTypeEnum.ACTION_PUT_3_THREE_ONE.value elif size == 4: cts.card_type == ActionTypeEnum.ACTION_PUT_4_THREE_ONE.value elif size == 5: cts.card_type == ActionTypeEnum.ACTION_PUT_5_THREE_ONE.value return True, cts #三带一对(连三带一对) if card_count % 5 == 0 and card_count > 0 and len(find_three) >= 1: # 三带一对 if len(find_two) == 1 and len(find_three) == 1: cts.primary_item = find_three[-1] cts.card_type = ActionTypeEnum.ACTION_PUT_THREE_DOU.value return True, cts elif len(find_three) * 3 + len( find_two ) * 2 == card_count and CardEnum.TW.value not in find_three: if HandCardUtils.is_find(find_three): cts.primary_item = find_three[-1] size = len(find_three) if size == 2: cts.card_type == ActionTypeEnum.ACTION_PUT_2_THREE_DOU.value elif size == 3: cts.card_type == ActionTypeEnum.ACTION_PUT_3_THREE_DOU.value elif size == 4: cts.card_type == ActionTypeEnum.ACTION_PUT_4_THREE_DOU.value return True, cts # 连子 if card_count >= 5: # 必须都是单牌 if len(find_one) == card_count: arr = set(find_one) exclude_arr = { CardEnum.TW.value, CardEnum.JA.value, CardEnum.QU.value } # 连子中不能包含2和大小王 if len(list(arr.intersection(exclude_arr))) == 0: if HandCardUtils.is_find(find_one): cts.primary_item = find_one[-1] size = len(find_one) if size == 5: cts.card_type = ActionTypeEnum.ACTION_PUT_5_CONTINUE.value elif size == 6: cts.card_type = ActionTypeEnum.ACTION_PUT_6_CONTINUE.value elif size == 7: cts.card_type = ActionTypeEnum.ACTION_PUT_7_CONTINUE.value elif size == 8: cts.card_type = ActionTypeEnum.ACTION_PUT_8_CONTINUE.value elif size == 9: cts.card_type = ActionTypeEnum.ACTION_PUT_9_CONTINUE.value elif size == 10: cts.card_type = ActionTypeEnum.ACTION_PUT_10_CONTINUE.value elif size == 11: cts.card_type = ActionTypeEnum.ACTION_PUT_11_CONTINUE.value elif size == 12: cts.card_type = ActionTypeEnum.ACTION_PUT_12_CONTINUE.value return True, cts # 四带两单 if card_count == 6 and len(find_four) >= 1: if len(find_four) == 1 and len(find_one) == 2: cts.primary_item = find_four[-1] cts.card_type = ActionTypeEnum.ACTION_PUT_FOUR_ONE.value return True, cts # 四带两对 if card_count == 8 and len(find_four) >= 1: if len(find_four) == 1 and len(find_two) == 2: cts.primary_item = find_four[-1] cts.card_type = ActionTypeEnum.ACTION_PUT_FOUR_DOU.value return True, cts # 三不带(连三不带) if card_count % 3 == 0 and card_count > 0 and len(find_three) >= 1: if len(find_three) == 1: cts.primary_item = find_three[-1] cts.card_type = ActionTypeEnum.ACTION_PUT_THREE.value return True, cts # 连三不带里面不能包含2 elif len( find_three ) * 3 == card_count and CardEnum.TW.value not in find_three: if HandCardUtils.is_find(find_three): cts.primary_item = find_three[-1] size = len(find_three) if size == 2: cts.card_type = ActionTypeEnum.ACTION_PUT_2_THREE.value elif size == 3: cts.card_type = ActionTypeEnum.ACTION_PUT_3_THREE.value elif size == 4: cts.card_type = ActionTypeEnum.ACTION_PUT_4_THREE.value elif size == 5: cts.card_type = ActionTypeEnum.ACTION_PUT_5_THREE.value elif size == 6: cts.card_type = ActionTypeEnum.ACTION_PUT_6_THREE.value return True, cts # 炸弹(王炸) if card_count == 2: if len(find_one ) == 2 and find_one[0] == CardEnum.QU.value and find_one[ 1] == CardEnum.JA.value: cts.primary_item = find_one[-1] cts.card_type = ActionTypeEnum.ACTION_PUT_BOMB.value return True, cts # 炸弹(普通炸弹) if card_count == 4 and len(find_four) >= 1: if len(find_four) == 1: cts.primary_item = find_four[-1] cts.card_type = ActionTypeEnum.ACTION_PUT_BOMB.value return True, cts return False, None
def judge_card_type(self, hand_card): assert len(hand_card) == 18, 'the size of hand card status must be 18.' cardTypeStruct = CardTypeStruct() card_count = sum(hand_card) cardTypeStruct.card_count = card_count find_one = list( map(lambda x: x[0], filter(lambda x: x[1] == 1, enumerate(hand_card)))) find_two = list( map(lambda x: x[0], filter(lambda x: x[1] == 2, enumerate(hand_card)))) find_three = list( map(lambda x: x[0], filter(lambda x: x[1] == 3, enumerate(hand_card)))) find_four = list( map(lambda x: x[0], filter(lambda x: x[1] == 4, enumerate(hand_card)))) # 单牌 if card_count == 1: assert len(find_one) == 1, 'hand card status error.' cardTypeStruct.card_type = CardTypeEnum.CT_ONE cardTypeStruct.primary_item = find_one[0] # 分值[-7,7] cardTypeStruct.card_score = HandCardUtils.value_map( find_one[0], CardTypeEnum.CT_ONE, card_count) # 对子(包含连对) if card_count % 2 == 0 and card_count > 0 and len(find_two) >= 1: # 列表中只含对子且对子中不含2 if len(find_two ) * 2 == card_count and CardEnum.TW.value not in find_two: if HandCardUtils.is_find(find_two): cardTypeStruct.card_type = CardTypeEnum.CT_DOU cardTypeStruct.primary_item = find_two[-1] # 分值[-7,7] cardTypeStruct.card_score = HandCardUtils.value_map( find_two[-1], CardTypeEnum.CT_DOU, card_count) #三带一单(连三带一单) if card_count % 4 == 0 and card_count > 0 and len(find_three) >= 1: # 三带一单 if len(find_three) == 1 and len(find_one) == 1: cardTypeStruct.card_type = CardTypeEnum.CT_THREE_ONE cardTypeStruct.primary_item = find_three[0] # 分值[-7,7] cardTypeStruct.card_score = HandCardUtils.value_map( find_three[0], CardTypeEnum.CT_THREE_ONE, card_count) elif len(find_three) * 3 + len( find_one ) == card_count and CardEnum.TW.value not in find_three: if HandCardUtils.is_find(find_three): cardTypeStruct.card_type = CardTypeEnum.CT_THREE_ONE cardTypeStruct.primary_item = find_three[-1] # 分值[0.5,7.5] cardTypeStruct.card_score = HandCardUtils.value_map( find_three[-1], CardTypeEnum.CT_THREE_ONE, card_count) #三带一对(连三带一对) if card_count % 5 == 0 and card_count > 0 and len(find_three) >= 1: # 三带一对 if len(find_two) == 1 and len(find_three) == 1: cardTypeStruct.card_type = CardTypeEnum.CT_THREE_DOU cardTypeStruct.primary_item = find_three[0] # 分值 [-7,7] cardTypeStruct.card_score = HandCardUtils.value_map( find_three[0], CardTypeEnum.CT_THREE_DOU, card_count) elif len(find_three) * 3 + len( find_two ) * 2 == card_count and CardEnum.TW.value not in find_three: if HandCardUtils.is_find(find_three): cardTypeStruct.card_type = CardTypeEnum.CT_THREE_DOU cardTypeStruct.primary_item = find_three[-1] # 分值[0.5,7.5] cardTypeStruct.card_score = HandCardUtils.value_map( find_three[-1], CardTypeEnum.CT_THREE_DOU, card_count) # 连子 if card_count >= 5: # 必须都是单牌 if len(find_one) == card_count: arr = set(find_one) exclude_arr = { CardEnum.TW.value, CardEnum.JA.value, CardEnum.QU.value } # 连子中不能包含2和大小王 if len(list(arr.intersection(exclude_arr))) == 0: if HandCardUtils.is_find(find_one): cardTypeStruct.card_type = CardTypeEnum.CT_CONTINUE cardTypeStruct.primary_item = find_one[-1] # 分值[-6,8] cardTypeStruct.card_score = HandCardUtils.value_map( find_one[-1], CardTypeEnum.CT_CONTINUE, card_count) # 四带两单 if card_count == 6 and len(find_four) >= 1: if len(find_four) == 1 and len(find_one) == 2: cardTypeStruct.card_type = CardTypeEnum.CT_FOUR_ONE cardTypeStruct.primary_item = find_four[0] # 分值[0,7] cardTypeStruct.card_score = HandCardUtils.value_map( find_four[0], CardTypeEnum.CT_FOUR_ONE, card_count) # 四带两对 if card_count == 8 and len(find_four) >= 1: if len(find_four) == 1 and len(find_two) == 2: cardTypeStruct.card_type = CardTypeEnum.CT_FOUR_DOU cardTypeStruct.primary_item = find_four[0] # 分值[0,7] cardTypeStruct.card_score = HandCardUtils.value_map( find_four[0], CardTypeEnum.CT_FOUR_DOU, card_count) # 三不带(连三不带) if card_count % 3 == 0 and card_count > 0 and len(find_three) >= 1: if len(find_three) == 1: cardTypeStruct.card_type = CardTypeEnum.CT_THREE cardTypeStruct.primary_item = find_three[0] # 分值[-7,7] cardTypeStruct.card_score = HandCardUtils.value_map( find_three[0], CardTypeEnum.CT_THREE, card_count) # 连三不带里面不能包含2 elif len( find_three ) * 3 == card_count and CardEnum.TW.value not in find_three: if HandCardUtils.is_find(find_three): cardTypeStruct.card_type = CardTypeEnum.CT_THREE cardTypeStruct.primary_item = find_three[-1] # 分值[0.5,7.5] cardTypeStruct.card_score = HandCardUtils.value_map( find_three[-1], CardTypeEnum.CT_THREE, card_count) # 炸弹(王炸) if card_count == 2: if len(find_one ) == 2 and find_one[0] == CardEnum.QU.value and find_one[ 1] == CardEnum.JA.value: cardTypeStruct.card_type = CardTypeEnum.CT_BOMB cardTypeStruct.primary_item = find_one[-1] # 分值 25 cardTypeStruct.card_score = HandCardUtils.value_map( find_one[-1], CardTypeEnum.CT_BOMB, card_count) # 炸弹(普通炸弹) if card_count == 4 and len(find_four) >= 1: if len(find_four) == 1: cardTypeStruct.card_type = CardTypeEnum.CT_BOMB cardTypeStruct.primary_item = find_four[0] # 分值[7,21] cardTypeStruct.card_score = HandCardUtils.value_map( find_four[0], CardTypeEnum.CT_BOMB, card_count) return cardTypeStruct
def test_is_find_hand_card_type(): # case 1:单牌测试 hand_card_seq = [7, 8, 9] hand_card_status = HandCardUtils.obtain_hand_card_status(hand_card_seq) cur_card_type = CardTypeStruct() cur_card_type.card_type = CardTypeEnum.CT_ONE cur_card_type.card_count = 1 cur_card_type.primary_item = 8 ans = HandCardUtils.is_find_hand_card_type(hand_card_seq, hand_card_status, cur_card_type) print('单牌:', ans) # case 2: 对子测试 hand_card_seq = [7, 8, 9, 9, 10] hand_card_status = HandCardUtils.obtain_hand_card_status(hand_card_seq) cur_card_type = CardTypeStruct() cur_card_type.card_type = CardTypeEnum.CT_DOU cur_card_type.card_count = 2 cur_card_type.primary_item = 4 ans = HandCardUtils.is_find_hand_card_type(hand_card_seq, hand_card_status, cur_card_type) print('对子', ans) # case 3: 三不带测试 hand_card_seq = [7, 8, 9, 9, 9] hand_card_status = HandCardUtils.obtain_hand_card_status(hand_card_seq) cur_card_type = CardTypeStruct() cur_card_type.card_type = CardTypeEnum.CT_THREE cur_card_type.card_count = 3 cur_card_type.primary_item = 8 ans = HandCardUtils.is_find_hand_card_type(hand_card_seq, hand_card_status, cur_card_type) print('三不带', ans) # case 4: 三带一测试 hand_card_seq = [7, 8, 9, 9, 9] hand_card_status = HandCardUtils.obtain_hand_card_status(hand_card_seq) cur_card_type = CardTypeStruct() cur_card_type.card_type = CardTypeEnum.CT_THREE_ONE cur_card_type.card_count = 4 cur_card_type.primary_item = 8 ans = HandCardUtils.is_find_hand_card_type(hand_card_seq, hand_card_status, cur_card_type) print('三带一', ans) # case 5: 三带二测试 hand_card_seq = [7, 8, 9, 9, 9, 10, 10] hand_card_status = HandCardUtils.obtain_hand_card_status(hand_card_seq) cur_card_type = CardTypeStruct() cur_card_type.card_type = CardTypeEnum.CT_THREE_DOU cur_card_type.card_count = 5 cur_card_type.primary_item = 8 ans = HandCardUtils.is_find_hand_card_type(hand_card_seq, hand_card_status, cur_card_type) print('三带二', ans) # case 6: 四带二单测试 hand_card_seq = [7, 8, 9, 9, 9, 9] hand_card_status = HandCardUtils.obtain_hand_card_status(hand_card_seq) cur_card_type = CardTypeStruct() cur_card_type.card_type = CardTypeEnum.CT_FOUR_ONE cur_card_type.card_count = 6 cur_card_type.primary_item = 8 ans = HandCardUtils.is_find_hand_card_type(hand_card_seq, hand_card_status, cur_card_type) print('四带二单', ans) # case 7: 四带二对测试 hand_card_seq = [7, 7, 8, 9, 9, 9, 9, 10, 11] hand_card_status = HandCardUtils.obtain_hand_card_status(hand_card_seq) cur_card_type = CardTypeStruct() cur_card_type.card_type = CardTypeEnum.CT_FOUR_DOU cur_card_type.card_count = 8 cur_card_type.primary_item = 8 ans = HandCardUtils.is_find_hand_card_type(hand_card_seq, hand_card_status, cur_card_type) print('四带二对', ans) # case 8: 连子测试 hand_card_seq = [7, 8, 9, 10, 11, 16] hand_card_status = HandCardUtils.obtain_hand_card_status(hand_card_seq) cur_card_type = CardTypeStruct() cur_card_type.card_type = CardTypeEnum.CT_CONTINUE cur_card_type.card_count = 5 cur_card_type.primary_item = 8 ans = HandCardUtils.is_find_hand_card_type(hand_card_seq, hand_card_status, cur_card_type) print('连子', ans) # case 9: 普通炸弹测试 hand_card_seq = [7, 8, 9, 9, 9, 9] hand_card_status = HandCardUtils.obtain_hand_card_status(hand_card_seq) cur_card_type = CardTypeStruct() cur_card_type.card_type = CardTypeEnum.CT_BOMB cur_card_type.card_count = 4 cur_card_type.primary_item = 8 ans = HandCardUtils.is_find_hand_card_type(hand_card_seq, hand_card_status, cur_card_type) print('普通炸弹', ans) # case 10: 王炸 hand_card_seq = [7, 8, 9, 15, 15, 15, 15] hand_card_status = HandCardUtils.obtain_hand_card_status(hand_card_seq) cur_card_type = CardTypeStruct() cur_card_type.card_type = CardTypeEnum.CT_BOMB cur_card_type.card_count = 2 cur_card_type.primary_item = 17 ans = HandCardUtils.is_find_hand_card_type(hand_card_seq, hand_card_status, cur_card_type) print('王炸', ans)
def clear_put_card_seq(self): self._put_card.clear() self._put_color_card.clear() self._put_card_type = CardTypeStruct()