完整代码这里,我正在尝试检查邻居是否有用户调用的单元格。编辑:“对不起,不清楚。问题是,当我检查单元格46时。我想知道周围8个单元中有多少是地雷。不幸的是,许多单元格没有这样做,我也不明白为什么。例如,它经常给我两个数字。”
def selfncheck(grid,matrix,ident): #shenanigans are present
'''check self and neighbours for being a mine. If not mined open neighbours'''
identifier=[]
identifier.append(ident)
for ident in identifier:
cellvalue=0
if ident==0: #top left corner +1 +11 +10
if matrix[ident+1]=='x':
cellvalue+=1
if matrix[ident+10]=='x':
cellvalue+=1
if matrix[ident+11]=='x':
cellvalue+=1
if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
identifier.append(ident+1)
identifier.append(ident+11)
identifier.append(ident+10)
if ident==9: #Top right corner -1 +10 +9
if matrix[ident-1]=='x':
cellvalue+=1
if matrix[ident+10]=='x':
cellvalue+=1
if matrix[ident+9]=='x':
cellvalue+=1
if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
identifier.append(ident-1)
identifier.append(ident+10)
identifier.append(ident+9)
if ident==90: #bot left corner +1 -10 -9
if matrix[ident+1]=='x':
cellvalue+=1
if matrix[ident-10]=='x':
cellvalue+=1
if matrix[ident-9]=='x':
cellvalue+=1
if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
identifier.append(ident+1)
identifier.append(ident-9)
identifier.append(ident+-10)
if ident==99: #bot right corenr -10 -11 -1
if matrix[ident-1]=='x':
cellvalue+=1
if matrix[ident-10]=='x':
cellvalue+=1
if matrix[ident-11]=='x':
cellvalue+=1
if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
identifier.append(ident-1)
identifier.append(ident-10)
identifier.append(ident-11)
if ident>0 and ident<9: #checking top row -1 +1 +10 +9 +11 cells only
if matrix[ident-1]=='x':
cellvalue+=1
if matrix[ident+1]=='x':
cellvalue+=1
if matrix[ident+10]=='x':
cellvalue+=1
if matrix[ident+9]=='x':
cellvalue+=1
if matrix[ident+11]=='x':
cellvalue+=1
if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
identifier.append(ident+1)
identifier.append(ident-1)
identifier.append(ident+10)
identifier.append(ident+9)
identifier.append(ident+11)
if ident>90 and ident<99: #checking bot row +1 -1 -10 -9 -11
if matrix[ident-1]=='x':
cellvalue+=1
if matrix[ident+1]=='x':
cellvalue+=1
if matrix[ident-10]=='x':
cellvalue+=1
if matrix[ident-9]=='x':
cellvalue+=1
if matrix[ident-11]=='x':
cellvalue+=1
if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
identifier.append(ident+1)
identifier.append(ident-1)
identifier.append(ident-10)
identifier.append(ident-9)
identifier.append(ident-11)
for i in range(1,10,1):
if ident==i*10: #checking left side +1 -10 +10 -9 +11
if matrix[ident-1]=='x':
cellvalue+=1
if matrix[ident+10]=='x':
cellvalue+=1
if matrix[ident-10]=='x':
cellvalue+=1
if matrix[ident-9]=='x':
cellvalue+=1
if matrix[ident+11]=='x':
cellvalue+=1
if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
identifier.append(ident+1)
identifier.append(ident-10)
identifier.append(ident-9)
identifier.append(ident+10)
identifier.append(ident+11)
if ident==(i+9)*10: # checking right side -1 -10 +10 +9 -11
if matrix[ident-1]=='x':
cellvalue+=1
if matrix[ident-10]=='x':
cellvalue+=1
if matrix[ident+10]=='x':
cellvalue+=1
if matrix[ident+9]=='x':
cellvalue+=1
if matrix[ident-11]=='x':
cellvalue+=1
if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
identifier.append(ident-1)
identifier.append(ident-1)
identifier.append(ident+10)
identifier.append(ident+9)
identifier.append(ident-11)
if ident<i*10 and ident<(i+9)*10: #checking +1 -1 -9 -11 -10 +9 +10 +11
if matrix[ident+1]=='x':
cellvalue+=1
if matrix[ident-1]=='x':
cellvalue+=1
if matrix[ident-9]=='x':
cellvalue+=1
if matrix[ident-11]=='x':
cellvalue+=1
if matrix[ident-10]=='x':
cellvalue+=1
if matrix[ident+9]=='x':
cellvalue+=1
if matrix[ident+10]=='x':
cellvalue+=1
if matrix[ident+11]=='x':
cellvalue+=1
if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
identifier.append(ident+1)
identifier.append(ident-1)
identifier.append(ident-9)
identifier.append(ident-11)
identifier.append(ident-10)
identifier.append(ident+9)
identifier.append(ident+10)
identifier.append(ident+11)
grid[ident]=str(cellvalue)
return grid发布于 2016-04-30 08:53:58
数字大于9的原因可能是这一行:
if ident<i*10 and ident<(i+9)*10: #checking +1 -1 -9 -11 -10 +9 +10 +11为了有意义,您需要切换第一个比较。否则,第二个条件是无用的,这可能不是,你想要的。
if ident>i*10 and ident<(i+9)*10: #checking +1 -1 -9 -11 -10 +9 +10 +11https://stackoverflow.com/questions/36950269
复制相似问题