1

i have an application of winforms with the logic of REMEMBER me game. what i want is that when ever i click reset button or run the game the locations of my buttons will be different(rumbled) so that the game will be interesting :)

enter image description here

this are my codes so far

public partial class Form1 : Form
{
    int b1 = 1, b2 = 2, b3 = 3,b4 = 4,b5 = 5,b6 = 6;
    public Form1()
    {
        InitializeComponent();
    }
    private void Wrong_Tick(object sender, EventArgs e)
    {
        progressBar1.Increment(10);
        if (progressBar1.Value == 100)
        {
            Wrong.Stop();
            button1.Text = "";
            button2.Text = "";
            button3.Text = "";
            button4.Text = "";
            button5.Text = "";
            button6.Text = "";
            b1 = 1;
            b2 = 2;
            b3 = 3;
            b4 = 4;
            b5 = 5;
            b6 = 6;
            progressBar1.Value = 0;
        }
    }
    private void Tb1b2_Tick(object sender, EventArgs e)
    {
        progressBar1.Increment(10);
        if (progressBar1.Value == 100)
        {
            Tb1b2.Stop();
            button1.Visible = false;
            button2.Visible = false;
            progressBar1.Value = 0;
        }
    }
    private void Tb3b4_Tick(object sender, EventArgs e)
    {
        progressBar1.Increment(10);
        if (progressBar1.Value == 100)
        {
            Tb3b4.Stop();
            button3.Visible = false;
            button4.Visible = false;
            progressBar1.Value = 0;
        }
    }
    private void Tb5b6_Tick(object sender, EventArgs e)
    {
        progressBar1.Increment(10);
        if (progressBar1.Value == 100)
        {
            Tb5b6.Stop();
            button5.Visible = false;
            button6.Visible = false;
            progressBar1.Value = 0;
        }
    }
    private void button1_Click(object sender, EventArgs e)
    {
        if(b1 == 1)
        {
            b1 = 0;
            button1.Text = "kevin";
            if (b1 == b2)
            {
                Tb1b2.Start(); 
            }
            else if (b1 == b3)
            {
                Wrong.Start();
            }
            else if (b1 == b4)
            {
                Wrong.Start();
            }
            else if (b1 == b5)
            {
                Wrong.Start();
            }
            else if (b1 == b6)
            {
                Wrong.Start();
            }

        }
        else if (b1 == 0)
        {
            b1 = 1;
            button1.Text = "";
        }



    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (b2 == 2)
        {
            b2 = 0;
            button2.Text = "kevin";
            if (b2 == b1)
            {
                Tb1b2.Start();
            }
            else if (b2 == b3)
            {
                Wrong.Start();
            }
            else if (b2 == b4)
            {
                Wrong.Start();
            }
            else if (b2 == b5)
            {
                Wrong.Start();
            }
            else if (b2 == b6)
            {
                Wrong.Start();
            }
        }

        else if (b1 == 0)
        {
            b2 = 2;
            button2.Text = "";
        }

    }



    private void button3_Click(object sender, EventArgs e)
    {
        if (b3 == 3)
        {
            b3 = 0;
            button3.Text = "kath";
            if (b3 == b4)
            {
                Tb3b4.Start();
            }
            else if (b3 == b1)
            {
                Wrong.Start();
            }
            else if (b3 == b2)
            {
                Wrong.Start();
            }
            else if (b3 == b5)
            {
                Wrong.Start();
            }
            else if (b3 == b6)
            {
                Wrong.Start();
            }


        }
        else if (b3 == 0)
        {
            b3 = 3;
            button1.Text = "";
        }
    }



    private void button4_Click(object sender, EventArgs e)
    {
        if (b4 == 4)
        {
            b4 = 0;
            button4.Text = "kath";
            if (b4 == b3)
            {
                Tb3b4.Start();
            }
            else if (b4 == b1)
            {
                Wrong.Start();
            }
            else if (b4 == b2)
            {
                Wrong.Start();
            }
            else if (b4 == b5)
            {
                Wrong.Start();
            }
            else if (b4 == b6)
            {
                Wrong.Start();
            }


        }
        else if (b4 == 0)
        {
            b4 = 3;
            button4.Text = "";
        }
    }

