我正在做一个项目,要求我创建一个没有GUI的picross游戏。我有一个打印picross板的功能有问题,我需要帮助。 我应该打印这个:
这是我当前的代码,并且董事会的实习生代表是board = [(规范元组),[列表与创建的单元格]默认情况下创建的单元格为零,我有一个单独的函数来创建它们的元组元组作为参数(注意元组长度的元组是2,电路板大小由元组长度len(规格元组[0])确定,如果长度为3,则电路板为3x3,例如):< / p>
t=[(((1,), (3,), (1,)), ((3,), (1,), (1,))), [[0, 0, 0], [0, 0, 0],[0, 0, 0]]]
board_size = len(t[0][0])
cells = t[1]
hnums = t[0][1]
vnums = t[0][0]
h1 = [x[1] if len(x) > 1 else " " for x in hnums]
h2 = [x[0] for x in hnums]
v1 = [x[1] if len(x) > 1 else " " for x in vnums]
v2 = [x[0] for x in vnums]
heading_string = " {} " * board_size
print(heading_string.format(*h1))
print(heading_string.format(*h2))
def cell_text(c):
if c == 0:
return "?"
elif c == 1:
return "."
else:
return "X"
row = []
for i in t[1]:
for x, cell in enumerate(i, 1):
row.append(cell_text(cell))
if x % board_size == 0:
row_string = "[ {} ]" * board_size
vnum_string = " " + str(v2[(x) // board_size])+ " " \
+ str(v1[(x) // board_size]) + "|"
print(row_string.format(*row) + vnum_string)
row = []
问题是元组t [0]内的元组长度不同而且对于这段代码我只能在长度为2的情况下打印。这是输出
1 3 1
[ ? ][ ? ][ ? ] 3 |
[ ? ][ ? ][ ? ] 3 |
[ ? ][ ? ][ ? ] 3 |
需要格式化为:
1 3 1
[ ? ][ ? ][ ? ] 3|
[ ? ][ ? ][ ? ] 3|
[ ? ][ ? ][ ? ] 3|
或例如此板规格t = [(((1,2),(1,),(1,1)),((1,2),(1,),(1,1) )),[[0,0,0,[0,0,0],[0,0,0]]]输出应该是这样的:
1 1
2 1 1
[ ? ][ ? ][ ? ] 1 2|
[ ? ][ ? ][ ? ] 1 |
[ ? ][ ? ][ ? ] 1 1|
电路板尺寸也取决于t [0] [0]长度
z
x x x x x
[ ? ] [ ? ] [ ? ] [ ? ] [ ? ] y |
[ ? ] [ ? ] [ ? ] [ ? ] [ ? ] y |
[ ? ] [ ? ] [ ? ] [ ? ] [ ? ] y |
[ ? ] [ ? ] [ ? ] [ ? ] [ ? ] y |
[ ? ] [ ? ] [ ? ] [ ? ] [ ? ] y z|
答案 0 :(得分:0)
这个怎么样
tablero_data1 = [(((1,), (3,), (1,)), ((3,), (1,), (1,))), [[0, 0, 0], [0, 0, 0],[0, 0, 0]]]
tablero_data2 = [(((1, 2), (1,), (1, 1)), ((1, 2), (1,), (1, 5))), [[0, 1, 0], [0, 0, 2], [0, 0, 0]]]
def print_board(board_data:"[border,inside]"):
"""border = (rows,columns)
inside = [ [int] ]
columns = ( espesication, )
rows = ( espesication, )
espesication = (int,)"""
(rows,columns),inside = board_data
board_size = len(columns)
nivel_columns = max(map(len,columns))
nivel_rows = max(map(len,rows))
rows = tuple(map(lambda x: x+(" ",)*nivel_rows,rows))
inside = list(map(lambda f:f[:board_size],inside))
columns = tuple(map(lambda esp:tuple(reversed(esp)),columns))
for i in reversed( range(nivel_columns) ):#print columns info
for esp in columns:
if len(esp)>i:
print(" %d "%esp[i],end="")
else:
print( " ",end="")
print()
for j,fila in enumerate(inside):#print rows
print("".join(map(lambda i: "[ %s ]"%cell_text(i),fila)) +" "+ " ".join(map(str,rows[j][:nivel_rows]))+"|" )
print_board(tablero_data1)
print()
print_board(tablero_data2)
输出
3 1 1
[ ? ][ ? ][ ? ] 1|
[ ? ][ ? ][ ? ] 3|
[ ? ][ ? ][ ? ] 1|
1 1
2 1 5
[ ? ][ . ][ ? ] 1 2|
[ ? ][ ? ][ X ] 1 |
[ ? ][ ? ][ ? ] 1 1|