Python 如何打印带有字母Q的虚线框?
我正在用Python尝试这个问题。我无法获得所需的输出。问题是在虚线框中打印“Q”。这是我的密码:Python 如何打印带有字母Q的虚线框?,python,Python,我正在用Python尝试这个问题。我无法获得所需的输出。问题是在虚线框中打印“Q”。这是我的密码: def printBoard (board): for row in range(0, len(board)): for col in range(0, len(board)): if board[row][col]: print(' Q ', end = '') else:
def printBoard (board):
for row in range(0, len(board)):
for col in range(0, len(board)):
if board[row][col]:
print(' Q ', end = '')
else:
print(' | ', end = '')
print('\n')
我的输出:
Board size = 4
| | Q |
Q | | |
| | | Q
| Q | |
预期产出:
Board size = 4
-----------------------
| | | | |
| | | Q | |
| | | | |
-----------------------
| | | | |
| Q | | | |
| | | | |
-----------------------
| | | | |
| | | | Q |
| | | | |
-----------------------
| | | | |
| | Q | | |
| | | | |
-----------------------
使用
制表
模块
从表格导入表格
董事会=[[None,None,1,None],[1,None,None,None,None,1],[None,1,None,None]]
线路板=[[['Q'如果ele==1,则行中ele为None]对于线路板中的行]
打印(制表(board,tablefmt=“grid”))
输出:
+---+---+---+---+
| | | Q | |
+---+---+---+---+
| Q | | | |
+---+---+---+---+
| | | | Q |
+---+---+---+---+
| | Q | | |
+---+---+---+---+
假设您的board变量包含一个列表,该列表指定Queen在板上的位置,如:
board = [[0,0,1,0],[1,0,0,0],[0,0,0,1],[0,1,0,0]]
下面是将打印电路板的python代码:
def水平线(i):
打印(“+”—“*(长度(i)*6-1)+”)
def水平_空间(i):
打印(“|”+“|”*(len(i)))
def水平_值(i):
打印(“|”,end=”“)
对于i中的j:
如果j==1,则打印(“Q |”,end=”),否则打印(“|”,end=”)
打印()
def打印板(板):
水平线(板)
我在董事会:
水平空间(一)
水平_值(i)
水平空间(一)
水平线(一)
印刷板
输出:
-----------------------
| | | | |
| | | Q | |
| | | | |
-----------------------
| | | | |
| Q | | | |
| | | | |
-----------------------
| | | | |
| | | | Q |
| | | | |
-----------------------
| | | | |
| | Q | | |
| | | | |
-----------------------
OP问题的重点不是在示例中的确切位置打印出带有Q的方框 一种实现,允许在运行时将Q放入框中,并随机填充框 这段代码应该适用于Python3和Python2
>>> from random import sample
>>>
>>> def board(x, y):
... b = (('+' + '---+' * x + '\n') +
... (('|' + ' . |' * x + '\n') +
... ('+' + '---+' * x + '\n')) * y)
... return list(b)
...
>>> def print_board(board):
... print(''.join(board))
...
>>> board_array = board(4, 4) # Generate a 4x4 game board.
>>>
>>> # Lists below have the positions to place the Q's at.
>>> # pos1d is a 1-dimension flat list of positions; pos2d, a 2-dim list.
>>> pos1d = tuple(i for i, ch in enumerate(board_array) if ch == '.')
>>> pos2d = tuple(row for row in zip(*[iter(pos1d)] * 4))
>>>
>>> # Remove the .'s if you want.
>>> board_array = [ch if ch != '.' else ' ' for ch in board_array]
>>>
>>> def populate(board, num_q, avail):
... """ Places num_q Q's at positions randomly selected from
avail. Taken positions are removed from avail."""
... pos_a = sample(avail, k=num_q)
... for p in pos_a:
... board[p] = 'Q'
... avail.remove(p) # Remove the taken position from avail.
...
>>> avail = list(pos1d) # List of available squares (or position indices).
>>>
>>> populate(board_array, 4, avail) # Place 4 Q's randomly.
我们现在有一个随机填充的板
>>> print_board(board_array)
+---+---+---+---+
| | | | |
+---+---+---+---+
| | Q | | Q |
+---+---+---+---+
| Q | | | |
+---+---+---+---+
| | | Q | |
+---+---+---+---+
如果你想要一块有较大方块的木板。这将产生更大的正方形。还可以动态指定宽和高的方块数
>>> def board(x, y):
... b = (('+' + '-----+' * x + '\n') +
... (('|' + ' |' * x + '\n') +
... ('|' + ' . |' * x + '\n') +
... ('|' + ' |' * x + '\n') +
... ('+' + '-----+' * x + '\n')) * y)
... return list(b)
位置列表,pos1d
和pos2d
,便于索引到线路板阵列中,并将字符放置在我们想要的位置
>>> def place_q(board, row, col):
... pos = pos2d[row-1][col-1]
... board[pos] = 'Q' # or '\U0000265B'
>>>
>>> place_q(board_array, row=3, col=2)
pos1d
用于随机选择放置Q的位置
>>> q_pos = random.choice(pos1d)
>>>
>>> board_array[q_pos] = 'Q'
一个盒子有两个垂直的墙,不管里面有没有Q;现在,如果有Q,你就不会打印墙。你也不会在任何地方打印水平墙,也不会在线条上下延伸的垂直部分打印Q。@mukhtar ali阵列板支持什么?看起来你正在尝试为n皇后问题绘制板。你能给出你传递给函数的变量
board
的值吗?@Thisisjaymeta我正在画n皇后板,板的大小是8Ok。但是你能告诉我什么是board
变量吗?只需在某个地方打印电路板的值,并显示输出。实际上这是n皇后问题,因此q的位置取决于放置在电路板上的随机值。这种方法对我不起作用,因为我在没有表格模块的油灰上执行它。如果我们调用一个随机函数,从随机导入randint生成位置,q的位置应该如何决定;网格数组[pos[randint(0,3)][randint(0,3)]
@mukhtaralican我将我的程序发送给你@toddt这里有粘贴箱,除非你不想让别人看到它(?)@mukhtaraliMy示例具有可用于构建简单游戏的功能。我至少应该得到一张赞成票=/