Index outside bounds of array

rubenarrr

New member
Joined
Mar 22, 2014
Messages
3
Programming Experience
Beginner
Im making a game and after the first run run when the user clicks new game I get index outside bounds of array.

This is the the new game button code
C#:
            panel1.Controls.Clear();
                  
            int x = int.Parse(heightentered.Text);
            int y = int.Parse(widthentered.Text);
            int density = int.Parse(blockedpercentageentered.Text); 
            uniwidth = y - 1;
            uniheight = x - 1;
            // 2d string array. Feel free to edit
            positions = new Label[y, x];
            empty = new int[y, x];
                       
            for (int i = 0; i < y; i++)
            {
                for (int j = 0; j < x; j++)
                {
                    Label gridmaze = new Label();
                    gridmaze.Size = new Size(8, 8);
                    gridmaze.Location = new Point(20 + (12 * i), 120 + (12 * j));
                    
                    gridmaze.Tag = new Random(Guid.NewGuid().GetHashCode()).Next(0, 100);
                    if (int.Parse(gridmaze.Tag.ToString()) < density)
                    {
                        
                        gridmaze.BackColor = Color.Red;
                        empty[i, j] = 1;
                    }
                    else
                    {
                        gridmaze.BackColor = Color.Yellow;
                        empty[i, j] = 0;
                    }
                    positions[i, j] = gridmaze;
                   
                    panel1.Controls.Add(gridmaze);
                }
            }
            
            //set the borders to red
            for (int i = 0; i < y; i++)
            {
                positions[i, 0].BackColor = Color.Red;
                positions[i, x - 1].BackColor = Color.Red;
                empty[i, 0] = 1;
                empty[i, x - 1] = 1;


            }
            for (int i = 0; i < x; i++)
            {
                positions[y - 1, i].BackColor = Color.Red;
                positions[0, i].BackColor = Color.Red;
                empty[0, i] = 1;
                empty[y - 1, i] = 1;
            }


            //creates a start and end point
             positions[1,5].BackColor = positions[2, 5].BackColor
            = positions[3, 5].BackColor = Color.Yellow;
             empty[0,5] = empty[uniwidth,4] = empty[1, 5] = empty[2, 5] = empty[3,5] = 0;
             positions[uniwidth - 2, 4].BackColor = positions[uniwidth - 1, 4].BackColor = positions[uniwidth, 4].BackColor = Color.Yellow;
            positions[0, 5].BackColor = positions[uniwidth, 5].BackColor = Color.Violet;

I try to call a clear function that sets both my empty, and positions arrays to null but I still get the same error. Im not sure why this happens because even if it wasn't empty would it now just override whats already in there?
Any help would be appreciated.
 
Back
Top Bottom