public void Setup(int width, int height, RoutedEventHandler callback) { Clear(); _buttons = new Button[width, height]; for (int x = 0; x < width; x++) { _grid.ColumnDefinitions.Add(new ColumnDefinition()); for (int y = 0; y < height; y++) { MineButton btn = new MineButton(x, y); btn.Click += callback; btn.MouseRightButtonUp += Flag; Grid.SetColumn(btn, x); Grid.SetRow(btn, y); _grid.Children.Add(btn); _buttons[x, y] = btn; } } for (int y = 0; y < height; y++) { _grid.RowDefinitions.Add(new RowDefinition()); } }
//event handler that manages when a MineButton is right clicked private void GameRightClick(object e, MouseEventArgs args) { MineButton gameButton = (MineButton)e; if (args.Button == MouseButtons.Right) { //toggle whether or not the right clicked MineButton has the Marked status, //and increment or decrement the mine count display int val = int.Parse(mineCount.Text); if (gameButton.State == Status.Clear) { val--; gameButton.Text = "?"; gameButton.ForeColor = Color.Red; gameButton.State = Status.Marked; mineCount.Text = val.ToString(); } else { val++; gameButton.Text = ""; gameButton.State = Status.Clear; mineCount.Text = val.ToString(); } } }
public MinesForm() { InitializeComponent(); const char mineChar = '۞'; var xbtns = (Width - 2 * BtnSize.X) / BtnSize.Width; var ybtns = (Height - 2 * BtnSize.Y) / BtnSize.Height; _mineArray = GenerateMines(xbtns, ybtns, 10); InsertMineNumbers(ref _mineArray); for (var i = 0; i < xbtns; i++) { for (var j = 0; j < ybtns; j++) { var btn = new MineButton(i, j, _mineArray[i, j], mineChar) { Width = BtnSize.Width, Height = BtnSize.Height, Left = i * BtnSize.Width + BtnSize.X, Top = j * BtnSize.Height + BtnSize.Y, TabStop = false, PositionClickedEventHandler = MineButtonClicked, Font = new Font("Arial", 12) }; Controls.Add(btn); Controls[Controls.Count - 1].Show(); } } }
private void gridButtonRightClicked(object sender, MouseButtonEventArgs e) { MineButton button = (MineButton)sender; game.flag(button.getRow(), button.getColumn()); this.renderBoard(); }
private void gridButtonClicked(object sender, EventArgs e) { MineButton button = (MineButton)sender; List <List <Cell> > cells = game.getCells(); game.touch(button.getRow(), button.getColumn()); this.renderBoard(); }
public MineButton GetNeighbor(RelativePositions position) { MineButton neighbor = null; switch (position) { case RelativePositions.Bottom: neighbor = board.GetButton(X, Y + 1); break; case RelativePositions.BottomLeft: neighbor = board.GetButton(X - 1, Y + 1); break; case RelativePositions.BottomRight: neighbor = board.GetButton(X + 1, Y + 1); break; case RelativePositions.Left: neighbor = board.GetButton(X - 1, Y); break; case RelativePositions.Right: neighbor = board.GetButton(X + 1, Y); break; case RelativePositions.Top: neighbor = board.GetButton(X, Y - 1); break; case RelativePositions.TopLeft: neighbor = board.GetButton(X - 1, Y - 1); break; case RelativePositions.TopRight: neighbor = board.GetButton(X + 1, Y - 1); break; } return(neighbor); }
public void Activate() { if (!IsRevealed && !IsFlagged) { IsRevealed = true; BTN.IsEnabled = false; board.UnrevealedButtons.Remove(this); if (IsMine) { BTN.Background = Brushes.Red; Image i = new Image(); i.Source = new BitmapImage(new Uri("Assets/Mine.png", UriKind.Relative)); BTN.Content = i; board.GameOver(); } else { // Set Text to number of neighbors. int Count = board.CountMineNeighbors(X, Y); if (Count > 0) { BTN.Content = Count; BTN.Foreground = Colors[Count - 1]; board.RevealedButtons.Add(this); } else { // 0 neighbors that are mines, so activate each one. foreach (RelativePositions pos in Enum.GetValues(typeof(RelativePositions))) { MineButton b = GetNeighbor(pos); if (b != null && b.IsRevealed == false) { b.Activate(); } } } } } }
private void button1_Click(object sender, EventArgs e) { mineButtons = new MineButton[100]; Random r = new Random(); for (int i = 0; i < 100; i++) { MineButton button = new MineButton { IsBomb = (r.Next(0, 4) == 0) ? true : false, Num = i, }; button.RaiseCustomEvent += CustomClick; button.Text = button.IsBomb ? "" : ""; mineButtons[i] = button; } foreach (MineButton button in mineButtons) { tableLayoutPanelButtons.Controls.Add(button); } }
private void ClearAdjacent(MineButton xy) { for (var i = 0; i < Controls.Count; i++) { if (Controls[i].GetType() != typeof(MineButton)) { continue; } var btn = (MineButton)Controls[i]; if (!btn.Enabled) { continue; } if (!btn.IsAdjacent(xy.GetPosition())) { continue; } if (btn.GetValue() != -1) { btn.PerformClick(); } } }
private void ButtonNewClick(object sender, EventArgs e) { for (var i = 0; i < Controls.Count; i++) { if (Controls[i].GetType() == typeof(MineButton)) { Controls.RemoveAt(i); } } const char mineChar = '۞'; var xbtns = (Width - 2 * BtnSize.X) / BtnSize.Width; var ybtns = (Height - 2 * BtnSize.Y) / BtnSize.Height; _mineArray = GenerateMines(xbtns, ybtns, 10); InsertMineNumbers(ref _mineArray); for (var i = 0; i < xbtns; i++) { for (var j = 0; j < ybtns; j++) { var btn = new MineButton(i, j, _mineArray[i, j], mineChar) { Width = BtnSize.Width, Height = BtnSize.Height, Left = i * BtnSize.Width + BtnSize.X, Top = j * BtnSize.Height + BtnSize.Y, TabStop = false, PositionClickedEventHandler = MineButtonClicked, Font = new Font("Arial", 12) }; Controls.Add(btn); Controls[Controls.Count - 1].Show(); } } }
//colors a button based on its Status public void ColorButton(ref MineButton button) { button.BackColor = Color.LightGray; switch (button.Numadjacent) { case 0: break; case 1: { button.Text = button.Numadjacent.ToString(); button.ForeColor = Color.Blue; break; } case 2: { button.Text = button.Numadjacent.ToString(); button.ForeColor = Color.Green; break; } case 3: { button.Text = button.Numadjacent.ToString(); button.ForeColor = Color.Red; break; } case 4: { button.Text = button.Numadjacent.ToString(); button.ForeColor = Color.Purple; break; } case 5: { button.Text = button.Numadjacent.ToString(); button.ForeColor = Color.Black; break; } case 6: { button.Text = button.Numadjacent.ToString(); button.ForeColor = Color.Maroon; break; } case 7: { button.Text = button.Numadjacent.ToString(); button.ForeColor = Color.Gray; break; } case 8: { button.Text = button.Numadjacent.ToString(); button.ForeColor = Color.Turquoise; break; } } }
public MineModel(int s, int f) { Random rando = new Random(); Buttons = new MineButton[s, s]; Size = s; //build each MineButton, link them together, and place into buttons array //repeat until number of mines is within THRESHOLD of expected do { Nummines = 0; for (int row = 0; row < s; row++) { for (int col = 0; col < s; col++) { bool hasMine = false; if (rando.Next(0, 99) < f) { hasMine = true; Nummines++; } MineButton newButton = new MineButton(row, col, hasMine); if (row > 0) { newButton.up = Buttons[row - 1, col]; Buttons[row - 1, col].down = newButton; } if (col > 0) { newButton.left = Buttons[row, col - 1]; Buttons[row, col - 1].right = newButton; } if (row > 0 && col > 0) { newButton.upleft = Buttons[row - 1, col - 1]; Buttons[row - 1, col - 1].downright = newButton; } if (row > 0 && col < s - 1) { newButton.upright = Buttons[row - 1, col + 1]; Buttons[row - 1, col + 1].downleft = newButton; } Buttons[row, col] = newButton; } } } while (!(s * s * (float)f / 100 - Nummines > -THRESHOLD && s * s * (float)f / 100 - Nummines < THRESHOLD)); //iterate through each MineButton and increment numadjacent for each adjacent entry that isMine == true for (int row = 0; row < s; row++) { for (int col = 0; col < s; col++) { if (row > 0) { if (Buttons[row, col].up.isMine == true) { Buttons[row, col].Numadjacent++; } if (col > 0) { if (Buttons[row, col].upleft.isMine == true) { Buttons[row, col].Numadjacent++; } } if (col < s - 1) { if (Buttons[row, col].upright.isMine == true) { Buttons[row, col].Numadjacent++; } } } if (row < s - 1) { if (Buttons[row, col].down.isMine == true) { Buttons[row, col].Numadjacent++; } if (col > 0) { if (Buttons[row, col].downleft.isMine == true) { Buttons[row, col].Numadjacent++; } } if (col < s - 1) { if (Buttons[row, col].downright.isMine == true) { Buttons[row, col].Numadjacent++; } } } if (col > 0) { if (Buttons[row, col].left.isMine == true) { Buttons[row, col].Numadjacent++; } } if (col < s - 1) { if (Buttons[row, col].right.isMine == true) { Buttons[row, col].Numadjacent++; } } } } }
private void renderBoard() { theGrid.Children.Clear(); minesRemainingLabel.Content = "" + game.getMineCount(); foreach (List <Cell> row in game.getCells()) { foreach (Cell cell in row) { MineButton button = new MineButton(); button.setRow(cell.row); button.setColumn(cell.column); BitmapImage bmp = new BitmapImage(new Uri(@"pack://application:,,,/minesweeper_tiles.jpg", UriKind.Absolute)); ImageBrush brush = new ImageBrush(bmp); brush.TileMode = TileMode.Tile; brush.Stretch = Stretch.Fill; if (cell.touched || game.isGameover()) { if (cell.mine) { Label label = new Label(); brush.Viewbox = new Rect(0.5, 0, 0.25, 0.333); label.Background = brush; label.HorizontalContentAlignment = HorizontalAlignment.Center; label.VerticalContentAlignment = VerticalAlignment.Center; Grid.SetColumnSpan(label, 1); Grid.SetColumn(label, cell.column); Grid.SetRowSpan(label, 1); Grid.SetRow(label, cell.row); theGrid.Children.Add(label); continue; } else if (cell.n == 0) { Label label = new Label(); brush.Viewbox = new Rect(0.75, 0, 0.25, 0.333); label.Background = brush; label.HorizontalContentAlignment = HorizontalAlignment.Center; label.VerticalContentAlignment = VerticalAlignment.Center; Grid.SetColumnSpan(label, 1); Grid.SetColumn(label, cell.column); Grid.SetRowSpan(label, 1); Grid.SetRow(label, cell.row); theGrid.Children.Add(label); continue; } else { switch (cell.n) { case (1): brush.Viewbox = new Rect(0, 0.333, 0.25, 0.333); break; case (2): brush.Viewbox = new Rect(0.25, 0.333, 0.25, 0.333); break; case (3): brush.Viewbox = new Rect(0.5, 0.333, 0.25, 0.333); break; case (4): brush.Viewbox = new Rect(0.75, 0.333, 0.25, 0.333); break; case (5): brush.Viewbox = new Rect(0, 0.333, 0.25, 0.333); break; case (6): brush.Viewbox = new Rect(0.25, 0.666, 0.25, 0.333); break; case (7): brush.Viewbox = new Rect(0.5, 0.666, 0.25, 0.333); break; case (8): brush.Viewbox = new Rect(0.75, 0.666, 0.25, 0.333); break; } Label label = new Label(); label.Background = brush; label.HorizontalContentAlignment = HorizontalAlignment.Center; label.VerticalContentAlignment = VerticalAlignment.Center; Grid.SetColumnSpan(label, 1); Grid.SetColumn(label, cell.column); Grid.SetRowSpan(label, 1); Grid.SetRow(label, cell.row); theGrid.Children.Add(label); continue; } } else if (cell.flagged) { brush.Viewbox = new Rect(0.25, 0, 0.25, 0.333); } else { brush.Viewbox = new Rect(0, 0, 0.25, 0.333); } Grid.SetColumnSpan(button, 1); Grid.SetColumn(button, cell.column); Grid.SetRowSpan(button, 1); Grid.SetRow(button, cell.row); button.Background = brush; theGrid.Children.Add(button); button.MouseDown += new MouseButtonEventHandler(gridButtonRightClicked); button.Click += new RoutedEventHandler(gridButtonClicked); } } }
//event handler that manages when a MineButton is left clicked private void GameClick(object e, EventArgs args) { if (!fail) { MineButton gameButton = (MineButton)e; //change the button to clicked gameButton.State = Status.Clicked; gameButton.Click -= GameClick; gameButton.MouseUp -= GameRightClick; view.ColorButton(ref gameButton); //if a mine is clicked, set failure state if (gameButton.isMine) { smiley.Text = "X.X"; view.SetFail(ref board); fail = true; } //if clicked button is not adjacent to a mine, recurse the click in all valid directions else { if (gameButton.Numadjacent == 0) { if (gameButton.up != null) { if (gameButton.up.State != Status.Clicked) { GameClickedRecurse(gameButton.up); } } if (gameButton.down != null) { if (gameButton.down.State != Status.Clicked) { GameClickedRecurse(gameButton.down); } } if (gameButton.left != null) { if (gameButton.left.State != Status.Clicked) { GameClickedRecurse(gameButton.left); } } if (gameButton.right != null) { if (gameButton.right.State != Status.Clicked) { GameClickedRecurse(gameButton.right); } } if (gameButton.upleft != null) { if (gameButton.upleft.State != Status.Clicked) { GameClickedRecurse(gameButton.upleft); } } if (gameButton.upright != null) { if (gameButton.upright.State != Status.Clicked) { GameClickedRecurse(gameButton.upright); } } if (gameButton.downleft != null) { if (gameButton.downleft.State != Status.Clicked) { GameClickedRecurse(gameButton.downleft); } } if (gameButton.downright != null) { if (gameButton.downright.State != Status.Clicked) { GameClickedRecurse(gameButton.downright); } } } } } }
// Find revealed cell with X neighbors and only X unrevealed cells. (math to exclude flagged neighbors). Flag the required cells. protected bool FlagGuaranteedCells() { var RevealedButtons = board.RevealedButtons.Where(n => !SolvedButtons.Contains(n)); foreach (var button in RevealedButtons) { if (!SolvedButtons.Contains(button)) { Console.WriteLine($"x: {button.X} y: {button.Y}"); int mines = 0; int neighbors = 0; int flagged = 0; int revealed = 0; List <MineButton> UnrevealedNeighbors = new List <MineButton>(); foreach (MineButton.RelativePositions pos in Enum.GetValues(typeof(MineButton.RelativePositions))) { Console.WriteLine("2"); MineButton n = button.GetNeighbor(pos); if (n != null) { neighbors++; if (n.IsMine) { mines++; } if (n.IsFlagged) { flagged++; } else if (n.IsRevealed) { revealed++; } else { UnrevealedNeighbors.Add(n); } } } if (neighbors == flagged + revealed) { SolvedButtons.Add(button); } //if (neighbors - (flagged + revealed) ) if ((UnrevealedNeighbors.Count > 0) && neighbors - (revealed + flagged) == (mines - flagged)) { Console.WriteLine("4"); foreach (MineButton n in UnrevealedNeighbors) { Console.WriteLine("flagging"); Application.Current.Dispatcher.Invoke((Action) delegate { n.Flag(); }); } return(true); } else { Console.WriteLine("5"); } } } return(false); }
// Find revealed cell with the same number of flag neighbors as mine neighbors. Activate all unflagged neighbors. protected bool ClickSafeNeighbors() { var RevealedButtons = board.RevealedButtons.Where(n => !SolvedButtons.Contains(n)); foreach (var button in RevealedButtons) { if (!SolvedButtons.Contains(button)) { // If we haven't solved this one yet. Console.WriteLine($"x: {button.X} y: {button.Y}"); int mines = 0; int flagged = 0; int neighbors = 0; int revealed = 0; List <MineButton> UnrevealedNeighbors = new List <MineButton>(); foreach (MineButton.RelativePositions pos in Enum.GetValues(typeof(MineButton.RelativePositions))) { Console.WriteLine("2"); MineButton n = button.GetNeighbor(pos); if (n != null) { neighbors++; if (n.IsMine) { mines++; } if (n.IsFlagged) { flagged++; } else if (n.IsRevealed) { revealed++; } else { UnrevealedNeighbors.Add(n); } } } if (neighbors == flagged + revealed) { SolvedButtons.Add(button); } if ((UnrevealedNeighbors.Count > 0) && flagged == mines) { int i = 0; foreach (var neighbor in UnrevealedNeighbors) { if (!neighbor.IsFlagged && !neighbor.IsRevealed) { Console.WriteLine("Neighbor"); Application.Current.Dispatcher.Invoke((Action) delegate { neighbor.Activate(); }); i++; } } if (i > 0) { Console.WriteLine("True!!"); return(true); } } } } return(false); }
public void InitGrid() { GameRunning = true; // Reset Arrays Buttons.Clear(); MineButtons.Clear(); RevealedButtons.Clear(); FlaggedButtons.Clear(); for (int i = 0; i < SizeY; i++) { Buttons.Add(new List <MineButton>()); } if (PlayGrid != null) { if (PlayGrid.Children != null) { PlayGrid.Children.Clear(); } PlayGrid.ColumnDefinitions.Clear(); PlayGrid.RowDefinitions.Clear(); for (int x = 0; x < SizeX; x++) { PlayGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(CellSize) }); } for (int y = 0; y < SizeY; y++) { PlayGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(CellSize) }); } for (int x = 0; x < SizeX; x++) { for (int y = 0; y < SizeY; y++) { MineButton b = new MineButton(this, x, y, false); Grid.SetColumn(b, x); Grid.SetRow(b, y); PlayGrid.Children.Add(b); Buttons[y].Add(b); UnrevealedButtons.Add(b); } } Console.WriteLine(Buttons.Count); Console.WriteLine(Buttons[0].Count); for (int i = 0; i < NumMines; i++) { int x, y; GetRandomCell(out x, out y); Buttons[y][x].IsMine = true; MineButtons.Add(Buttons[y][x]); } if (winRef != null) { winRef.TB_FlagsLeft.Text = NumMines.ToString(); } } else { Console.WriteLine("NULL!"); } }
//handles the recursion of a MineButton with no adjacent mines being clicked private void GameClickedRecurse(MineButton gameButton) { //functionally identical to the GameClick method, minus event handling and checking for fail state gameButton.State = Status.Clicked; gameButton.Click -= GameClick; gameButton.MouseUp -= GameRightClick; view.ColorButton(ref gameButton); if (gameButton.Numadjacent == 0 && !gameButton.isMine) { if (gameButton.up != null) { if (gameButton.up.State != Status.Clicked) { GameClickedRecurse(gameButton.up); } } if (gameButton.down != null) { if (gameButton.down.State != Status.Clicked) { GameClickedRecurse(gameButton.down); } } if (gameButton.left != null) { if (gameButton.left.State != Status.Clicked) { GameClickedRecurse(gameButton.left); } } if (gameButton.right != null) { if (gameButton.right.State != Status.Clicked) { GameClickedRecurse(gameButton.right); } } if (gameButton.upleft != null) { if (gameButton.upleft.State != Status.Clicked) { GameClickedRecurse(gameButton.upleft); } } if (gameButton.upright != null) { if (gameButton.upright.State != Status.Clicked) { GameClickedRecurse(gameButton.upright); } } if (gameButton.downleft != null) { if (gameButton.downleft.State != Status.Clicked) { GameClickedRecurse(gameButton.downleft); } } if (gameButton.downright != null) { if (gameButton.downright.State != Status.Clicked) { GameClickedRecurse(gameButton.downright); } } } }
private void Flag(object obj, RoutedEventArgs args) { MineButton h = obj as MineButton; DisplayTile(h.x, h.y, -3); }
private void handleClick(object obj, RoutedEventArgs args) { MineButton h = obj as MineButton; ChangeField(h.x, h.y); }