我需要创建一个8x8网格,并在网格上随机分配10个硬币。我面临的问题是randint函数有时会生成相同的随机坐标,因此只有9或8个硬币生成并放置在网格上。我怎样才能确保这不会发生?干杯:)这是我目前的密码:
from random import randint
grid = []
#Create a 8x8 grid
for row in range(8):
grid.append([])
for col in range(8):
grid[row].append("0")
#create 10 random treasure chests
#problem is that it might generate the same co-ordinates and therefore not enough coins
for coins in range(10):
c_x = randint(0, len(grid)-1)
c_y = randint(0, len(grid[0])-1)
while c_x == 7 and c_y == 0:
c_x = randint(0, len(grid)-1)
c_y = randint(0, len(grid[0])-1)
else:
grid[c_x][c_y] = "C"
for row in grid:
print(" ".join(row))
我已经包括了一段时间/其他-因为网格左下角一定没有硬币
目前没有回答
相关问题 更多 >
编程相关推荐