Пример #1
0
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent(int nElements, int mElements, int bElements)
        {
            // Creating multidimentional button array
            btnArray = new btn[nElements, mElements];

            this.SuspendLayout();
            //
            // Form2
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode       = System.Windows.Forms.AutoScaleMode.Font;

            // Client Size = number of n x m elements * 25 (where 25 is w, h dimentions in px)
            this.ClientSize = new System.Drawing.Size(nElements * BTN_SIZE, mElements * BTN_SIZE);
            this.Name       = "Form2";
            this.Text       = "Form2";
            this.Load      += new System.EventHandler(this.Form2_Load);
            this.ResumeLayout(false);

            // Creating grid of buttons that the user can interact with
            for (int i = 0; i < nElements; i++)
            {
                for (int j = 0; j < mElements; j++)
                {
                    // Creating new Button for user to click
                    Button newBtn = new Button();

                    newBtn.Size     = new System.Drawing.Size(BTN_SIZE, BTN_SIZE);
                    newBtn.Location = new System.Drawing.Point(i * BTN_SIZE, j * BTN_SIZE);
                    newBtn.Text     = null;
                    newBtn.TabIndex = i + j * nElements;
                    this.Controls.Add(newBtn);

                    // Creating new btn struct to hold button information
                    btn newBtnContainer = new btn(newBtn, null, null, newBtn.TabIndex, i, j);

                    // Event Handler using an Anonymouse Delegate to pass extra data to a click function
                    newBtn.Click += new System.EventHandler((object sender, EventArgs e) => btnClick(sender, e, newBtnContainer));

                    btnArray[i, j] = newBtnContainer;
                }
            }

            // Generating board and assigning text once all buttons are placed
            generateBoard(nElements, mElements, bElements);
        } // END - InitializeComponent
