def test_monte(): Logger.init() log = logging.getLogger("mahjong") server = GameServer() client1 = Client(1) client2 = Client(2) client3 = Client(3) server.bind(client1) server.bind(client2) server.bind(client3) server.init() server.deal() tile = server.tiles.pop(0) client1.touch_tile(tile) hands = [client1.hands(), client2.hands(), client3.hands()] discards = [[], [], []] melds_3 = [[], [], []] melds_4 = [[], [], []] for hand in hands: log.info(TilesConv.tiles_18_to_str(hand)) game_state = GameState(hands=hands, discards=discards, melds_3=melds_3, melds_4=melds_4) game_state.check() tree_node = TreeNode(net=None, game_state=game_state) node = tree_search(tree_node, N_SIMS, server, MahjongState.AfterTouching) log.info( "The result of monte tree search is :discard tile {} for given hand {}" .format(Tile(node.discard_tile), TilesConv.tiles_18_to_str(hands[0])))
def calc_effective_cards(self, tiles_18, n): count = TilesConv.tiles18_count(tiles_18) if count % 3 != 1: self.log.error( "hands must be ready hand example:13 length or 7 length") return None judge_tile_chains = self.calc_effective_cards_internal( tiles_18, n, [], []) logger().debug(TilesConv.tiles_18_to_str(tiles_18)) for chain in judge_tile_chains: logger().debug(chain) return judge_tile_chains
def rp_t1(t, index, game_state): """ 计算邻近的牌的个数和权重,权重越高邻近的牌越多 :param index: 计算玩家{index}的t牌邻近权重 :param t:牌值 :param game_state:牌局 :return: 返回邻近牌的权重,邻近牌越多权重越高,如果手牌中没有该牌返回0 """ s = game_state p_t = 0 sn_0 = s.hands[index][t] if sn_0: sn_1, sn_2, kn_0, kn_1, kn_2 = 0, 0, 0, 0, 0 kn_0 = Attack.tile_remain_num(t, game_state, index) # 计算相邻的牌的个数 for t1 in Attack.get_first_level_ts(t): sn_1 += s.hands[index][t1] kn_1 += Attack.tile_remain_num(t1, game_state, index) # 计算相隔一张牌的个数 for t2 in Attack.get_second_level_ts(t): sn_2 += s.hands[index][t2] kn_2 += Attack.tile_remain_num(t2, game_state, index) p_t = sn_0 * S0 + (sn_1 * S1) + (sn_2 * S2) + kn_0 * K0 + ( kn_1 * K1) + (kn_2 * K2) + sn_0 * sn_0 * sn_0 p_t /= TilesConv.tiles18_count(s.hands[index]) return p_t
from mahjong.rule.algo.hand_calculator import HandCalculator from mahjong.rule.util.tile_convert import TilesConv # useful helper def print_hand_result(hand_results): for result in hand_results: print(result) print('') tiles_18 = TilesConv.string_to_18_array(tongzi='111123', tiaozi='55667788') results = HandCalculator.estimate_hand_value_zigong(tiles_18, 5) print_hand_result(results) for i in range(0,1): print(i)
def hand_str(self): tongzi, tiaozi = TilesConv.tiles_18_to_str(self.tiles_18) r = "tongzi:{} tiaozi:{}".format(tongzi, tiaozi) return r
def __str__(self): return TilesConv.tile_to_string(self.tile)
def get_hands_str_index(self, index): return TilesConv.tiles_18_to_str(self.hands[index])
def get_cur_hands_str(self): return TilesConv.tiles_18_to_str(self.hands[self.turn])