首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扫雷船蟒蛇,在检查邻近细胞的含量时会产生奇怪的结果

扫雷船蟒蛇,在检查邻近细胞的含量时会产生奇怪的结果
EN

Stack Overflow用户
提问于 2016-04-30 03:10:32
回答 1查看 262关注 0票数 0

完整代码这里,我正在尝试检查邻居是否有用户调用的单元格。编辑:“对不起,不清楚。问题是,当我检查单元格46时。我想知道周围8个单元中有多少是地雷。不幸的是,许多单元格没有这样做,我也不明白为什么。例如,它经常给我两个数字。”

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-30 08:53:58

数字大于9的原因可能是这一行:

代码语言:javascript
复制
if ident<i*10 and ident<(i+9)*10: #checking +1 -1 -9 -11 -10 +9 +10 +11

为了有意义,您需要切换第一个比较。否则,第二个条件是无用的,这可能不是,你想要的。

代码语言:javascript
复制
if ident>i*10 and ident<(i+9)*10: #checking +1 -1 -9 -11 -10 +9 +10 +11
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36950269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档