private void BuildBoard(Boolean rebuild = false) { Done = false; Cheat = false; Seconds = 0; MarkedMines = 0; DoLabels(); timer1.Enabled = false; labelMessage.Text = String.Empty; this.buttonNewGame.BackgroundImage = global::minesweeper.Properties.Resources.smiley1; if (rebuild) { this.SuspendLayout(); this.Hide(); //Loop Through the rows and columns MineField = new MineCell[CurrentLevel.Rows, CurrentLevel.Colonnes]; pnlMine.Visible = false; pnlMine.Controls.Clear(); for (int Row = 0; Row < CurrentLevel.Rows; Row++) { for (int Col = 0; Col < CurrentLevel.Colonnes; Col++) { MineCell C = new MineCell { Left = (CellSize * Col) + 3, Top = (CellSize * Row) + 3, Width = CellSize, Height = CellSize, HasMine = false, Number = 0, X = Col, Y = Row }; MineField[Row, Col] = C; C.MouseClick += mine_Click; C.MouseDoubleClick += mine_MouseDoubleClick; C.MouseUp += mine_MouseUp; C.MouseDown += mine_MouseDown; pnlMine.Controls.Add(C); } } pnlMine.Visible = true; this.Show(); this.ResumeLayout(); } else { foreach (MineCell M in MineField) { M.Reset(); } } }
private void mine_MouseUp(object sender, MouseEventArgs e) { if (Done) { return; } this.buttonNewGame.BackgroundImage = global::minesweeper.Properties.Resources.smiley1; MineCell M = (MineCell)sender; if (M.View == MineCell.MineCellView.Pressed) { M.View = MineCell.MineCellView.Button; } }
private void mine_MouseDown(object sender, MouseEventArgs e) { if (Done) { return; } MineCell M = (MineCell)sender; if (e.Button == MouseButtons.Left && M.View == MineCell.MineCellView.Button) { this.buttonNewGame.BackgroundImage = global::minesweeper.Properties.Resources.smiley2; M.View = MineCell.MineCellView.Pressed; } }
private void mine_MouseDoubleClick(object sender, MouseEventArgs e) { if (Done) { return; } MineCell M = (MineCell)sender; if (e.Button == MouseButtons.Left && M.View == MineCell.MineCellView.Number) { int FC = 0; for (int R = M.Y - 1; R <= M.Y + 1; R++) { for (int C = M.X - 1; C <= M.X + 1; C++) { if (R >= 0 && R < CurrentLevel.Rows && C >= 0 && C < CurrentLevel.Colonnes) { if (MineField[R, C].View == MineCell.MineCellView.Flag) { FC++; } } } } if (FC != M.Number) { return; } for (int R = M.Y - 1; R <= M.Y + 1; R++) { for (int C = M.X - 1; C <= M.X + 1; C++) { if (R >= 0 && R < CurrentLevel.Rows && C >= 0 && C < CurrentLevel.Colonnes) { if (MineField[R, C].View == MineCell.MineCellView.Button || MineField[R, C].View == MineCell.MineCellView.Question) { mine_Click(MineField[R, C], new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0)); } } } } } }
private void GenerateMineField(MineCell excludedCell) { //Generate Random Mine Locations //first click is allways a safe location Random RX = new Random(); for (int i = 1; i <= CurrentLevel.Mines; i++) { int X; int Y; do { X = RX.Next(0, CurrentLevel.Colonnes); Y = RX.Next(0, CurrentLevel.Rows); } while (MineField[Y, X].HasMine || MineField[Y, X].Equals(excludedCell)); MineField[Y, X].HasMine = true; } //Count the mines for (int Row = 0; Row < CurrentLevel.Rows; Row++) { for (int Col = 0; Col < CurrentLevel.Colonnes; Col++) { if (!MineField[Row, Col].HasMine) { for (int R = Row - 1; R <= Row + 1; R++) { for (int C = Col - 1; C <= Col + 1; C++) { if (R >= 0 && R < CurrentLevel.Rows && C >= 0 && C < CurrentLevel.Colonnes && !(Row == R && Col == C)) { if (MineField[R, C].HasMine) { MineField[Row, Col].Number++; } } } } } } } }
private void ShowBlank(MineCell M) { M.View = MineCell.MineCellView.Number; for (int R = M.Y - 1; R <= M.Y + 1; R++) { for (int C = M.X - 1; C <= M.X + 1; C++) { if (R >= 0 && R < CurrentLevel.Rows && C >= 0 && C < CurrentLevel.Colonnes) { if (MineField[R, C].View == MineCell.MineCellView.Button) { if (MineField[R, C].Number == 0) { ShowBlank(MineField[R, C]); } else { MineField[R, C].View = MineCell.MineCellView.Number; } } } } } }
private void mine_Click(object sender, MouseEventArgs e) { if (Done) { return; } MineCell M = (MineCell)sender; //generate minefield on first click if (!timer1.Enabled) { GenerateMineField(M); timer1.Enabled = true; labelMessage.Text = "Game in Progress"; } if (e.Button == MouseButtons.Left && (M.View == MineCell.MineCellView.Pressed || M.View == MineCell.MineCellView.Button || M.View == MineCell.MineCellView.Question)) { if (M.HasMine) { M.HasExplosed = true; EndGame(false); } else if (M.Number > 0) { M.View = MineCell.MineCellView.Number; if (GameOver()) { EndGame(true); } } else if (M.Number == 0) { ShowBlank(M); if (GameOver()) { EndGame(true); } } } else if (e.Button == MouseButtons.Right && M.View != MineCell.MineCellView.Number) { switch (M.View) { case MineCell.MineCellView.Button: M.View = MineCell.MineCellView.Flag; MarkedMines++; DoLabels(); break; case MineCell.MineCellView.Flag: if (marksToolStripMenuItem.Checked) { M.View = MineCell.MineCellView.Question; } else { M.View = MineCell.MineCellView.Button; } MarkedMines--; DoLabels(); break; default: M.View = MineCell.MineCellView.Button; break; } } }