Пример #2
0
        }// End - updateNumbers

        // After btn clicked, the program will preform steps to address the display and update of the clicked on square
        public void updateBtnText(btn clickedBtn)
        {
            // If the clicked button is not '*' and not null, reveal it and do nothing else
            if (btnArray[clickedBtn.btnN, clickedBtn.btnM].btnText != "*" && btnArray[clickedBtn.btnN, clickedBtn.btnM].btnText != null)
            {
                btnArray[clickedBtn.btnN, clickedBtn.btnM].heldBtn.Text = btnArray[clickedBtn.btnN, clickedBtn.btnM].btnText;
                clickedBtn.heldBtn.BackColor = Color.Cornsilk;
            }
            // If clicked button is '*', then BOOM and game over
            else if (btnArray[clickedBtn.btnN, clickedBtn.btnM].btnText == "*")
            {
                revealBoard();

                MessageBox.Show("BOOM!");

                this.Close();
            }
            // if the others fail, that means the button clicked is a blank, time to reveal!
            else
            {
                // New stack that will hold all the buttons to be checked
                Queue <btn> heldBtns = new Queue <btn>();

                // Adding first button to be checked
                heldBtns.Enqueue(clickedBtn);

                // Chaning btn collor to blue
                btnArray[clickedBtn.btnN, clickedBtn.btnM].heldBtn.BackColor = Color.AliceBlue;

                // While there are still buttons to be checked, keep looping
                while (heldBtns.Count != 0)
                {
                    btn activeBtn = heldBtns.Peek();

                    //MessageBox.Show(activeBtn.btnN + " " + activeBtn.btnM);

                    int nIndex = activeBtn.btnN;
                    int mIndex = activeBtn.btnM;

                    // Holding the indecies to be checked in the 8 spaces around the b
                    int checkIndexN = nIndex;
                    int checkIndexM = mIndex;

                    // N  check (-n, m)
                    checkIndexN = nIndex - 1;
                    checkIndexM = mIndex;
                    // Going up; checking if n is in bounds
                    if (checkIndexN >= 0)
                    {
                        // If text is null, then
                        if (btnArray[checkIndexN, checkIndexM].btnText == null)
                        {
                            // If button is not already in queue AND not already uncovered
                            if (heldBtns.Contains(btnArray[checkIndexN, checkIndexM]) == false && btnArray[checkIndexN, checkIndexM].uncovered == false)
                            {
                                heldBtns.Enqueue(btnArray[checkIndexN, checkIndexM]);

                                // Once added to queue, mark blue
                                btnArray[checkIndexN, checkIndexM].heldBtn.BackColor = Color.AliceBlue;
                            }
                        }
                        // If text is NOT '*', it is a number. Reveal and mark cornsilk
                        else if (btnArray[checkIndexN, checkIndexM].btnText != "*")
                        {
                            btnArray[checkIndexN, checkIndexM].heldBtn.Text      = btnArray[checkIndexN, checkIndexM].btnText;
                            btnArray[checkIndexN, checkIndexM].heldBtn.BackColor = Color.Cornsilk;
                        }
                    }
                    // NE check (-n,+m)
                    checkIndexN = nIndex - 1;
                    checkIndexM = mIndex + 1;
                    // Going up, going right; checking if n, m is in bounds
                    if (checkIndexN >= 0 && checkIndexM < mHeight)
                    {
                        // If text is null, add to queue if not already added
                        if (btnArray[checkIndexN, checkIndexM].btnText == null)
                        {
                            // If button is not already in queue AND not already uncovered
                            if (heldBtns.Contains(btnArray[checkIndexN, checkIndexM]) == false && btnArray[checkIndexN, checkIndexM].uncovered == false)
                            {
                                heldBtns.Enqueue(btnArray[checkIndexN, checkIndexM]);

                                // Once added to queue, mark blue
                                btnArray[checkIndexN, checkIndexM].heldBtn.BackColor = Color.AliceBlue;
                            }
                        }
                        // If text is NOT '*', it is a number. Reveal and mark cornsilk
                        else if (btnArray[checkIndexN, checkIndexM].btnText != "*")
                        {
                            btnArray[checkIndexN, checkIndexM].heldBtn.Text      = btnArray[checkIndexN, checkIndexM].btnText;
                            btnArray[checkIndexN, checkIndexM].heldBtn.BackColor = Color.Cornsilk;
                        }
                    }
                    // E  check (n, +m)
                    checkIndexN = nIndex;
                    checkIndexM = mIndex + 1;
                    // Going right; checking if m is in bounds
                    if (checkIndexM < mHeight)
                    {
                        // If text is null, add to queue if not already added
                        if (btnArray[checkIndexN, checkIndexM].btnText == null)
                        {
                            // If button is not already in queue AND not already uncovered
                            if (heldBtns.Contains(btnArray[checkIndexN, checkIndexM]) == false && btnArray[checkIndexN, checkIndexM].uncovered == false)
                            {
                                heldBtns.Enqueue(btnArray[checkIndexN, checkIndexM]);

                                // Once added to queue, mark blue
                                btnArray[checkIndexN, checkIndexM].heldBtn.BackColor = Color.AliceBlue;
                            }
                        }
                        // If text is NOT '*', it is a number. Reveal and mark cornsilk
                        else if (btnArray[checkIndexN, checkIndexM].btnText != "*")
                        {
                            btnArray[checkIndexN, checkIndexM].heldBtn.Text      = btnArray[checkIndexN, checkIndexM].btnText;
                            btnArray[checkIndexN, checkIndexM].heldBtn.BackColor = Color.Cornsilk;
                        }
                    }
                    // SW check (+n,+m)
                    checkIndexN = nIndex + 1;
                    checkIndexM = mIndex + 1;
                    // Going down, going right; checking if n, m is in bounds
                    if (checkIndexN < nWidth && checkIndexM < mHeight)
                    {
                        // If text is null, add to queue if not already added
                        if (btnArray[checkIndexN, checkIndexM].btnText == null)
                        {
                            // If button is not already in queue AND not already uncovered
                            if (heldBtns.Contains(btnArray[checkIndexN, checkIndexM]) == false && btnArray[checkIndexN, checkIndexM].uncovered == false)
                            {
                                heldBtns.Enqueue(btnArray[checkIndexN, checkIndexM]);

                                // Once added to queue, mark blue
                                btnArray[checkIndexN, checkIndexM].heldBtn.BackColor = Color.AliceBlue;
                            }
                        }
                        // If text is NOT '*', it is a number. Reveal and mark cornsilk
                        else if (btnArray[checkIndexN, checkIndexM].btnText != "*")
                        {
                            btnArray[checkIndexN, checkIndexM].heldBtn.Text      = btnArray[checkIndexN, checkIndexM].btnText;
                            btnArray[checkIndexN, checkIndexM].heldBtn.BackColor = Color.Cornsilk;
                        }
                    }
                    // S  check (+n, m)
                    checkIndexN = nIndex + 1;
                    checkIndexM = mIndex;
                    // Going down; checking if n is in bounds
                    if (checkIndexN < nWidth)
                    {
                        // If text is null, add to queue if not already added
                        if (btnArray[checkIndexN, checkIndexM].btnText == null)
                        {
                            // If button is not already in queue AND not already uncovered
                            if (heldBtns.Contains(btnArray[checkIndexN, checkIndexM]) == false && btnArray[checkIndexN, checkIndexM].uncovered == false)
                            {
                                heldBtns.Enqueue(btnArray[checkIndexN, checkIndexM]);

                                // Once added to queue, mark blue
                                btnArray[checkIndexN, checkIndexM].heldBtn.BackColor = Color.AliceBlue;
                            }
                        }
                        // If text is NOT '*', it is a number. Reveal and mark cornsilk
                        else if (btnArray[checkIndexN, checkIndexM].btnText != "*")
                        {
                            btnArray[checkIndexN, checkIndexM].heldBtn.Text      = btnArray[checkIndexN, checkIndexM].btnText;
                            btnArray[checkIndexN, checkIndexM].heldBtn.BackColor = Color.Cornsilk;
                        }
                    }
                    // SW check (+n,-m)
                    checkIndexN = nIndex + 1;
                    checkIndexM = mIndex - 1;
                    // Going down, going left; checking if n, m is in bounds
                    if (checkIndexN < nWidth && checkIndexM >= 0)
                    {
                        // If text is null, add to queue if not already added
                        if (btnArray[checkIndexN, checkIndexM].btnText == null)
                        {
                            // If button is not already in queue AND not already uncovered
                            if (heldBtns.Contains(btnArray[checkIndexN, checkIndexM]) == false && btnArray[checkIndexN, checkIndexM].uncovered == false)
                            {
                                heldBtns.Enqueue(btnArray[checkIndexN, checkIndexM]);

                                // Once added to queue, mark blue
                                btnArray[checkIndexN, checkIndexM].heldBtn.BackColor = Color.AliceBlue;
                            }
                        }
                        // If text is NOT '*', it is a number. Reveal and mark cornsilk
                        else if (btnArray[checkIndexN, checkIndexM].btnText != "*")
                        {
                            btnArray[checkIndexN, checkIndexM].heldBtn.Text      = btnArray[checkIndexN, checkIndexM].btnText;
                            btnArray[checkIndexN, checkIndexM].heldBtn.BackColor = Color.Cornsilk;
                        }
                    }
                    // W  check (n, -m)
                    checkIndexN = nIndex;
                    checkIndexM = mIndex - 1;
                    // Going left; checking if m is in bounds
                    if (checkIndexM >= 0)
                    {
                        // If text is null, add to queue if not already added
                        if (btnArray[checkIndexN, checkIndexM].btnText == null)
                        {
                            // If button is not already in queue AND not already uncovered
                            if (heldBtns.Contains(btnArray[checkIndexN, checkIndexM]) == false && btnArray[checkIndexN, checkIndexM].uncovered == false)
                            {
                                heldBtns.Enqueue(btnArray[checkIndexN, checkIndexM]);

                                // Once added to queue, mark blue
                                btnArray[checkIndexN, checkIndexM].heldBtn.BackColor = Color.AliceBlue;
                            }
                        }
                        // If text is NOT '*', it is a number. Reveal and mark cornsilk
                        else if (btnArray[checkIndexN, checkIndexM].btnText != "*")
                        {
                            btnArray[checkIndexN, checkIndexM].heldBtn.Text      = btnArray[checkIndexN, checkIndexM].btnText;
                            btnArray[checkIndexN, checkIndexM].heldBtn.BackColor = Color.Cornsilk;
                        }
                    }
                    // NW check (-n,-m)
                    checkIndexN = nIndex - 1;
                    checkIndexM = mIndex - 1;
                    // Going left; checking if m is in bounds
                    if (checkIndexN >= 0 && checkIndexM >= 0)
                    {
                        // If text is null, add to queue if not already added
                        if (btnArray[checkIndexN, checkIndexM].btnText == null)
                        {
                            // If button is not already in queue AND not already uncovered
                            if (heldBtns.Contains(btnArray[checkIndexN, checkIndexM]) == false && btnArray[checkIndexN, checkIndexM].uncovered == false)
                            {
                                heldBtns.Enqueue(btnArray[checkIndexN, checkIndexM]);

                                // Once added to queue, mark blue
                                btnArray[checkIndexN, checkIndexM].heldBtn.BackColor = Color.AliceBlue;
                            }
                        }
                        // If text is NOT '*', it is a number. Reveal and mark cornsilk
                        else if (btnArray[checkIndexN, checkIndexM].btnText != "*")
                        {
                            btnArray[checkIndexN, checkIndexM].heldBtn.Text      = btnArray[checkIndexN, checkIndexM].btnText;
                            btnArray[checkIndexN, checkIndexM].heldBtn.BackColor = Color.Cornsilk;
                        }
                    }

                    // Once that's all done, marking button as uncovered
                    btnArray[nIndex, mIndex].uncovered = true;

                    // Remove the button currently being checked
                    heldBtns.Dequeue();
                } // End - while
            }     // End - else
        }         // End - updateBtnText
Пример #3
0
        } // END - InitializeComponent

        /* A delegate function that will handle button click event.
         * The following checks will be made:
         *     1) Is button null?
         *            If so, see function.
         *     2) Is button '*'?
         *            If so, player loses. Game over, exit game.
         *     3) Is button a numbered element?
         *            If so, uncover number - nothing more.
         */
        private void btnClick(object sender, EventArgs e, btn clickedBtn)
        {
            // On click, calling function for updating button text display
            updateBtnText(clickedBtn);
        }