Forum guidelines
AuthorQuestion
create array and change color button
Posted on: 28 Oct 2009
Hi,
You help me this game

using windows forms

This game is a single-player game. it consists of a form with 25 buttons, as shown in the following:
1   2   3   4   5
6   7   8   9  10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

the time elapsed(in seconds) will appear on the screen.

When the game starts, all the button are green in color, when a player clicks a button, the color of that button and its adjacent buttons in the same row and column changes. For example, if a button is green, its color changes to red; and if it is red, its color changes to green. To win the game, the player needs to click the buttons in such a manner that all the buttons turn red.

when a player clicks a button, the color of the following buttons will change:
the button that was clicked
the button above the clicked button
the button below the clicked button
the button to the left of the clicked button
the button to the right of the clicked button

for example, if a player clicks 7, the color of 2, 6, 7, 8 and 12 will change.

for a button in the first row, there is no button above it. therefore, if a button in the first row is clicked, the corresponding button in the last row will also change its color. for example, if a player clicks 2, the color of 1, 2, 3, 7, and 22 will change.

for a button in the last row, there is no button below it. therefore, if a button in the last row is clicked, the corresponding button in the first row will also change its color. for example, if a player clicks 22, the color of 2, 17, 21, 22, and 23 will change.

for a button in the first column, there is no button to its left, therefore, if a button in the first column is clicked, the corresponding button in the last column will also change its color. for example, if a player clicks 11, the color of 6, 11, 12, 15 and 16 will change.

for a button in the last column, there is no button to its right. therefore, if a button in the last column is clicked, the corresponding button in the first column will also change its color. for example, if player clicks 15, the color of 10, 11, 14, 15 and 20 will change.

for button at the corners, two of the adjacent buttons are missing. for example, 1 is located in the first row and in the first column. there is no button above it and to its the left. therefore, if a player clicks 1, the color of 1, 2, 5, 6 and 21 will change.

the player will interact with the game by using a mouse or a keyboard.

there should be an option to check the top score by clicking a button. clicking this button will display the scoreboard in the following format:
name:##########
time:##########(Secs)

if the player is able to finish the game in a record time, he/she will be asked to enter his/her name. the name and the new time will be stored in a file and displayed whenever the game starts.

AuthorReply
Re: create array and change color button
Posted on: 28 Oct 2009  

This is text of main form:

(I added only TabbedLayot panel with 25 cells (5 columns auto size * 5 rows auto size)

 

namespace ChangeColorGame

{

      public partial class FormMain : Form

      {

             public FormMain()

             {

                InitializeComponent();

                FillPanel();

                LinkNeighbours();

             }

 

             private void FillPanel()

             {

                for (int iii = 1; iii < 26; iii++)

                {

                                var nextButton = new ucOneCell();

                                nextButton.Name = iii.ToString();

                                nextButton.CellText = iii.ToString();

                                nextButton.ChangeColor(Color.Blue);

                                tableLayoutPanel1.Controls.Add(nextButton);

                }

             }

             private void LinkNeighbours()

             {

                                //                            1   2   3   4   5

                                //                            6   7   8   9  10

                                //                            11 12 13 14 15

                                //                            16 17 18 19 20

                                //                            21 22 23 24 25

                AddNeighboursTo(1, 2, 6, 21, 5);

                AddNeighboursTo(2, 1, 3, 7, 22);

                AddNeighboursTo(3, 2, 4, 8, 23);

                AddNeighboursTo(4, 3, 5, 9, 24);

                AddNeighboursTo(5, 4, 1, 10, 25);

 

                AddNeighboursTo(6, 10, 7, 1, 11);

                AddNeighboursTo(7, 6, 8, 2, 12);

                AddNeighboursTo(8, 7, 9, 3, 13);

                AddNeighboursTo(9, 8, 10, 4, 14);

                AddNeighboursTo(10, 9, 6, 15, 5);

 

                AddNeighboursTo(11, 6, 16, 12, 15);

                AddNeighboursTo(12, 11, 13, 7, 17);

                AddNeighboursTo(13, 12, 14, 8, 18);

                AddNeighboursTo(14, 13, 15, 9, 19);

                AddNeighboursTo(15, 14, 11, 10, 20);

 

                AddNeighboursTo(16, 20, 17, 11, 21);

                AddNeighboursTo(17, 16, 18, 12, 22);

                AddNeighboursTo(18, 17, 19, 13, 23);

                AddNeighboursTo(19, 18, 20, 14, 24);

                AddNeighboursTo(20, 19, 16, 15, 25);

 

                AddNeighboursTo(21,25, 22, 16, 1);

                AddNeighboursTo(22, 21, 23, 17, 2);

                AddNeighboursTo(23, 22, 24, 18, 3);

                AddNeighboursTo(24, 23, 25, 19,4);

                AddNeighboursTo(25, 24, 21, 20, 5);

 

 

 

             }

 

             private void AddNeighboursTo(int centralButton, params int [] anyButtons)

             {

                ucOneCell center = tableLayoutPanel1.Controls.Find(centralButton.ToString(), false).FirstOrDefault() as ucOneCell;

                if (center != null)

                {

                                center.Neighbours = new List<ucOneCell>();

                                foreach (int nBour in anyButtons)

                                {

                                                ucOneCell existingNbour = tableLayoutPanel1.Controls.Find(nBour.ToString(), false).FirstOrDefault() as ucOneCell;

                                                if (existingNbour != null)

                                                {

                                                                center.Neighbours.Add(existingNbour);

                                                }

                                }

                }

             }

      }

}


This is code of user control, that contain only buttonCell (Button with dock = fill)

 

namespace ChangeColorGame

{

      public partial class ucOneCell : UserControl

      {

             public ucOneCell()

             {

                InitializeComponent();

                this.buttonCell.MouseClick += new MouseEventHandler(buttonCell_MouseClick);

             }

 

             public void ChangeColor(Color toUse)

             {

                buttonCell.BackColor = toUse;

             }

 

 

             public void ChangeColor()

             {

                if (buttonCell.BackColor == Color.Blue)

                {

                                buttonCell.BackColor = Color.Red;

                                return;

                }

                if (buttonCell.BackColor == Color.Red)

                {

                                buttonCell.BackColor = Color.Blue;

                }

             }

 

             void buttonCell_MouseClick(object sender, MouseEventArgs e)

             {

                foreach (var nBour in Neighbours)

                {

                                nBour.ChangeColor();

                }

                this.ChangeColor();

             }

             public List<ucOneCell> Neighbours

             {

                get;

                set;

             }

 

             public string CellText

             {

                get

                {

                                return buttonCell.Text;

                }

                set

                {

                                buttonCell.Text = value;

                }

             }

      }

}

 

 

I hope to add timer and score counting you can do yourself.

:-)


Employers - Post Free Jobs

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.