我有一个类似这样的网格。我随机地将一个 "b" 放在网格中,并在字母 "b" 周围放置数字 1。这似乎在除了当一个 1 应该被放置在底行和整个右侧列时出现问题。例如,它看起来像这样:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1 b
0 0 0 0 0 0 0 0 0 0
应该看起来像什么
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 1 b
0 0 0 0 0 0 0 0 1 1
这是我正在使用的代码,但我无法弄清楚为什么那些1没有被放置在那里。
from random import*
mat1 = []
mat2 = []
def makemat(x):
for y in range(x):
list1 = []
list2 = []
for z in range(x):
list1.append(0)
list2.append("-")
mat1.append(list1)
mat2.append(list2)
makemat(10)
def printmat(mat):
for a in range(len(mat)):
for b in range(len(mat)):
print(str(mat[a][b]) + "\t",end="")
print("\t")
def addmines(z):
count = 0
while (count < z):
x = randrange(0,len(mat1))
y = randrange(0,len(mat1))
if mat1[y][x] == "b":
count -= 1
else:
mat1[y][x] = "b"
count += 1
addmines(1)
def addscores():
for x in range(len(mat1)):
for y in range(len(mat1)):
if ((y < len(mat1)-1) and (x < len(mat1)-1)) and ((y >= 0) and (x >= 0))):
if mat1[y+1][x] == "b":
mat1[y][x] = 1
if mat1[y-1][x] == "b":
mat1[y][x] = 1
if mat1[y][x+1] == "b":
mat1[y][x] = 1
if mat1[y][x-1] == "b":
mat1[y][x] = 1
if mat1[y+1][x+1] == "b":
mat1[y][x] = 1
if mat1[y+1][x-1] == "b":
mat1[y][x] = 1
if mat1[y-1][x+1] == "b":
mat1[y][x] = 1
if mat1[y-1][x-1] == "b":
mat1[y][x] = 1
printmat(mat1)
addscores()
-1
是干什么的?x < len(mat1)-1
- avishaypx+1
坐标处是否存在炸弹。 - inspectorG4dgetx
达到9
时,你的代码会测试x + 1
的值,即10
,超出了边界。你在这里尝试解决的方法是永远不允许x
达到9
。但是如果x
永远不达到9
,那么你如何在第9
列中放置一个1
呢?你需要找到一个不同的解决方案来解决原始问题。 - senderle