Guest User

Untitled

a guest
Oct 31st, 2012
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | None | 0 0
  1.         private void UncoverTile(Button btn)
  2.         {
  3.             //Checks if theres a mine under the tile you pressed
  4.             if (minedNodes.Contains(btn.Name))
  5.             {
  6.                 //This is simply to show all the mines once the game is finished (either a win or a loss). Only coded for a loss so far.
  7.                 foreach (string button in minedNodes)
  8.                 {
  9.                     Button buttonName = StringToButton(button);
  10.                     buttonName.Image = new Bitmap(dir + "mine.png");
  11.                 }
  12.  
  13.                 DialogResult playAgain = MessageBox.Show("You hit a mine! Do you want to play again?", "You hit a mine :(", MessageBoxButtons.YesNo);
  14.                 if (playAgain == DialogResult.Yes)
  15.                 {
  16.                     playButton_Click(this, null);
  17.                 }
  18.                 else
  19.                 {
  20.                     stopButton_Click(this, null);
  21.                 }
  22.             }
  23.                 //If there isn't then we do the actual logic here.
  24.             else
  25.             {
  26.                 string buttonName = btn.Name;
  27.  
  28.                 //USE LOOPS LATER TO SIMPLIFY THE CODE
  29.  
  30.                 //set up the variables for the logic
  31.                 //Calculates all 8 adjacent tiles
  32.                 char btnLetter = Convert.ToChar(buttonName.Substring(0, buttonName.Length - 1));
  33.                 char aboveLetter = btnLetter; aboveLetter--;
  34.                 char belowLetter = aboveLetter; belowLetter++; belowLetter++;
  35.                 int upDown = Convert.ToInt32(buttonName.Substring(1));
  36.                 int leftSide = Convert.ToInt32(buttonName.Substring(1)) - 1;
  37.                 int rightSide = Convert.ToInt32(buttonName.Substring(1)) + 1;
  38.                
  39.                 //Clear the list by making a new list before adding the new values as to not overlap.
  40.                 adjacence = new List<String>();
  41.  
  42.                 //Put all adjacent sides in to a list
  43.                 adjacence.Add(Convert.ToString(btnLetter.ToString() + leftSide));
  44.                 adjacence.Add(Convert.ToString(btnLetter.ToString() + rightSide));
  45.                 adjacence.Add(Convert.ToString(aboveLetter.ToString() + leftSide));
  46.                 adjacence.Add(Convert.ToString(aboveLetter.ToString() + rightSide));
  47.                 adjacence.Add(Convert.ToString(belowLetter.ToString() + leftSide));
  48.                 adjacence.Add(Convert.ToString(belowLetter.ToString() + rightSide));
  49.                 adjacence.Add(Convert.ToString(aboveLetter.ToString() + upDown));
  50.                 adjacence.Add(Convert.ToString(belowLetter.ToString() + upDown));
  51.  
  52.  
  53.                 foreach (string i in adjacence)
  54.                 {
  55.                     if (minedNodes.Contains(i))
  56.                     {
  57.                         adjacentMines += 1;
  58.                     }
  59.                 }
  60.  
  61.                 btn.Text = Convert.ToString(adjacentMines);
  62.                 btn.BackColor = System.Drawing.Color.White;
  63.                 //Do stuff to uncover the tiles
  64.                 adjacentMines = 0;
  65.             }
  66.         }
Advertisement
Add Comment
Please, Sign In to add comment