Ejemplo n.º 1
0
 /// <summary>
 /// Method that is used to instantiate the buttons and start the game.
 /// </summary>
 private void StartGame()
 {
     for (int i = 0; i < row; i++)
     {
         for (int j = 0; j < row; j++)
         {
             // Adding buttons to a 2d array.
             LightButtons[i, j] = new LightsOutButton
             {
                 // Setting the size and color of the buttons.
                 BackColor = lightColor[colors.Next(2)],
                 Size      = new Size(40, 40)
             };
             LightButtons[i, j].Enabled = true;
             // Adding on to the counter to indicate how many lights are turned on.
             if (LightButtons[i, j].BackColor == Color.Blue)
             {
                 LightsOn++;
             }
             // setting the location for the buttons with spaces inbetween.
             LightButtons[i, j].Location = new Point(20 + (40 * i), 20 + (40 * j));
             LightButtons[i, j].xLoc     = i;
             LightButtons[i, j].yLoc     = j;
             LightButtons[i, j].TabIndex = i + j + 1;
             LightButtons[i, j].Click   += Check_Lights;
             flowLayoutPanel1.Controls.Add(LightButtons[i, j]);
         }
     }
     // Setting the text to show how many lights are on and refreshing it to be displayed.
     OnLights.Text = "Lights On: " + LightsOn;
     OnLights.Refresh();
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Method used to swap the colors of the lights, and check to see how many lights are left to turn off.
        /// </summary>
        private void Check_Lights(object sender, EventArgs e)
        {
            LightsOutButton buttonclicked = (LightsOutButton)sender;

            ChooseColor(buttonclicked);
            int ButtonXloc = (buttonclicked.xLoc);
            int ButtonYloc = (buttonclicked.yLoc);

            //Checks and changes each button around the clicked button. (left side, right side, on top and below)
            if (ButtonXloc > 0)
            {
                ChooseColor(LightButtons[ButtonXloc - 1, ButtonYloc]);
            }
            if (ButtonXloc < (row - 1))
            {
                ChooseColor(LightButtons[ButtonXloc + 1, ButtonYloc]);
            }
            if (ButtonYloc > 0)
            {
                ChooseColor(LightButtons[ButtonXloc, ButtonYloc - 1]);
            }
            if (ButtonYloc < (col - 1))
            {
                ChooseColor(LightButtons[ButtonXloc, ButtonYloc + 1]);
            }
            // Disables the buttons from being clicked when game is finished and displays all lights have been turned off.
            if (GameFinished())
            {
                for (int i = 0; i < row; i++)
                {
                    for (int j = 0; j < row; j++)
                    {
                        LightButtons[i, j].Enabled = false;
                    }
                }
                MessageBox.Show("All Light Have Been Turned Off");
                NewGame.Enabled = false;
                Restart.Enabled = true;
            }
            // checks all the colors to give a new amount for the LightsOn counter.
            LightsOn = 1;
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < row; j++)
                {
                    if (LightButtons[i, j].BackColor == Color.Blue)
                    {
                        LightsOn++;
                    }
                }
            }
            LightsOn--;
            OnLights.Text = "Lights On: " + LightsOn;
            OnLights.Refresh();
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Method used to change the lights color.
 /// </summary>
 /// <param name="button"></param>
 private void ChooseColor(LightsOutButton button)
 {
     if (button.BackColor == Color.Blue)
     {
         button.BackColor = Color.White;
     }
     else
     {
         button.BackColor = Color.Blue;
     }
 }