简体   繁体   中英

IndexError I just can't figure out (Python)

I'm trying to make a minesweeper game through text in python. This error comes up when I try to draw the little numbers. Maybe the way I'm doing this is inefficient, but I don't understand why it throws this error. I've been trying to fiddle with the code, but nothing seems to work. Does anyone have any idea for why it's not working?

import random

minenum = 12
gridsize = 10

grid = [[0] * gridsize for _ in range(gridsize)]

def setup():
    global grid
    global minecount
    for i in range(minenum):
        x = random.randrange(0,10)
        y = random.randrange(0,10)
        grid[y][x] = "m"

    xpos = 0
    ypos = 0

    for n in range(10):
        for z in range(10):
            count = 0
            try:
                if grid[ypos + 1][xpos] == "m":
                    count += 1
            except:
                pass
            try:
                if grid[ypos + 1][xpos + 1] == "m":
                    count += 1
            except:
                pass
            try:
                if grid[ypos + 1][xpos - 1] == "m":
                    count += 1
            except:
                pass
            try:
                if grid[ypos - 1][xpos + 1] == "m":
                    count += 1
            except:
                pass
            try:
                if grid[ypos - 1][xpos - 1] == "m":
                    count += 1
            except:
                pass
            try:
                if grid[ypos - 1][xpos] == "m":
                    count += 1
            except:
                pass
            try:
                if grid[ypos][xpos + 1] == "m":
                    count += 1
            except:
                pass
            try:
                if grid[ypos][xpos - 1] == "m":
                    count += 1
            except:
                pass

            grid[ypos][xpos] = count

            xpos += 1
        ypos += 1
        
                

def printBoard():
    for i in range(10):
        print(' '.join(str(v) for v in grid[i]))

setup()
printBoard()

[Edit]

here is the error:

Traceback (most recent call last):
  File "main.py", line 74, in <module>
    setup()
  File "main.py", line 63, in setup
    grid[ypos][xpos] = count
IndexError: list assignment index out of range

Your code was not working because you never reset xpos when you incremented your ypos so your indexes looked like this (for gridsize = 4):

0 0
1 0
2 0
3 0
4 1
5 1
6 1
7 1
8 2

Instead of what you inteded, ie

0 0
1 0
2 0
3 0
0 1
1 1
2 1
3 1
0 2

You should have added xpos = 0 whenever you do ypos += 1

        xpos += 1
    ypos += 1
    xpos = 0

Your code could also use a bit of cleanup:

import random


def setup(minecount, gridsize):
    grid = [[0] * gridsize for _ in range(gridsize)]

    for _ in range(minecount):
        x = random.randrange(0,gridsize)
        y = random.randrange(0,gridsize)
        grid[y][x] = "m"

    for xpos in range(gridsize):
        for ypos in range(gridsize):
            count = 0
            if ypos + 1 < 10 and grid[ypos + 1][xpos] == "m":
                count += 1
            if ypos + 1 < 10 and xpos +1 < 10 and grid[ypos + 1][xpos + 1] == "m":
                count += 1
            if ypos + 1 < 10 and xpos - 1 >= 0 and grid[ypos + 1][xpos - 1] == "m":
                count += 1
            if ypos - 1 >= 0 and xpos + 1 < 10 and grid[ypos - 1][xpos + 1] == "m":
                count += 1
            if ypos - 1 >= 0 and xpos -1 >= 10 and grid[ypos - 1][xpos - 1] == "m":
                count += 1
            if ypos - 1 >= 0 and grid[ypos - 1][xpos] == "m":
                count += 1
            if xpos + 1 < 10 and grid[ypos][xpos + 1] == "m":
                count += 1
            if xpos - 1 >= 0 and grid[ypos][xpos - 1] == "m":
                count += 1
            grid[ypos][xpos] = count
    return grid
        
                

def printBoard(grid):
    for i in range(10):
        print(' '.join(str(v) for v in grid[i]))

minecount = 12
gridsize = 10


grid = setup(minecount, gridsize)
printBoard(grid)

Didn't even have to change the logic, just switched magic numbers to proper parameters. You are also overwriting all your "m" cells when counting neighbouring bombs, you might want to avoid that.

You have to assign zero to xpos in the end of setup() function:

    ypos += 1
    xpos = 0

If you add a print(count) before the grid[ypos][xpos] = count you will see that you have 11 instances of count but grid is only 10 so that's why.

You add to the ypos and xpos even when it's at max, a quick fix below but can be better:

    print(count)
    grid[ypos][xpos] = count

    if xpos < gridsize - 1:
        xpos += 1
if ypos < gridsize - 1:
    ypos += 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