Example #1
0
        private void RevealMineCell(MineCell revealingMineCell)
        {
            string[] parsedCoords = revealingMineCell.Name.ToString().Split(',');
            int      proxXCoord   = Convert.ToInt32(parsedCoords[0]);
            int      proxYCoord   = Convert.ToInt32(parsedCoords[1]);
            string   proxCellName; //the name of the proximity mine cell
            MineCell proxCell;     //the proximity mine cell

            int[,] proxCoords =
            {
                { proxXCoord + 1, proxYCoord - 1 },//all 8 adjacent coordinates of `revealingMineCell`
                { proxXCoord + 1, proxYCoord     },
                { proxXCoord + 1, proxYCoord + 1 },
                { proxXCoord - 1, proxYCoord - 1 },
                { proxXCoord - 1, proxYCoord     },
                { proxXCoord - 1, proxYCoord + 1 },
                { proxXCoord,     proxYCoord - 1 },
                { proxXCoord,     proxYCoord + 1 }
            };
            if (revealingMineCell.HasMine)//if the mine cell revealed has mine in it
            {
                MineField.Enabled    = false;
                GameOverSign.Visible = true;
                RetryButton.Enabled  = true;
                RetryButton.Visible  = true;
                RetryButton.BringToFront();
            }
            else if (revealingMineCell.ProxMineCount == 0)//if the mine cell revealed is empty
            {
                revealingMineCell.Enabled    = false;
                revealingMineCell.BackColor  = SystemColors.ControlDark;
                revealingMineCell.IsRevealed = true;
                for (int i = 0; i < 8; i++)
                {
                    proxCellName = Convert.ToString(proxCoords[i, 0]) + "," + Convert.ToString(proxCoords[i, 1]);
                    proxCell     = (MineCell)MineField.Controls.Find(proxCellName, true).FirstOrDefault();
                    if (proxCell != null && proxCell.IsRevealed == false && proxCell.IsFlagged)
                    {
                        proxCell.IsFlagged = false;
                        proxCell.BackColor = SystemColors.ControlDark;
                        RevealMineCell(proxCell);
                    }
                    else if (proxCell != null && proxCell.IsRevealed == false) //reminder: `proxCell` is impossible to have a mine in it as `revealingMineCell` is empty in the first place
                    {
                        RevealMineCell(proxCell);                              //repeat the process until all adjacent empty mine cells are all revealed
                    }
                }
            }
            else
            {
                revealingMineCell.Enabled    = false;
                revealingMineCell.Text       = Convert.ToString(revealingMineCell.ProxMineCount);
                revealingMineCell.BackColor  = SystemColors.ControlDark;
                revealingMineCell.IsRevealed = true;
            }
        }
Example #2
0
        private void CheckIfWon(bool byFlaging)
        {
            int totalMineFound    = 0;
            int totalRevealedCell = 0;
            int flaggedCount      = 0;
            int flaggedMineCount  = 0;

            foreach (MineCell mineCell in MineField.Controls)
            {
                if (mineCell.HasMine && mineCell.IsRevealed == false)
                {
                    totalMineFound++;
                }
                if (mineCell.IsRevealed)
                {
                    totalRevealedCell++;
                }
                if (byFlaging == true)
                {
                    if (mineCell.IsFlagged && mineCell.HasMine)
                    {
                        flaggedMineCount++;
                    }
                    if (mineCell.IsFlagged)
                    {
                        flaggedCount++;
                    }
                }
            }
            if (totalMineFound == GameInfo.TotalMineCount && totalRevealedCell == (GameInfo.BoardX * GameInfo.BoardY) - GameInfo.TotalMineCount)//check if all non-mine cells have been revealed
            {
                MineField.Enabled   = false;
                WinSign.Visible     = true;
                RetryButton.Enabled = true;
                RetryButton.Visible = true;
                RetryButton.BringToFront();
            }
            else if (flaggedCount == flaggedMineCount && flaggedCount == GameInfo.TotalMineCount && byFlaging)//if all mines have been flagged and no excess flag was used
            {
                MineField.Enabled   = false;
                WinSign.Visible     = true;
                RetryButton.Enabled = true;
                RetryButton.Visible = true;
                RetryButton.BringToFront();
            }
        }