扫雷 python,检查相邻单元格的内容时出现奇怪的结果
Minesweeper python, weird results when checking for contents of neighbouring cells
完整代码here
我正在尝试检查邻居是否有用户呼叫的小区。编辑:“抱歉不清楚。问题是当我检查例如单元格 46 时。我想知道周围的 8 个单元格中有多少是地雷。不幸的是,很多单元格都没有这样做,我不明白为什么。通常它例如,给我 2 位数字。
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
你得到大于 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 +11
完整代码here 我正在尝试检查邻居是否有用户呼叫的小区。编辑:“抱歉不清楚。问题是当我检查例如单元格 46 时。我想知道周围的 8 个单元格中有多少是地雷。不幸的是,很多单元格都没有这样做,我不明白为什么。通常它例如,给我 2 位数字。
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
你得到大于 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 +11