有什么方法可以基于两个数组集的索引来禁用一对匹配的按钮吗?

时间:2019-04-30 22:01:40

标签: c# windows

我正在尝试基于学习两种外语来构建游戏(与游戏匹配的按钮)。在两个不同的 tableLayoutPanels 中有两组按钮。我已经使用两个数组集分配了按钮的值。该程序将像一个单词与另一种语言的含义匹配时一样执行,然后在单击后禁用这两个按钮。当我设置匹配按钮的条件时会发生问题。我通过在按钮的文本值相同时给出if条件来匹配它们。但是现在由于按钮的值(文本)不同,但是两个不同单词的含义相同,因此我想通过调用这些数组集的数组索引来匹配它们。我怎样才能做到这一点?有更好的解决方案吗?请告诉我。提前致谢。

我尝试通过在firstButtonClick.Text == secondButtonClick.Text上设置if条件来匹配一对按钮。但是现在我必须匹配具有相同含义的两个不同值(两个不同的语言单词)。

    // two global array
    string[] words1 = new string[] {
    "Book1", "Book2", "Book3", "Book4", "Book5",
    "Book6", "Book7", "Book8", "Book9", "Book10"
    };

    string[] words2 = new string[] {
    "Book1", "Book2", "Book3", "Book4", "Book5",
    "Book6", "Book7", "Book8", "Book9", "Book10"
    };

     public Form1()
    {
        InitializeComponent();
        assignValues1();
        assignValues2();
    }

    // function for assigning values inside the first set of buttons
    private void assignValues1()
    {
        int counter = 0;
        Array.Reverse(words1);

        foreach (Button btn in tableLayoutPanel1.Controls.OfType<Button>())
        {
            for (int i = 0; i < words1.Length; i++)
            {
                btn.Text = words1[counter]; 

            }
            counter++;
        }
    }

    // function for assigning values inside the second set of buttons
    private void assignValues2()
    {
        int counter = 0;

        foreach (Button btn in tableLayoutPanel2.Controls.OfType<Button>())
        {
            for (int i = 0; i < words1.Length; i++)
            {
                btn.Text = words2[counter]; 
            }
            counter++;
        }
    }

    Button btnClicks1, btnClicks2;

    // this function below is applied in all the buttons in two different 
    // tableLayoutPanels

    private void button1_Click(object sender, EventArgs e)
    {
        Button clickedBtn = sender as Button;

        if (btnClicks1 != null && btnClicks2 != null)
        {
            return;
        }

        if (clickedBtn.BackColor == Color.Coral)
            return;

        if (btnClicks1 == null)
        {
            btnClicks1 = clickedBtn;
            btnClicks1.BackColor = Color.Coral;
            return;
        }

        btnClicks2 = clickedBtn;
        btnClicks2.BackColor = Color.Coral;

        // the problems happens here. which condition should I use. Or is
        // there any better solution then as I'm trying to do?

        for (int i = 0; i < words1.Length; i++)
        {
            for (int j = 0; j < words2.Length; j++)
            {
                if (words1[i] == words2[j])
                {
                    btnClicks1.Enabled = false;
                    btnClicks2.Enabled = false;

                    btnClicks1 = null;
                    btnClicks2 = null;
                }
                else
                    timer1.Start();
            }
        }
        /*
        // When the values inside the text of the buttons were same.

        if (btnClicks1.Text == btnClicks2.Text)
        {
            checkingArrayValue();
            btnClicks1 = null;
            btnClicks2 = null;
        }
        else
        {
            timer1.Start();
        }*/
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        timer1.Stop();

        btnClicks1.BackColor = Color.LightGray;
        btnClicks2.BackColor = Color.LightGray;

        btnClicks1 = null;
        btnClicks2 = null;
    }

我想匹配来自两种不同语言的具有相同含义的按钮,并禁用这两个按钮,但是我得到的只是一个例外。

0 个答案:

没有答案