    private void button5_Click(object sender, EventArgs e)
    {
        if (b5 == 5)
        {
            b5 = 0;
            button5.Text = "eka";
            if (b5 == b6)
            {
                Tb5b6.Start();
            }
            else if (b5 == b1)
            {
                Wrong.Start();
            }
            else if (b5 == b2)
            {
                Wrong.Start();
            }
            else if (b5 == b3)
            {
                Wrong.Start();
            }
            else if (b5 == b4)
            {
                Wrong.Start();
            }


        }
        else if (b5 == 0)
        {
            b5 = 5;
            button5.Text = "";
        }
    }
    private void button6_Click(object sender, EventArgs e)
    {
        if (b6 == 6)
        {
            b6 = 0;
            button6.Text = "eka";
            if (b6 == b5)
            {
                Tb5b6.Start();
            }
            else if (b6 == b1)
            {
                Wrong.Start();
            }
            else if (b6 == b2)
            {
                Wrong.Start();
            }
            else if (b6 == b3)
            {
                Wrong.Start();
            }
            else if (b6 == b4)
            {
                Wrong.Start();
            }


        }
        else if (b6 == 0)
        {
            b6 = 6;
            button6.Text = "";
        }
    }

    private void Reset_Click(object sender, EventArgs e)
    {
        b1 = 1;
        b2 = 2;
        b3 = 3;
        b4 = 4;
        b5 = 5 ;
        b6 = 6;
        button1.Visible = true;
        button2.Visible = true;
        button3.Visible = true;
        button4.Visible = true;
        button5.Visible = true;
        button6.Visible = true;
        button1.Text = "";
        button2.Text = "";
        button3.Text = "";
        button4.Text = "";
        button5.Text = "";
        button6.Text = "";

    }
}

2 Answers 2

1

I think i understand, you want to have the locations of the buttons different for every game.

Create a class to store the locations:

public class ButtonLocation
    {
        public int Left {get;set;}
        public int Top {get;set;}

    }

Then, populate a list with each of the possible locations, and then use Random to pick one location, and put a button there, the way you do that is up to you, as you may want to create something more scale-able in case you want more buttons in the future.

heres code for randomly picking the locations though :

private void Form1_Load(object sender, EventArgs e)
    {
        //Create a list of locations.
        List<ButtonLocation> buttonLocations = new List<ButtonLocation>() { 
            new ButtonLocation {Left = 100, Top = 100},
            new ButtonLocation {Left = 100, Top = 200},
            new ButtonLocation {Left = 100, Top = 300},
            new ButtonLocation {Left = 300, Top = 400},
            new ButtonLocation {Left = 300, Top = 500},
            new ButtonLocation {Left = 300, Top = 600}
        };
        //Instantiate Random
        Random random = new Random();

        //Loop through the different Locations
        while (buttonLocations.Count() > 0)
        {
            //Generate a random int from 0 to -1 of the number of possible locations
            int randomButtonLocationIndex = random.Next(0, buttonLocations.Count() - 1);
            //Create New button, Rememeber to add the event to this new button!
            Button button = new Button();

            button.Left = buttonLocations[randomButtonLocationIndex].Left;
            button.Top = buttonLocations[randomButtonLocationIndex].Top;

            this.Controls.Add(button);



            buttonLocations.RemoveAt(randomButtonLocationIndex);
        }





    }

Martyn

0

well, if you say different and mean that the location is random, then you have the location property that you can play with.

Random rand = new Random(5);
button1.Location = new Point(
       rand.Next(Width - button1.Width), 
       rand.Next(Height - button1.Height))

if you mean the code to be different you need to have a random token, that each button, upon being clicked will check if he got the token

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.