Python-从2D位置对象数组打印ASCII直角坐标网格

问题描述 投票:0回答:1

非常抱歉,所有,但是这将是一个漫长的过程。因此,作为我之前有关STDIN的询问的分支,很遗憾,它没有出现与我的特定问题类似的足够多的问题,至少在Python中没有(也不使用类似numpy的东西,这超出了我的水平) 。我确实有很多已经实现的代码,因此希望会对您有所帮助(尽管它不是最干净的,而且正如我上面提到的那样,它很长)。

我的问题是,打印出表示类对象的2D数组的ASCII艺术,以便ASCII以简单的x,y直角坐标网格的方向准确地描述它们。

我有几个相互交互的类,第一个类构造包含两个整数的位置对象,它们代表笛卡尔坐标对的x和y:

class Pos:
    def __init__(self, x, y):
        self.x = x
        self.y = y

第二类从用户在我程序的不同部分中提供的某个大小参数构造一个“正方形”网格,在这里实现为填充有这些位置对象的2D数组:

class Grid:
    def __init__(self, size):
        assert size > 0
        self.size = size
        self.grid = []
        #1  Perhaps there is something wrong with my 2D implementation...
        for x in range(size):
            sub_grid = []
            for y in range(size):
                sub_grid.append(Pos(x, y))
            self.grid.append(sub_grid)

也在该类之内的一种方法是打印出网格的ASCII艺术形式,以作为用户的外部表示形式(这是杂乱的位:]]

def print(self):
    #  This is the main ASCII-art.
    size_check = self.size - 1
    if self.size > 10:
        print('   +' + ('-' * (((self.size * 2) + 1)) + '+'))
    else:
        print('  +' + ('-' * (((self.size * 2) + 1)) + '+'))
    counter = 0
    #2  ...or perhaps it's something wrong with this iterative condition.
    for x in self.grid[::-1]:
        if self.size > 10:
            if size_check > 9:
                print(self.size - 1 - counter, '|', end=' ')
            else:
                print('', self.size - 1 - counter, '|', end=' ')
        else:
            print(self.size - 1 - counter, '|', end=' ')
        counter += 1
        size_check -= 1
        for y in x:
            #  This is only here to check if the coords are in the correct position in the art.
            ^^if y.x == 5 and y.y == 8:
                print('O ', end='')^^
            else:
                print('. ', end='')
        print('|')
    if self.size > 10:
        print('   +' + ('-' * (((self.size * 2) + 1)) + '+'))
        print('     ', end='')
    else:
        print('  +' + ('-' * (((self.size * 2) + 1)) + '+'))
        print('    ', end='')

    #  This is simply support for grid sizes greater than 10.
    if self.size > 10:
        for x in self.grid:
            if x[0].x <= 9:
                print('', '', end=' ')
            elif x[0].x > 9 and x[0].x < (self.size - 1):
                strng = str(x[0].x)
                print(int(strng[0]), end=' ')
            elif x[0].x == (self.size - 1):
                strng = str(x[0].x)
                print(int(strng[0]))
        print('     ', end='')
        for x in self.grid:
            if x[0].x <= 9:
                print(x[0].x, '', end='')
            elif x[0].x > 9 and x[0].x < (self.size - 1):
                strng = str(x[0].x)
                print(int(strng[1]), end=' ')
            elif x[0].x == (self.size - 1):
                strng = str(x[0].x)
                print(int(strng[1]), end=' ')
    else:
        for x in self.grid:
            if x[0].x < (self.size - 1):
                print(x[0].x, '', end='')
            elif x[0].x == (self.size - 1):
                print(x[0].x, end='')
    print()

我知道很多,但是成功打印出来的内容看起来像这样(给定大小= 10):

  +---------------------+
9 | . . . . . . . . . . |
8 | . . . . . . . . . . |
7 | . . . . . . . . . . |
6 | . . . . . . . . . . |
5 | . . . . . . . . . . |
4 | . . . . . . . . . . |
3 | . . . . . . . . . . |
2 | . . . . . . . . . . |
1 | . . . . . . . . . . |
0 | . . . . . . . . . . |
  +---------------------+
    0 1 2 3 4 5 6 7 8 9

现在看起来很好,直到我尝试显示应该放置位置对象的位置。在上面的代码中,我用^^标记了一个条件语句,该条件语句在坐标(5,8)处打印出“ O”。结果看起来像这样:

  +---------------------+
9 | . . . . . . . . . . |
8 | . . . . . . . . . . |
7 | . . . . . . . . . . |
6 | . . . . . . . . . . |
5 | . . . . . . . . O . |
4 | . . . . . . . . . . |
3 | . . . . . . . . . . |
2 | . . . . . . . . . . |
1 | . . . . . . . . . . |
0 | . . . . . . . . . . |
  +---------------------+
    0 1 2 3 4 5 6 7 8 9

理想的输出实际上应该看起来像这样:

  +---------------------+
9 | . . . . . . . . . . |
8 | . . . . . O . . . . |
7 | . . . . . . . . . . |
6 | . . . . . . . . . . |
5 | . . . . . . . . . . |
4 | . . . . . . . . . . |
3 | . . . . . . . . . . |
2 | . . . . . . . . . . |
1 | . . . . . . . . . . |
0 | . . . . . . . . . . |
  +---------------------+
    0 1 2 3 4 5 6 7 8 9

正如您所看到的,即使在内部,任何位置对象的坐标都正确响应,但ASCII艺术打印的方式都是从外部外观

,好像x和y轴已互换。我在上面的代码中写了两个注释,我推测可能是问题所在。我最好的猜测是我如何实现网格本身,或者我如何印刷艺术品。但这是我需要弄清楚的东西。

一如既往,我们将尽一切帮助;我再次道歉!

非常抱歉,所有,但是这将是一个漫长的过程。因此,作为我之前有关STDIN的询问的分支,很遗憾,似乎没有很多与我......

python arrays python-3.x class ascii
1个回答
2
投票

我建议您使用字符串操作而不是直接打印来构建行。这将使代码更简单,并允许在模板字符串中替换典型行内容的模式。

例如:

© www.soinside.com 2019 - 2024. All rights reserved.