简体   繁体   中英

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'. 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. 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 . An index of -1 is an alternate way to refer to the last row. Similary for column 0 you add to the count for column -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.

If you modify updatenumbers() to range check the coordinates you should be able to avoid the double counting. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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