简体   繁体   中英

Index is not in array issues - C#

I'm currently in the process of making mine sweeper for a class project. I have got most of the fundamentals but when I click on an edge button, because it checks all buttons around it, the code crashes when there is no index value. Eg When x is equal to 10 but the code calls for buttonArray[x + 1, y] . Its a null value and just crashes. Any Ideas on how to fix this? Code is included...

decimal MineCount = 0;
                    #region MineCount
                    if ((buttonArray[x + 1, y] != null) && (isMine[x + 1, y] == true))
                    {
                        
                        isNeighbour[x, y] = true;
                        isBlank[x, y] = false;
                        MineCount += 1m;
                    }
                    if ((buttonArray[x + 1, y + 1] != null) && (isMine[x + 1, y + 1] == true))
                    {
                        
                        isNeighbour[x, y] = true;
                        isBlank[x, y] = false;
                        MineCount += 1m;
                    }
                    if ((buttonArray[x + 1, y - 1] != null) && (isMine[x + 1, y - 1] == true))
                    {
                        
                        isNeighbour[x, y] = true;
                        isBlank[x, y] = false;
                        MineCount += 1m;
                    }
                    if ((buttonArray[x, y + 1] != null) && (isMine[x, y + 1] == true))
                    {
                        
                        isNeighbour[x, y] = true;
                        isBlank[x, y] = false;
                        MineCount += 1m;
                    }
                    if ((buttonArray[x, y - 1] != null) && (isMine[x, y - 1] == true))
                    {
                        
                        isNeighbour[x, y] = true;
                        isBlank[x, y] = false;
                        MineCount += 1m;
                    }
                    if ((buttonArray[x - 1, y - 1] != null) && (isMine[x - 1, y - 1] == true))
                    {
                        
                        isNeighbour[x, y] = true;
                        isBlank[x, y] = false;
                        MineCount += 1m;
                    }
                    if ((buttonArray[x - 1, y] != null) && (isMine[x - 1, y] == true))
                    {
                       
                        isNeighbour[x, y] = true;
                        isBlank[x, y] = false;
                        MineCount += 1m;
                    }
                    if ((buttonArray[x - 1, y + 1] != null) && (isMine[x - 1, y + 1] == true))
                    {
                        
                        isNeighbour[x, y] = true;
                        isBlank[x, y] = false;
                        MineCount += 1m;
                    }

You can use this function for this, instead of duplicate code. The point of the function is to use limited boundaries.

void CountCells()
    {
        for(int ix = Math.Max(x - 1, 0); ix <= Math.Min(x + 1, isMine.GetLength(0)); ix++)
        {
            for(int iy = Math.Max(y - 1, 0); iy <= Math.Min(y + 1, isMine.GetLength(1)); iy++)
            {
                if ((buttonArray[ix,iy] != null) && isMine[ix,iy])
                { 
                    isNeighbour[ix, iy] = true;
                    isBlank[ix, iy] = false;
                    MineCount += 1m;
                }
            }
        }
    }

Hope it's can be helpfull!

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