Question

That's my code below and it works perfectly fine, except there is one thing that doesn't work correctly. When I click two buttons to match, they should stay visible until the user clicks a third button. How can I do that? Thank you.

namespace Memorija_Seminarska
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            tableLayoutPanel1.Enabled = false;
            label1.Visible = false;
            label2.Visible = false;
            label3.Visible = false;
            progressBar1.Visible = false;
            label2.Text = vreme.ToString();

        }

        public int vreme = 150;

        Random random = new Random();

        Button firstClicked = null;
        Button secondClicked = null;

        List<string> drzava = new List<string>() 
        { 
            "Македонија","Македонија", "Бугарија","Бугарија", "Србија","Србија",
            "Германија","Германија", "Канада","Канада", "Шпанија","Шпанија", 
            "Португалија","Португалија", "Австрија","Австрија", "Данска","Данска",
            "Индија","Индија", "Италија","Италија", "Англија","Англија", 
            "Турција","Турција", "Грција","Грција","Хрватска","Хрватска", 
            "Холандија","Холандија", "Русија", "Русија", "Швајцарија","Швајцарија"
        };

        private void startButton_Click(object sender, EventArgs e)
        {
            Add();
            tableLayoutPanel1.Enabled = true;
            label1.Visible = true;
            label2.Visible = true;
            progressBar1.Visible = true;
            timer2.Start();
            timer3.Start();

        }

        private void Add()
        {
            foreach (Control control in tableLayoutPanel1.Controls)
            {
                Button b = control as Button;

                    if (b != null)
                    {
                        int randNum = random.Next(drzava.Count);
                        b.Text = drzava[randNum];
                        b.ForeColor = b.BackColor;
                        drzava.RemoveAt(randNum);
                    }
            }
        }


        private void button_Click(object sender, EventArgs e)
        {
            if (timer1.Enabled == true)
                return;

            Button clickedButton = sender as Button;

            if (clickedButton != null)
            {
                if (clickedButton.ForeColor == Color.Black)
                    return;


                    if (firstClicked == null)
                    {
                        firstClicked = clickedButton;
                        firstClicked.ForeColor = Color.Black;
                        return;

                    }

                    secondClicked = clickedButton;
                    secondClicked.ForeColor = Color.Black;

                    Win();

                    if (firstClicked.Text == secondClicked.Text)
                    {
                        firstClicked.BackColor = Color.GreenYellow;
                        secondClicked.BackColor = Color.GreenYellow;

                        firstClicked = null;
                        secondClicked = null;
                        return;

                    }


                    timer1.Start();

                }
            }

        private void timer1_Tick(object sender, EventArgs e)
        {

            timer1.Stop();

            firstClicked.ForeColor = firstClicked.BackColor;
            secondClicked.ForeColor = secondClicked.BackColor;

            firstClicked = null;
            secondClicked = null;

        }

        private void timer2_Tick(object sender, EventArgs e)
        {

            vreme--;
            label2.Text = vreme.ToString();

            if (vreme == 0)
            {
                tableLayoutPanel1.Enabled = false;

                label3.Text = "Game over!";
                label3.Visible = true;
                label2.Visible = false;
                timer2.Stop();
                timer3.Stop();
                label1.Visible = false;
                progressBar1.Visible = false;
            }

        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            progressBar1.Value -= 1;

            if (progressBar1.Value == 0)
            {
                timer3.Stop();
            }
        }

        private void Win()
        {
            foreach (Control control in tableLayoutPanel1.Controls)
            {
                Button button1 = control as Button;

                if (button1 != null)
                {

                    if (button1.ForeColor == button1.BackColor)
                    {
                        return;
                    }
                }
            }

            label3.Text = "Браво!!!";
            label3.Visible = true;
            tableLayoutPanel1.Enabled = false;
            timer2.Stop();
            timer3.Stop();
            label2.Visible = false;
            progressBar1.Visible = false;
            label1.Visible = false;

        }



    }
}
Was it helpful?

Solution

As far as I can see, your timer1_Tick handler performs the hiding automatically when it's time period expires. In case you want this hiding to happen manually when third card is clicked, you should not hide the buttons there, but should just perform a check in the beginning of button_Click:

private void button_Click(object sender, EventArgs e)
{
    //two cards are open and not matching (if they matched, they would be already null)
    if ( firstClicked != null && secondClicked != null )
    {
        //hide the buttons
        firstClicked.ForeColor = firstClicked.BackColor;
        secondClicked.ForeColor = secondClicked.BackColor;
        firstClicked = null;
        secondClicked = null;
    }
}

And delete the timer1.Start() from the end of the event handler.

OTHER TIPS

im not good at understanding other people code, but as far as i look through, you do this in here: private void button_Click(object sender, EventArgs e)

  1. Check for null
  2. get reference of button 1 and return
  3. get reference of button 2
  4. do win() method
  5. check for equality

here you should send step 5 to front line, check for null as MZetko said, and then check for equality, so it will be third click, else if you get reference as you did, after second button filled up, the checking will also launch

i hope i were helpful

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top