Question

I need to figure out how to get an element on an enum in an array. Basically, I have a grid of 9x9 buttons. I have two multi-dimensional arrays that houses these values. One houses their names (if the name is 43) it means 5 down, 4 across (because they start at 0). The name is also the same as the ELEMENT of itself in the array.

string[,] playingField = new string[9, 9];
enum CellType { Empty, Flag, Hidden, Bomb }
CellType[,] cells = new CellType[9, 9];

the names of the buttons are held in playingField.
the status of each cell is held in cells (if it is empty, has a bomb, etc.)

Credit to AbdElRaheim for giving the above. The reason I'm doing this is so I can get a button name (exactly the same as the element name) which will be the same in both arrays.
For example: I can do this:

string dim1 = Convert.ToString(btn.Name[0]);
string dim2 = Convert.ToString(btn.Name[1]);
if (cells[Convert.ToInt32(dim1), Convert.ToInt32(dim2)] == CellType.Bomb)

(please excuse my terrible converting. i'll fix that up later ;)) and what the above does is it allows me to see if a cell that you click has a bomb under it.

However, what I need to do now, is essentially reverse of this. In the above I know the element name that I want to compare it to, because the element name is the same as the button name. However, now what I need to do is FIND the element name (button name) by getting the element of all elements that are Bomb in cells.

I'm not sure how to do this, I tried:

foreach (CellType Bomb in cells)
{

but it doesn't do anything. I need to find all 'bomb' in 'cells' and return the element name. That way I can use that element name, convert it to string, and use my StringToButton method to create a reference to the button.

This is the way I'm currently doing it, for reference, and to help you understand a little better, but take note this is NOT the way I want to continue doing it. I want to do it the way I asked about above :)

foreach (string i in minedNodes)
{
    Button buttonName = StringToButton(Convert.ToString(i));
    buttonName.Image = new Bitmap(dir + "mine.png");
}

Thanks!

Was it helpful?

Solution

If you are looking for a way to traverse your cells array, you would do this:

int oi, ii;
for (oi = 0; oi <= cells.GetUpperBound(0); ++oi)
{
    for (ii = 0; ii <= cells.GetUpperBound(1); ++ii)
    {
        System.Diagnostics.Debug.WriteLine(
            "Checking: " + oi + "," + ii + " : " + cells[oi, ii].ToString()
        );
    }
}

You can then save a list of references to cells[oi, ii] contents which match your desired value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top