如何在网格中随机放置符号?-Python 3.4.3
假设我有一个7x7的网格:如何在网格中随机放置符号?-Python 3.4.3,python,Python,假设我有一个7x7的网格: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 如何将0随机放置在网格中任意位置的3行/列中 . . . . . . . . . . . . . . . . 0 0 0 . . . . . . . . . . . . . . 0 . . . . . . 0 . . . . . . 0 . 你随机化方向。。。水平或垂直。
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
如何将0随机放置在网格中任意位置的3行/列中
. . . . . . .
. . . . . . .
. . 0 0 0 . .
. . . . . . .
. . . . . 0 .
. . . . . 0 .
. . . . . 0 .
你随机化方向。。。水平或垂直。 然后,将“000”位置的两个数字随机化,但这取决于方向。一个数字在范围(0,7)内,另一个在范围(0,7-3)内。“-3”部分是这样的,它不会被放置在板外 下面是一些python代码
from random import randint
def place_randomly(grid):
orientation = ['h', 'v'][randint(0, 1)] #randomly choose orientation
if orientation == 'h':
x = randint(0, 4)
y = randint(0, 6)
grid[y][x] = grid[y][x+1] = grid[y][x+2] = '0'
elif orientation == 'v':
x = randint(0, 6)
y = randint(0, 4)
grid[y][x] = grid[y+1][x] = grid[y+2][x] = '0'
print "XY:", x, y
def print_grid(grid):
for i in grid:
for j in i:
print j,
print ""
for i in range(10): # try it out 10 times
grid = [['.' for x in range(7)] for x in range(7)]
place_randomly(grid)
print_grid(grid)
print ""
你需要几个街区?它们能重叠吗?你能展示一下到目前为止你写的代码吗?