/// <summary> /// The update cell button. /// </summary> /// <param name="grid"> /// The game grid. /// </param> /// <param name="button"> /// The button. /// </param> private void UpdateCellButton(IMinesweeperGrid grid, WpfMinesweeperButton button) { var i = button.Row; var j = button.Col; var color = new System.Windows.Media.Color(); if (grid.IsCellProtected(i, j)) { button.Background = this.images[1]; } else if (grid.IsCellRevealed(i, j)) { button.ClearValue(Control.BackgroundProperty); button.Background = new SolidColorBrush(Colors.LightGray); if (grid.HasCellBomb(i, j)) { button.Background = this.images[0]; } else { switch (grid.NeighborMinesCount(i, j)) { case 1: color = Colors.Blue; break; case 2: color = Colors.Green; break; case 3: color = Colors.Red; break; case 4: color = Colors.Indigo; break; case 5: color = Colors.DarkSlateGray; break; case 6: color = Colors.DarkRed; break; case 7: color = Colors.DarkBlue; break; case 8: color = Colors.Black; break; } if (grid.NeighborMinesCount(i, j) != 0) { button.Content = grid.NeighborMinesCount(i, j).ToString(); button.Foreground = new SolidColorBrush(color); } } button.IsHitTestVisible = false; } else { button.Background = new SolidColorBrush(Colors.AliceBlue); button.Content = string.Empty; } }
/// <summary> /// The display grid. /// </summary> /// <param name="grid"> /// The game grid. /// </param> public void DisplayGrid(IMinesweeperGrid grid) { var rows = grid.Rows; var cols = grid.Cols; if (grid.NeighborMinesCount(this.lastRow, this.lastCol) == 0) { this.isGridInitialized = false; } if (this.isGridInitialized) { var i = this.lastRow; var j = this.lastCol; var button = this.buttons.First(x => x.Name == "Button_" + i.ToString() + "_" + j.ToString()); this.UpdateCellButton(grid, button); } else { this.buttons.Clear(); this.win.Children.Clear(); for (var i = 0; i < rows; i++) { for (var j = 0; j < cols; j++) { var button = new WpfMinesweeperButton { Row = rows, Col = cols }; button.Name = "Button_" + i + "_" + j; button.HorizontalAlignment = HorizontalAlignment.Left; button.VerticalAlignment = VerticalAlignment.Top; button.Width = CellHeightAndWidth; button.Height = CellHeightAndWidth; button.Margin = new Thickness(i * CellHeightAndWidth, j * CellHeightAndWidth, 0, 0); button.Click += this.CellButtonClick; button.MouseRightButtonUp += this.ButtonOnMouseRightButtonUp; button.Row = i; button.Col = j; this.UpdateCellButton(grid, button); this.win.Children.Add(button); this.buttons.Add(button); } } this.isGridInitialized = true; } }