0

我想打印一个连接四个板到控制台,我有以下代码:

def print_Board (b):
print('.'.join(map(lambda x: str(x + 1), range(connectfour.BOARD_COLUMNS))))
for y in range(connectfour.BOARD_ROWS):
    print('. '.join(b[x][y] for x in range(connectfour.BOARD_COLUMNS)))

输出应如下所示:

1 2 3 4 5 6 7
. . . . . . .
. . . . . . .
. . . . . . .
R . . . . . .
R Y R . . . .
Y Y R . . . .

但它的出现是:

1.2.3.4.5.6.7
. . . . . . 
. . . . . . 
. . . . . . 
R . . . . . 
R Y R . . . 
Y Y R . . . 
4

1 回答 1

0

If empty cell is represented as empty string or space character, you can dump it as follow.

def print_board (b):
    print(' '.join(map(lambda x: str(x + 1), range(connectfour.BOARD_COLUMNS))))
    for y in range(connectfour.BOARD_ROWS):
        print(' '.join(b[x][y].strip() or '.' for x in range(connectfour.BOARD_COLUMNS)))

>>> print_board([
...     [' ', ' ', ' ', ' ', 'R', 'R', 'Y'],
...     [' ', ' ', ' ', ' ', ' ', 'Y', 'Y'],
...     [' ', ' ', ' ', ' ', ' ', 'R', 'R'],
...     [' ', ' ', ' ', ' ', ' ', ' ', ' '],
...     [' ', ' ', ' ', ' ', ' ', ' ', ' '],
...     [' ', ' ', ' ', ' ', ' ', ' ', ' '],
...     [' ', ' ', ' ', ' ', ' ', ' ', ' '],
... ])
1 2 3 4 5 6 7
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
R . . . . . .
R Y R . . . .
Y Y R . . . .
于 2013-10-19T05:46:59.837 回答