/// <summary> /// The display grid. /// </summary> /// <param name="grid"> /// The game grid. /// </param> public void DisplayGrid(IMinesweeperGrid grid) { if (this.isRealView) { var sb = new StringBuilder(); for (var i = 0; i < grid.Rows; i++) { for (var j = 0; j < grid.Cols; j++) { if (grid.IsCellRevealed(i, j)) { if (grid.HasCellBomb(i, j)) { sb.Append("*"); } else if (grid.NeighborMinesCount(i, j) == 0) { sb.Append(" "); } else { sb.Append(grid.NeighborMinesCount(i, j).ToString()); } } else if (grid.IsCellProtected(i, j)) { sb.Append("~"); } else { sb.Append('▒'); } } } if (this.gridBox == null) { this.gridBox = new ConsoleBox<ConsoleColor>( 10, 10, grid.Rows + 1, grid.Cols + 1, ConsoleColor.Cyan, ConsoleColor.DarkRed, sb.ToString()); this.consoleWrpView.Clear(); ConsolePrinter.PrintGrid( this.consoleWrpView, this.gridBox, this.OpenCellEvent, this.ProtectCellEvent); } this.gridBox.Text = sb.ToString(); ConsolePrinter.Print(this.consoleWrpView, this.gridBox); } }
/// <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; } }
/// <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> /// Initializes a new instance of the <see cref="MinesweeperGameController"/> class. /// </summary> /// /// <param name="gameGrid"> /// The game grid. /// </param> /// <param name="gameView"> /// The game view. /// </param> /// <param name="timer"> /// The timer. /// </param> /// /// <param name="players"> /// The players list. /// </param> /// /// <param name="type"> /// The game's difficulty type. /// </param> public MinesweeperGameController( IMinesweeperGrid gameGrid, IMinesweeperView gameView, IMinesweeperTimer timer, List<MinesweeperPlayer> players, MinesweeperDifficultyType type) { if (gameGrid == null) { throw new ArgumentNullException("gameGrid"); } if (gameView == null) { throw new ArgumentNullException("gameView"); } if (timer == null) { throw new ArgumentNullException("timer"); } if (players == null) { throw new ArgumentNullException("players"); } // Initialize objects this.grid = gameGrid; this.gameView = gameView; this.minesweeperTimer = timer; this.players = players; this.type = type; // Handle all view callbacks this.gameView.OpenCellEvent += this.GameViewOnOpenCellEvent; this.gameView.ProtectCellEvent += this.GameViewOnProtectCellEvent; this.gameView.AddPlayerEvent += this.GameViewOnAddPlayerEvent; this.gameView.ShowScoreBoardEvent += this.GameViewOnShowScoreBoardEvent; // Handle all grid callbacks this.grid.BoomEvent += this.GridOnBoomEvent; this.gameView.DisplayGrid(this.grid); this.isGameStarted = false; }