Home > Software engineering >  Creating a Map Grid WinForms.... Arrays
Creating a Map Grid WinForms.... Arrays

Time:11-21

First Post. Basically I am trying create a map grid based on the array in Winforms. I'm failing though. This is what I've got so far

The problem is it displays everything as one color. Whereas I want it to be a certain color based on the array.

The issue I think is the foreach loop in the for loop. As the foreach loop is within the second for loop, it's probably going to repeat for ever i and then for every j.

But I am too dumb to know how to sort it. I know winforms is not ideal for this and I do not really want to redo the code I just kinda wanna make this work if possible.


namespace Test { public partial class Form1 : Form { int TILE_SIZE = 64; int MAP_NUM_ROWS = 11; int MAP_NUM_COLS = 15; List items = new List();

    int[] mapGrid =
    {
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1,
        1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1,
        1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1,
        1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1,
        1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1,
        1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
        1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
        1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1,
        1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1

};




    public Form1()
    {
        InitializeComponent();
        newTest();
        
    }

    private void newTest()
    {


        for (var i = 0; i < MAP_NUM_ROWS; i  ) //
        {

            for (var j = 0; j < MAP_NUM_COLS; j  )

            {
                PictureBox Walls = new PictureBox();
                Walls.Location = new Point(i * TILE_SIZE, j * TILE_SIZE);  
                Walls.Height = TILE_SIZE;
                Walls.Width = TILE_SIZE;
                Walls.BorderStyle = BorderStyle.FixedSingle;




                foreach (int test in mapGrid)

                {


                    if (test == 0)
                    {

                        Walls.BackColor = Color.Red;
                        items.Add(Walls);
                        this.Controls.Add(Walls);



                    }

                    if (test == 1)
                    {

                        Walls.BackColor = Color.Orange;
                        items.Add(Walls);
                        this.Controls.Add(Walls);


                    }
                }


                }

            }

        }
    }
}

enter image description here

I've tried using Panels and Flowlayoutpanels and TableLayout panels. The issue seems to be the loops and I am too tired to see the solution.

CodePudding user response:

you should not be looping over the grid inside that main outer loop. YOu want

  int test = mapGrid[i*MAP_NUM_COLS   j]

ie , pick out the cell that matched the row / col you are filling

  • Related