首页 > 解决方案 > 有没有办法可视化网格?

问题描述

我有一个包含 8 个列表的列表,其中 8 个没有(就像一个 8x8 的网格)。我在板上填了几块,现在我想以一种友好的方式看板。

现在我正在使用这种方法:

class ChessBoard():
      def __init__(self):
           self.board = [[None for x in range(0, 8)] for y in range(0, 8)]

      def Show(self):
           for line in self.board:
                  new_line = [None for x in range(0, 8)]
                  for i in range(0, 8):
                        if line[i] is not None:
                             new_line[i] = line[i].kind
                  print(new_line)

像这样打印它:

['White Rook', None, None, None, None, None, None, None]
['White Rook', None, None, None, None, None, None, None]
[None, None, None, None, None, None, None, None]
[None, None, None, None, None, None, None, None]
[None, None, None, None, None, None, None, None]
[None, None, None, None, 'Black King', None, None, None]
[None, None, None, None, None, None, 'White King', None]
[None, None, None, None, None, None, None, None]

我是python新手,所以我不太了解函数。有没有一种简单的方法可以将其显示为网格或任何其他方式。我可以制作一个网格并在其中写下零件名称吗?我可以下载棋盘和棋子的图像并将它们加载到特定位置吗?

标签: python

解决方案


您可以手动完成,这并不难。迭代数组并找出每列中最长的项目,然后再次迭代它并打印每个单元格,将每个项目填充到该列的最大宽度。

如果涉及到,在单元格中实现包装甚至非常容易。

对我来说,这是有益的,我一直在使用它。由于它只是显示地图列表(或对象列表),因此可以无限重复使用。因为我是在 Groovy 中完成的,所以我并没有真正的 python 源代码来展示给你。


推荐阅读