Вопрос

I'm making a minesweeper game, and so far it's completely playable and you can finish the game. However, my solving algorithm sucks. It's pretty messy and I'm looking for a cleaner way, perhaps with loops, but I can't think of anything.

Here is what i'm currently using:

I have four global lists:

    List<String> flaggedButtons = new List<String>();
    List<String> minedNodes = new List<String>();
    List<String> playingField = new List<String>();
    List<String> adjacence = new List<String>();

and this is the method that utilizes the lists to solve the game:

Entire method: http://pastebin.com/7J2Fc8yw

most important parts:

            char btnLetter = Convert.ToChar(buttonName.Substring(0, buttonName.Length - 1));
            char aboveLetter = btnLetter; aboveLetter--;
            char belowLetter = aboveLetter; belowLetter++; belowLetter++;
            int upDown = Convert.ToInt32(buttonName.Substring(1));
            int leftSide = Convert.ToInt32(buttonName.Substring(1)) - 1;
            int rightSide = Convert.ToInt32(buttonName.Substring(1)) + 1;

            //Clear the list by making a new list before adding the new values as to not overlap.
            adjacence = new List<String>();

            //Put all adjacent sides in to a list
            adjacence.Add(Convert.ToString(btnLetter.ToString() + leftSide));
            adjacence.Add(Convert.ToString(btnLetter.ToString() + rightSide));
            adjacence.Add(Convert.ToString(aboveLetter.ToString() + leftSide));
            adjacence.Add(Convert.ToString(aboveLetter.ToString() + rightSide));
            adjacence.Add(Convert.ToString(belowLetter.ToString() + leftSide));
            adjacence.Add(Convert.ToString(belowLetter.ToString() + rightSide));
            adjacence.Add(Convert.ToString(aboveLetter.ToString() + upDown));
            adjacence.Add(Convert.ToString(belowLetter.ToString() + upDown));

Basically this is the part that I need help with. It works, but it's messy. I know I should be using loops and whatnot but I can't figure out a way to loop this to make it cleaner. Essentially what it's doing is it's finding all 8 adjacent tiles around the tile that you press. Any ideas? Thanks :)

Это было полезно?

Решение

Use flood fill. It is the algorithm that MS uses in theirs. Change maybe to an array that stores the cell values without using string.

enum CellType 
{
   Bomb,
   Flag,
   Hidden,  
   Empty
}

CellType[,] cells = new CellType[10,10];

Basically when someone clicks on a cell check to see if it hidden, check to see if the cells around it are hidden. Each cell around it that is hidden you would add to queue or stack and then back track. http://en.wikipedia.org/wiki/Flood_fill

Updated: Here is a complete example in C#. These are recursive instead of using some collection for backtracking https://gamedev.stackexchange.com/questions/31909/best-algorithm-for-recursive-adjacent-tiles

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top