5

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!

RB.
  • 36,301
  • 12
  • 91
  • 131
Anteara
  • 729
  • 3
  • 14
  • 33
  • 1
    Why do you keep two different arrays? Isn't it better to create an item model and create and array of this model? What do you mean by "but it doesn't do anything"? – Amiram Korach Oct 31 '12 at 14:42
  • I'm in the process of cleaning up my code, and I was given the idea of using the 'cells' array which has enum Celltype in a previous question of mine, and I had the first array beforehand. I could possible combine the two. ``playingField`` contains the button names, and ``cells`` contains the state of the button. I'm not sure if it would be possible to combine them. But I think it might be. Could you please elaborate on what you mean by an 'item model'? Thanks. – Anteara Oct 31 '12 at 15:03
  • I mean that you can create a class that has the button name and the cell type and make an array of this class – Amiram Korach Oct 31 '12 at 16:26
  • @AmiramKorach I am not sure why you think making a single array of classes is better than 2 arrays? In many cases it would use more memory and perform worse than keeping two arrays. – Ed Bayiates Oct 31 '12 at 16:29
  • @AresAvatar, I don't think a 9*9 array will consume so much memory. He should consider only simplicity. – Amiram Korach Oct 31 '12 at 16:31
  • @AmiramKorach using one array certainly is not simpler, either. You must create a class to hold values and then all your loops have longer addressing instead of simply using 2 arrays. – Ed Bayiates Oct 31 '12 at 17:38

1 Answers1

2

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.

Ed Bayiates
  • 11,060
  • 4
  • 43
  • 62