压缩文本而不更改缩进

时间:2017-02-10 17:40:43

标签: python grid

在Python中,我试图创建一个游戏,允许玩家在网格上输入一个位置,并从他们的位置检查多个实例并收集某个变量的x量。

我有一个允许用户使用8x8网格或12x12的功能。为了生成12x12网格,我直接通过8x8网格中的代码进行剪切和复制,更改整数以便打印12行和列。

如果用户选择8x8网格,则代码打印完美:

-------------------------- 
   1  2  3  4  5  6  7  8 
1  .  .  .  .  .  .  .  . 
2  .  .  .  .  .  .  .  . 
3  .  .  .  .  .  .  .  . 
4  .  .  .  .  .  .  .  . 
5  .  .  .  .  .  .  .  . 
6  .  .  .  .  .  .  .  . 
7  .  .  .  .  .  .  .  . 
8  0  .  .  .  .  .  .  .  
-------------------------- 

但是,当12x12网格生成为' 10,11和12'时,代码将从8x8网格中剪切并粘贴。有两个数字缩进使得网格上的较低行变得错误缩进。

--------------------------------------- 
   1  2  3  4  5  6  7  8  9  10  11  12   
1  .  .  .  .  .  .  .  .  .  .  .  . 
2  .  .  .  .  .  .  .  .  .  .  .  . 
3  .  .  .  .  .  .  .  .  .  .  .  . 
4  .  .  .  .  .  .  .  .  .  .  .  . 
5  .  .  .  .  .  .  .  .  .  .  .  . 
6  .  .  .  .  .  .  .  .  .  .  .  . 
7  .  .  .  .  .  .  .  .  .  .  .  . 
8  .  .  .  .  .  .  .  .  .  .  .  . 
9  .  .  .  .  .  .  .  .  .  .  .  . 
10  .  .  .  .  .  .  .  .  .  .  .  . 
11  .  .  .  .  .  .  .  .  .  .  .  . 
12  0  .  .  .  .  .  .  .  .  .  .  . 
--------------------------------------- 

以下是生成网格的示例代码:

for i in range(-1+(coordinate[0])):
   print(length," . "*12)
   length=length+1
print(length," . "*((coordinate[1])-1),counter," . "*(11-coordinate[1]))
length=length+1
for i in range(12-coordinate[0]):
   print(length," . "*12)
   length=length+1

长度是指从1开始的行号,每个循环添加1,因此打印新的行号。

我想知道是否有一个快速修复,以便,例如,缩小行数,使其适合作为一行;这样的任何修复都可以起作用。任何关于如何快速修复这种缩进的想法都会有很大用处。我是一张相对较新的海报,所以对于我可能错过的任何信息,请给我留言。

1 个答案:

答案 0 :(得分:0)

简短的回答是使用指定宽度的str.format()。例如:

>>> for n in (1, 18, 100):
...     print '---|{:>3}|---'.format(n) # 3 is the width, > is to right justify
...
---|  1|---
---| 18|---
---|100|---

我写了一个简短的类来封装你的网格:

class SquareGrid(object):
    def __init__(self, size, empty_spot='.'):
        self._size = size
        self._empty_spot = empty_spot
        self._grid = [[empty_spot for _ in range(size)] for __ in range(size)]

    def set_marker(self, row, col, marker):
        self._grid[row-1][col-1] = marker

    def clear_marker(self, row, col):
        self._grid[row-1][col-1] = self._empty_spot

    def display(self):
        label_width = len(str(self._size))
        label_format = '{{:>{}}}'.format(label_width)
        labels = [label_format.format(i+1) for i in range(self._size)]
        print(' ' * (label_width), ' '.join(labels))
        for label, row in zip(labels, self._grid):
            print(label, ' '.join(label_format.format(marker) for marker in row))

这是在行动:

>>> grid = SquareGrid(15)
>>> grid.set_marker(15, 15, "x")
>>> grid.set_marker(2, 8, "o")
>>> grid.set_marker(13, 3, "x")
>>> grid.set_marker(3, 10, "o")
>>> grid.display()
    1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
 1  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
 2  .  .  .  .  .  .  .  o  .  .  .  .  .  .  .
 3  .  .  .  .  .  .  .  .  .  o  .  .  .  .  .
 4  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
 5  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
 6  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
 7  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
 8  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
 9  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
10  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
11  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
12  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
13  .  .  x  .  .  .  .  .  .  .  .  .  .  .  .
14  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
15  .  .  .  .  .  .  .  .  .  .  .  .  .  .  x
相关问题