// Field unit rightclick event (setting flag) private void FieldUnit_right_click(object sender, System.EventArgs e) { FieldUnit fu = sender as FieldUnit; // If it is flag, remove it and return if (fu.Flag) { fu.Flag = false; fu.Content = ""; mineCounter++; txtMineCounter.Text = mineCounter.ToString(); return; } // If mine counter is 0 then can't put more flags if (mineCounter == 0) { return; } fu.Flag = true; // Attaches flag image to the button fu.Content = new Image { Source = new BitmapImage(new Uri("Resources/flag.png", UriKind.Relative)), VerticalAlignment = VerticalAlignment.Center }; mineCounter--; txtMineCounter.Text = mineCounter.ToString(); }
// Field unit click event private void FieldUnit_click(object sender, System.EventArgs e) { FieldUnit fu = sender as FieldUnit; // Checking if it's start of game if (startOfGame) { startOfGame = false; dTimer.Start(); int indexOfFirstFieldUnit = Field.Children.IndexOf(fu); InitializeField(indexOfFirstFieldUnit); } if (fu.Flag) { return; } OpenField(fu); // Check if all units that does not have mine are opened if (AllOpened()) { GameWon(); } }
// Creating field units private void PrepareField() { this.startOfGame = true; for (int i = 0; i < this.fieldWidth; i++) { for (int j = 0; j < this.fieldWidth; j++) { FieldUnit fu = new FieldUnit(false, 0, i, j); listOfUnits.Add(fu); fu.Style = FindResource("FieldUnitStyle") as Style; Grid.SetRow(fu, i); Grid.SetColumn(fu, j); Field.Children.Add(fu); // Adding field unit events fu.Click += FieldUnit_click; fu.PreviewMouseLeftButtonDown += delegate { if (fu.Flag) { return; } btnSmiley.Content = smiley_click; }; fu.PreviewMouseLeftButtonUp += delegate { btnSmiley.Content = smiley; }; fu.MouseRightButtonUp += FieldUnit_right_click; } } }
// Ending the game (stepped on mine) private void GameOver(FieldUnit fu = null) { dTimer.Stop(); if (fu != null) { fu.IsEnabled = false; // Attaches mine-blown image to the button fu.Content = new Image { Source = new BitmapImage(new Uri("Resources/mine-blown.png", UriKind.Relative)), VerticalAlignment = VerticalAlignment.Center }; } // Show other mines and disable filed units foreach (FieldUnit funit in listOfUnits) { funit.IsEnabled = false; if (funit.Bomb) { if (funit == fu || funit.Flag) { continue; } funit.Content = new Image { Source = new BitmapImage(new Uri("Resources/mine.png", UriKind.Relative)), VerticalAlignment = VerticalAlignment.Center }; } // If unit does not containt bomb and has a flag marker, put notMine image to it else { if (funit.Flag) { funit.Content = new Image { Source = new BitmapImage(new Uri("Resources/notMine.png", UriKind.Relative)), VerticalAlignment = VerticalAlignment.Center }; } } } // Putting dead smiley face on smiley button btnSmiley.Content = new Image { Source = new BitmapImage(new Uri("Resources/Smiley-dead.png", UriKind.Relative)), VerticalAlignment = VerticalAlignment.Center }; }
// Initializing field units with bombs and other data such as nearby bombs private void InitializeField(int indexOfFirstFieldUnit) { this.rnd = new Random(); int fieldUnitNumber = (int)Math.Pow(this.fieldWidth, 2); List <int> listOfBombs = new List <int>(this.fieldWidth); // First opened field unit can't have the bomb listOfBombs.Add(indexOfFirstFieldUnit); int rng; for (int k = 0; k < this.bombNumber; k++) { rng = rnd.Next(0, fieldUnitNumber - 1); if (listOfBombs.Contains(rng)) { k--; continue; } else { listOfBombs.Add(rng); listOfUnits[rng].Bomb = true; listOfUnits[rng].NearbyBombs = -1; listOfUnits[rng].IsOpened = false; int row = Grid.GetRow(listOfUnits[rng]); int col = Grid.GetColumn(listOfUnits[rng]); // updating 'nearby bombs' value to units around bomb-initialized unit for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { int r = row + i; int c = col + j; if (r < 0 || r > fieldWidth - 1 || c < 0 || c > fieldWidth - 1) { continue; } FieldUnit unit = FieldUnit.GetUnit(listOfUnits, r, c); if (unit.Bomb || (i == 0 && j == 0)) { continue; } unit.NearbyBombs++; } } } } }
// Recursive function that opens field units private void OpenField(FieldUnit fu) { fu.IsOpened = true; int row = Grid.GetRow(fu); int column = Grid.GetColumn(fu); TextBlock tb = new TextBlock { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center }; if (fu.Bomb) { GameOver(fu); return; } else { tb.Text = fu.NearbyBombs.ToString(); } SetTbStyle(tb); Field.Children.Remove(fu); Border border = new Border { BorderThickness = new Thickness(1), BorderBrush = new SolidColorBrush(Colors.LightGray), Child = tb }; Grid.SetRow(border, row); Grid.SetColumn(border, column); Field.Children.Add(border); // Opening adjacent field units if field is empty if (fu.NearbyBombs == 0) { for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { int r = row + i; int c = column + j; if (r < 0 || r > fieldWidth - 1 || c < 0 || c > fieldWidth - 1 || (i == 0 && j == 0)) { continue; } FieldUnit unit = FieldUnit.GetUnit(listOfUnits, r, c); // If it's already open or if it's marked with flag, continue if (unit.IsOpened || unit.Flag) { continue; } else { OpenField(unit); } } } } else { return; } }