简体   繁体   English

python3 minesweeper排序中的嵌套列表无法正常工作

[英]nested list in python3 minesweeper sorting doesn't work correctly

Ok guys so i got some help from you earlier. 好的,所以我早些时候得到了您的帮助。 But right now i am banging my head against the table. 但是现在我正在把头撞在桌子上。

The code works 'kinda'. 该代码的工作方式是“ kinda”。 But i don't want the last numbers of the list to add to the first number of another list. 但我不希望列表的最后一个数字添加到另一个列表的第一个数字。 Hard for me to explain so i´ll show you further down! 我很难解释,因此我将进一步介绍给您!

all the zeroes adjacent to the 'M'´s is supposed to change. 所有与“ M”相邻的零都应该改变。 but some are getting too high numbers. 但是有些数字太高了。

heres the code: 这是代码:

hiddenfield =  [[0, 0, 0, 0, 0, 'M', 0, 0, 0, 'M'],
            [0, 0, 0, 'M', 0, 0, 0, 'M', 'M', 'M'],
            [0, 0, 'M', 0, 'M', 0, 0, 'M', 0, 'M'],
            [0, 0, 0, 0, 'M', 0, 0, 0, 0, 0],
            [0, 0, 'M', 0, 'M', 0, 0, 0, 0, 0],
            [0, 0, 0, 'M', 0, 'M', 'M', 0, 0, 0],
            ['M', 0, 0, 'M', 0, 0, 0, 'M', 0, 0],
            [0, 0, 0, 'M', 0, 0, 0, 0, 0, 0],
            [0, 'M', 'M', 0, 'M', 0, 0, 0, 0, 'M'],
            ['M', 0, 0, 'M', 0, 0, 0, 0, 'M', 0]] 



def update_numbers(x, y, hiddenfield):
    try:
        if hiddenfield[x][y] != 'M':
            hiddenfield[x][y] += 1
    except IndexError:
        pass

def numbersinhiddefield(indexes):
    indexes = [[i,j] for i,row in enumerate(indexes) for j,elem in enumerate(row) if elem == 'M']
    loop = 0
    while loop < len(indexes):
        update_numbers(indexes[loop][0]+1,indexes[loop][1], hiddenfield)
        update_numbers(indexes[loop][0]-1,indexes[loop][1], hiddenfield)
        update_numbers(indexes[loop][0],indexes[loop][1]+1, hiddenfield)
        update_numbers(indexes[loop][0],indexes[loop][1]-1, hiddenfield)
        update_numbers(indexes[loop][0]+1,indexes[loop][1]+1, hiddenfield)
        update_numbers(indexes[loop][0]-1,indexes[loop][1]-1, hiddenfield)
        update_numbers(indexes[loop][0]+1,indexes[loop][1]-1, hiddenfield)
        update_numbers(indexes[loop][0]-1,indexes[loop][1]+1, hiddenfield)
        loop += 1

def showMineFieldHidden(hiddenfield):
    border = list(range(0,len(hiddenfield)))
    row = [' ']+border
    i = 0
    for rows in [border]+hiddenfield:
        print(row[i], end=' ')
        i += 1
        for lines in rows:
            print(lines, end=' ')
        print()

numbersinhiddefield(hiddenfield)

showMineFieldHidden(hiddenfield)

But the thing i get out isn't just right: 但是我得到的不只是正确的:

  0 1 2 3 4 5 6 7 8 9 
0 0 0 1 1 2 M 2 2 4 M 
1 0 1 2 M 3 2 3 M M M 
2 0 1 M 4 M 2 2 M 5 M 
3 0 2 2 5 M 3 1 1 2 1 
4 0 1 M 4 M 4 2 1 0 0 
5 1 2 3 M 4 M M 2 1 1 
6 M 1 3 M 4 2 3 M 1 1 
7 2 3 4 M 3 1 1 1 2 2 
8 2 M M 4 M 1 0 1 2 M 
9 M 3 3 M 3 2 1 1 M 4 <-- this is supposed to be a 2.
            ^
            this is supposed to be a 1 and the one next to it a zero

I think it is adding from the borders to the lower list? 我认为它是从边界添加到较低的列表?

Would appreciate som help! 希望索姆帮助!

Your problem is that when you have an 'M' in row 0 you use that to add to the count for row -1 . 您的问题是,当第0行中有一个'M'时,您会使用它添加到第-1行的计数中。 An index of -1 is an alternate way to refer to the last row. 索引-1是引用最后一行的另一种方法。 Similary for column 0 you add to the count for column -1 . 将第0列的相似度添加到第-1列的计数中。

This means that an 'M' on the top row counts for the bottom row, and an 'M' in the left column counts for the right column. 这意味着最上面一行的“ M”代表最下面一行,而最左边一行的“ M”代表最右边一行。

If you modify updatenumbers() to range check the coordinates you should be able to avoid the double counting. 如果修改updatenumbers()进行范围检查坐标,则应该能够避免重复计算。 That might also look cleaner than the exception handling. 这看起来也比异常处理干净。

def update_numbers(x, y, hiddenfield):
    if 0 <= x < len(hiddenfield) and 0 <= y < len(hiddenfield[x]):
        if hiddenfield[x][y] != 'M':
            hiddenfield[x][y] += 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM