예제 #1
0
        private void ClickZeroButtons(XYButton b)
        {
            int x, y;

            for (int i = -1; i < 2; i++)
            {
                for (int j = -1; j < 2; j++)
                {
                    x = b.PosX + i;
                    y = b.PosY + j;
                    if (x >= 0 && y >= 0 && x < matrixDimensions.X && y < matrixDimensions.Y && !(buttonMatrix[x, y].IsClicked))
                    {
                        int  adjacentMines = FindAdjacent(buttonMatrix[x, y]);
                        bool ZeroAdjacent  = adjacentMines == 0 ? true : false;

                        buttonMatrix[x, y].IsClicked = true;
                        buttonMatrix[x, y].BackColor = Color.LightGoldenrodYellow;
                        buttonMatrix[x, y].Text      = ZeroAdjacent ? "" : adjacentMines.ToString();
                        if (ZeroAdjacent)
                        {
                            ClickZeroButtons(buttonMatrix[x, y]);
                            Console.WriteLine("Should be called recursively!");
                        }
                    }
                }
            }
        }
예제 #2
0
        public void InitializeButtonMatrix()
        {
            // Initialize main elements
            this.groupBox1    = new GroupBox();
            this.buttonMatrix = new XYButton[matrixDimensions.X, matrixDimensions.Y];


            // Groupbox dimensions / properties
            this.groupBox1.Location = new Point(30, 30);
            this.groupBox1.Size     = new Size(windowSize.X - 50, windowSize.Y - 50);
            this.groupBox1.Text     = "Minefield - " + NumberOfMines.ToString() + " Mines left";
            this.groupBox1.Font     = new Font(groupBox1.Font, FontStyle.Bold);


            // Populate buttonMatrix
            for (int i = 0; i < matrixDimensions.X; i++)
            {
                for (int j = 0; j < matrixDimensions.Y; j++)
                {
                    // Create button
                    buttonMatrix[i, j]          = new XYButton(i, j);
                    buttonMatrix[i, j].Size     = new Size(50, 50);
                    buttonMatrix[i, j].Location = new Point(50 * i + 15, 50 * j + 18);
                    //buttonMatrix[i, j].Text = (i + 1).ToString() + ", " + (j + 1).ToString();
                    buttonMatrix[i, j].Font = new Font(groupBox1.Font, FontStyle.Regular);

                    // trigger buttonClicked event
                    buttonMatrix[i, j].MouseDown += new MouseEventHandler(buttonClicked);



                    // add to group box
                    this.groupBox1.Controls.Add(this.buttonMatrix[i, j]);
                }
            }

            // Window size
            this.ClientSize = new System.Drawing.Size(windowSize.X, windowSize.Y);
            // Put Group box in the client window
            this.Controls.Add(this.groupBox1);
        }
예제 #3
0
        private int FindAdjacent(XYButton b)
        {
            int counter = 0;
            int x, y;

            for (int i = -1; i < 2; i++)
            {
                for (int j = -1; j < 2; j++)
                {
                    x = b.PosX + i;
                    y = b.PosY + j;
                    if (x >= 0 && y >= 0 && x < matrixDimensions.X && y < matrixDimensions.Y)
                    {
                        if (buttonMatrix[x, y].IsMine)
                        {
                            counter++;
                        }
                    }
                }
            }

            return(counter);
        }
예제 #4
0
        // Handle mouse clicks on a button
        private void buttonClicked(object sender, MouseEventArgs e)
        {
            XYButton b = sender as XYButton;

            switch (e.Button)
            {
            /*----- Left Click ----- */
            case MouseButtons.Left:
                if (!b.IsFlagged)
                {
                    b.IsClicked = true;
                }
                else
                {
                    break;
                }

                if (b.IsMine)
                {
                    GameOver();
                }

                if (!b.IsMine)
                {
                    int adjacentMines = FindAdjacent(b);
                    b.Text = adjacentMines > 0 ? adjacentMines.ToString() : "";
                    if (FindAdjacent(b) == 0)
                    {
                        ClickZeroButtons(b);
                    }
                    b.BackColor = Color.LightGoldenrodYellow;
                }
                Console.WriteLine("(" + b.Text + ") buttonClicked() | i = " + b.PosX.ToString() + " j = " + b.PosY.ToString());
                break;

            /*----- Right Click ----- */
            case MouseButtons.Right:
                if (!b.IsClicked)
                {
                    b.IsFlagged = b.IsFlagged ? false : true;
                }
                else
                {
                    break;
                }

                if (b.IsFlagged && b.IsMine)
                {
                    FlaggedMines++;
                    b.BackColor = Color.LightGreen;
                }

                if (b.IsFlagged && !b.IsMine)
                {
                    FalsePositives++;
                    b.BackColor = Color.LightGreen;
                    //b.BackColor = Color.DarkGoldenrod; // used for debugging
                }

                if (!b.IsFlagged && b.IsMine)
                {
                    FlaggedMines--;
                    b.BackColor = Color.Transparent;
                }

                if (!b.IsFlagged && !b.IsMine)
                {
                    FalsePositives--;
                    Console.WriteLine("FalsePositives--");
                    b.BackColor = Color.Transparent;
                }

                if (FlaggedMines == NumberOfMines && FalsePositives == 0)
                {
                    GameWin();
                }

                Console.WriteLine("(" + b.Text + ") fieldFlagged() | i = " + b.PosX.ToString() + " j = " + b.PosY.ToString());
                break;

            default:
                throw new Exception("Mouse button not recognized");
            }
            this.groupBox1.Text = "Minefield - " + (NumberOfMines - (FlaggedMines + FalsePositives)).ToString() + " Mines left";
        }