Example #1
0
        protected override void OnMouseDown(MouseEventArgs e)
        {
            MinesweeperForm.GetTimer().Enabled = true;
            if (MinesweeperForm.GetTimer().Enabled)
            {
                MinesweeperForm.GetTimer().Start();
            }

            if (e.Button == MouseButtons.Right)
            {
                if (field.GetCell(i, j).GetStatus() == Cell.Status.Flaged)
                {
                    field.GetCell(i, j).SetStatus(Cell.Status.Close);
                    BackgroundImage = Properties.Resources.closed;
                }
                else if (field.GetCell(i, j).GetStatus() == Cell.Status.Close)
                {
                    field.GetCell(i, j).SetStatus(Cell.Status.Flaged);
                    BackgroundImage = Properties.Resources.flaged;
                }
            }
            else
            {
                if (field.GetCell(i, j).GetStatus() == Cell.Status.Open)
                {
                    var countFlagedCells = 0;

                    for (var p = i - 1; p <= i + 1; p++)
                    {
                        for (var k = j - 1; k <= j + 1; k++)
                        {
                            if (p >= 0 && k >= 0 && p < field.GetColumnsCount() && k < field.GetRowsCount() && !(p == i && k == j) && field.GetCell(p, k).GetStatus() == Cell.Status.Flaged)
                            {
                                countFlagedCells++;
                            }
                        }
                    }

                    if ((int)field.GetCell(i, j).GetMean() - 1 == countFlagedCells)
                    {
                        for (var p = i - 1; p <= i + 1; p++)
                        {
                            for (var k = j - 1; k <= j + 1; k++)
                            {
                                if (p >= 0 && k >= 0 && p < field.GetColumnsCount() && k < field.GetRowsCount() && !(p == i && k == j) && field.GetCell(p, k).GetStatus() == Cell.Status.Close)
                                {
                                    field.OpenCell(p, k);
                                }
                            }
                        }

                        var m = 0;
                        var n = 0;

                        foreach (FieldButton b in table.Controls)
                        {
                            if (b.field.GetCell(m, n).GetStatus() == Cell.Status.Open)
                            {
                                b.BackgroundImage = dictionary[b.field.GetCell(m, n).GetMean()];
                            }

                            n++;

                            if (n >= field.GetRowsCount())
                            {
                                m++;
                                n = 0;
                            }
                        }
                    }
                }
                else if (field.GetCell(i, j).GetStatus() == Cell.Status.Close)
                {
                    field.OpenCell(i, j);

                    if (field.GetCell(i, j).GetStatus() == Cell.Status.Open)
                    {
                        if (field.GetCell(i, j).GetMean() == Cell.Mean.Zero)
                        {
                            var p = 0;
                            var k = 0;

                            foreach (FieldButton b in table.Controls)
                            {
                                if (b.field.GetCell(p, k).GetStatus() == Cell.Status.Open)
                                {
                                    b.BackgroundImage = dictionary[b.field.GetCell(p, k).GetMean()];
                                }

                                k++;

                                if (k >= field.GetRowsCount())
                                {
                                    p++;
                                    k = 0;
                                }
                            }
                        }
                        else
                        {
                            BackgroundImage = dictionary[field.GetCell(i, j).GetMean()];
                        }
                    }
                }

                if (field.GetStatusGame() == GameStatus.Lose)
                {
                    if (!field.GetFirstTurn())
                    {
                        field.RebuildField();
                        OnMouseDown(e);
                    }
                    else
                    {
                        if (field.GetCell(i, j).GetMean() == Cell.Mean.Bomb)
                        {
                            BackgroundImage = Properties.Resources.bombed;
                        }

                        foreach (FieldButton b in table.Controls)
                        {
                            b.Enabled = false;
                        }

                        MinesweeperForm.GetTimer().Stop();
                        MinesweeperForm.GetTimer().Enabled = false;
                        MessageBox.Show("Вы проиграли.", "Поражение");
                    }
                }
                else if (field.GetStatusGame() == GameStatus.Win)
                {
                    foreach (FieldButton b in table.Controls)
                    {
                        b.Enabled = false;
                    }

                    MinesweeperForm.GetTimer().Stop();
                    MinesweeperForm.GetTimer().Enabled = false;
                    MinesweeperForm.GetHighScores().Add(new HighScores(MinesweeperForm.GetStartTime(), MinesweeperForm.GetDate()));
                    MessageBox.Show("Вы выиграли!", "Победа");
                }

                if (!field.GetFirstTurn())
                {
                    field.SetFirstTurn(true);
                }
            }
        }
Example #2
0
 public OptionsForm(MinesweeperForm minesweeper)
 {
     InitializeComponent();
     this.minesweeper = minesweeper;
 }
Example #3
0
 public HighScoresForm(MinesweeperForm minesweeper)
 {
     InitializeComponent();
     this.minesweeper = minesweeper;
 }