Python 中列出游戏空间中所有可能坐标的最佳方法是什么?

问题描述

为了学习 Python,我正在开发一款游戏。我遇到了一个可能有多种解决方案的难题(递归?减少?理解?),所以我想知道什么是最好的方法(例如,最“Pythonic”、性能最佳、可读等)。你能帮助新手学习很酷的东西吗?

谜题是获得一个笛卡尔坐标列表,用它来引用游戏板中的空间——但我希望我的解决方案适用于任意数量的维度(不仅是 2D 国际象棋,还有 3D 国际象棋等) .)

这是我所知道的,我想知道是否有更好的方法

def cartc(dimensions):
    """Returns an object that generates a unique tuple of integers (cartesian coordinates) for each spot 
        in the dimensions. 


    Args:
        dimensions: An arbitrarily long sequence range objects. Each range object represents one
            dimension of the space

    Returns:
        an iterable object that generates tuples of integers: e.g. (1,1)
        
            cartc( [range(3),range(2)] ) generates (0,0),(0,1),(1,(2,and (2,1)
            
    """
    
    # Generate sets of coordinates by joining lists
    tally = [[]]
    for dim in reversed( dimensions ):
        tally = [ [coord] + partial for coord in dim for partial in tally ]
    
    # Convert to tuples
    for coords in tally:
        yield tuple( coords )

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)