Quick actions

cmd+k|ctrl+k

Navigation

Languages

mapgen

Snippet info

Language

Python

Visibility

public

Author

hyrious

Created

2020-09-11T02:31:34Z

Updated

2020-09-11T02:31:34Z

"""
简化的生成代码:
假设这里只有一种单元, 他有三个接口 (门), 标记为 A, B, C
每个接口可以连接 **其他** 单元的 **不同** 接口, 也就是说 A 只能连 B, C
这里不考虑门可以隐藏的情况 (就是说每个接口必须连接出去)
下面代码中 max_depth 是 **至少** 生成这么多个单元
你可以把输出粘贴到 https://dreampuf.github.io/GraphvizOnline 来看
"""

import random

flag = True
points = [[None] * 3]

def any_not_None(iterable):
    for e in iterable:
        if e is not None:
            return True
    return False

def gather(not_this_i=None, not_this_j=None):
    global flag
    if flag:
        flag = False
        return [(0, i) for i in range(3)]
    candidates = []
    for i, x in enumerate(points):
        if i == not_this_i: continue
        if any_not_None(x):
            for j, y in enumerate(x):
                if j == not_this_j: continue
                if y is None:
                    candidates.append((i, j))
    return candidates


max_depth = 12
while a := gather():
    i, j = random.choice(a)
    need_expand = len(points) < max_depth
    b = gather(i, j)
    if need_expand or not b:
        k, l = len(points), random.choice([x for x in range(3) if x != j])
        points += [[None] * 3]
    else:
        k, l = random.choice(b)
    points[i][j] = [k, l]
    points[k][l] = [i, j]

print("digraph G {\n  edge [\n    arrowhead=\"none\"\n  ];\n")
for i, x in enumerate(points):
    for y in x:
        j = y[0]
        if i < j:
            print(f"  {i} -> {j};")
print("}")
INFO