def compare(): import datetime depth = 3 dfs = Dfs() print("start dfs a") start = datetime.datetime.now() manzu = [0,0,0,0,0,0,0,0,0] pinzu = [0,0,0,0,0,0,0,0,0] souzu = [0,0,0,0,0,0,0,0,0] ji = [1,0,0,0,0,3,1] tehai_a = manzu + pinzu + souzu + ji depth = 3 furo_num = (14-sum(tehai_a))//3 from mjaigym.board.function.furo import Furo w_pai = Pai.from_str("W") pai_3m =Pai.from_str("3m") furos = [ Furo({"type":"pon", "actor":0, "target":2, "taken":w_pai, "consumed":[w_pai,w_pai]}), Furo({"type":"pon", "actor":0, "target":2, "taken":w_pai, "consumed":[w_pai,w_pai]}), Furo({"type":"pon", "actor":0, "target":2, "taken":pai_3m, "consumed":[pai_3m,pai_3m]}), ] assert 13 <= sum(tehai_a) + 3 * len(furos) <= 14 shanten_normal, shanten_kokushi, shanten_chitoitsu = shanten.get_shanten_all(tehai_a, len(furos)) result_a = dfs.dfs_with_score_normal(tehai_a, furos, depth, shanten_normal=shanten_normal) # a_changes = set() # for ra in result_a: # a_changes.add(tuple(ra[0])) print("start dfs b") dfs = Dfs() manzu = [0,0,0,0,0,0,0,0,0] pinzu = [0,0,0,0,0,0,0,0,0] souzu = [0,0,0,0,0,0,0,0,0] ji = [1,0,0,0,0,1,3] tehai_b = manzu + pinzu + souzu + ji shanten_normal, shanten_kokushi, shanten_chitoitsu = shanten.get_shanten_all(tehai_b, len(furos)) result_b = dfs.dfs_with_score_normal(tehai_b, furos, depth, shanten_normal=shanten_normal) result_a = sorted(result_a, key=lambda x:x[0]) result_b = sorted(result_b, key=lambda x:x[0]) print(len(result_a)) print(len(result_b)) for i in range(max(len(result_a),len(result_b))): if i >= len(result_b): print(result_a[i][0]) else: print(result_a[i][0], result_b[i][0])
def test_dfs_pattern_chitoitsu(): dfs = Dfs() # chitoitsu pattern nums = [ 0, 2, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 1, ] dora_ids = [1, 5, 33] doras = [p for p in Pai.from_idlist(dora_ids)] depth = 3 _, _, chitoitsu_shanten = shanten.get_shanten_all(nums, 0) results = dfs.dfs_with_score_chitoitsu( nums, furos=[], depth=depth, shanten_chitoitsu=chitoitsu_shanten, doras=doras, ) result = sorted(results, key=lambda x: x.point_info.points)[-1] print(f"{nums} \n-> {result}, depth:{depth}, {[d.id for d in doras]}") dora_id_contains = False for toitsu in result.combination: dora_id_contains |= toitsu[0] in dora_ids assert dora_id_contains
def main(): import datetime manzu = [1,0,0,1,0,0,0,0,0] pinzu = [0,0,0,0,0,2,2,0,0] souzu = [0,0,0,2,0,0,2,0,0] ji = [0,3,0,0,0,0,0] """ manzu = [1,0,0,0,0,0,0,0,1] pinzu = [1,0,0,0,0,0,0,0,1] souzu = [1,0,3,0,0,0,0,0,1] ji = [1,1,1,1,0,0,1] """ tehai = manzu + pinzu + souzu + ji tehai = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 3, 0, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] depth = 3 furo_num = (14-sum(tehai))//3 from mjaigym.board.function.furo import Furo w_pai = Pai.from_str("W") pai_3m =Pai.from_str("3m") furos = [ Furo({"type":"pon", "actor":0, "target":2, "taken":pai_3m, "consumed":[pai_3m,pai_3m]}), # Furo({"type":"pon", "actor":0, "target":2, "taken":w_pai, "consumed":[w_pai,w_pai]}), # Furo({"type":"pon", "actor":0, "target":2, "taken":pai_3m, "consumed":[pai_3m,pai_3m]}), ] assert 13 <= sum(tehai) + 3 * len(furos) <= 14 dfs = Dfs() shanten_normal, shanten_kokushi, shanten_chitoitsu = shanten.get_shanten_all(tehai, len(furos)) start = datetime.datetime.now() for i in range(40): tehai = get_random_tehai() # if 0 <= shanten_normal < depth-1: result = dfs.dfs_with_score_normal(tehai, furos, depth, oya=True, shanten_normal=shanten_normal) # result = dfs.dfs_with_score_chitoitsu(tehai, furos, depth, doras=Pai.from_list(["1m","3m"]), shanten_chitoitsu=shanten_chitoitsu) # result = dfs.dfs_with_score_kokushi(tehai, furos, depth, oya=True, shanten_kokushi=shanten_kokushi) # result = dfs.dfs(tehai, furo_num, depth) end = datetime.datetime.now() print(tehai) for r in result: print(r) pass print(len(result)) print(end - start)
def test_dfs_score(): dfs = Dfs() # can add new dora and dora tanki test nums = [ 0, 0, 2, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 1, ] dora_ids = [1, 5, 27, 33] doras = [p for p in Pai.from_idlist(dora_ids)] depth = 9 _, _, chitoitsu_shanten = shanten.get_shanten_all(nums, 0) result = dfs.dfs_with_score_chitoitsu( nums, furos=[], depth=3, shanten_chitoitsu=chitoitsu_shanten, doras=doras, ) print(f"{nums} \n-> {result}, depth:{depth}, {[d.id for d in doras]}") assert len(result) > 0
def calc_all_shanten(self, tehai, furonum): """returns [normal, kokushi, chitoi] Args: tehai ([type]): [description] furonum ([type]): [description] Returns: [] """ return shanten.get_shanten_all(tehai, furonum)
def test_dfs_pattern_chitoitsu_toitsu_tanki(): dfs = Dfs() # can add new dora and dora tanki test nums = [ 0, 0, 2, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 1, ] dora_ids = [1, 5, 20, 33] doras = [p for p in Pai.from_idlist(dora_ids)] depth = 3 _, _, chitoitsu_shanten = shanten.get_shanten_all(nums, 0) results = dfs.dfs_with_score_chitoitsu( nums, furos=[], depth=depth, shanten_chitoitsu=chitoitsu_shanten, doras=doras, ) result = sorted(results, key=lambda x: x.point_info.points)[-1] print(f"{nums} \n-> {result}, depth:{depth}, {[d.id for d in doras]}") contains_33 = False contains_1_5_20 = False for toitsu in result.combination: if toitsu[0] in [1, 5, 20]: contains_1_5_20 = True if toitsu[0] == 33: contains_33 = True assert contains_1_5_20 & contains_33
def test_dfs_pattern_kokushi(): dfs = Dfs() # can add new dora and dora tanki test nums = [ 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, ] dora_ids = [1, 5, 27, 33] doras = [p for p in Pai.from_idlist(dora_ids)] depth = 3 oya = True _, shanten_kokushi, _ = shanten.get_shanten_all(nums, 0) results = dfs.dfs_with_score_kokushi( nums, furos=[], depth=3, oya=oya, shanten_kokushi=shanten_kokushi, ) print(f"{nums} \n-> {results}, depth:{depth}, {[d.id for d in doras]}") assert len(results) > 0 kokushi_exists = False for result in results: yakus = result.point_info.yakus if any([yaku[0] == "kokushimuso" for yaku in yakus]): kokushi_exists = True assert kokushi_exists