0

我正在创建一个匹配游戏。我要完成的是检查先前单击的按钮的标记是否与“当前”单击的按钮的标记匹配。如果这些标签匹配,它将禁用这两个按钮,因为它们不再是游戏中的选项。我的部分困惑是在哪里集成这部分代码而不搞砸我的大部分工作。

Random myRandom = new Random();
    var buttons = new List<Button> { button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12 };
    var carString = new List<string> { "Camaro", "Mini Cooper", "Porsche 944", "Ford Focus", "Chevy Blazer", "Model T", "Camaro", "Mini Cooper", "Porsche 944", "Ford Focus", "Chevy Blazer", "Model T" };
    while (matchingButtonIndex < numOfButtons)
        {
            int index = myRandom.Next(carString.Count);
            var name = carString[index];
            if (name != null)
        {
        buttons[matchingButtonIndex].Tag = name;
        carString[index] = null;
        matchingButtonIndex = matchingButtonIndex + 1;
     }
   }
 }
    void SwitchTagWithText()
    {
        string text = lastButton.Text;
        lastButton.Text = lastButton.Tag.ToString();
        lastButton.Tag = text;
    }
    private void button1_Click(object sender, EventArgs e)
    {
        if (lastButton != null)
        {
            SwitchTagWithText();
        }

        lastButton = sender as Button;
        SwitchTagWithText();

        buttoncount++;
        label2.Text = buttoncount.ToString();
    }
4

3 回答 3

0

就像是:

private void button1_Click(object sender, EventArgs e)
    {
        if (lastButton != null)
        {
            SwitchTagWithText();
            var thisButton = sender as Button;
            if(thisButton.Text != lastButton.Text 
                 && thisButton.Tag.ToString() == lastButton.Tag.ToString())
            {
                //two different buttons with the same tag were clicked in succession
                thisButton.Enabled = false;
                lastButton.Enabled = false;
                lastButton = null;
                return;
            }
            lastButton = thisButton;
        }
        else
        {
           lastButton = sender as Button;
        }

        SwitchTagWithText();

        buttoncount++;
        label2.Text = buttoncount.ToString();
    }
于 2012-12-14T14:55:51.977 回答
0

我建议您创建一个新类来处理这个问题,例如

public class ButtonManager {
    private Button lastButton;

    public void SwitchTagWithText(Button button){
        string text = lastButton.Text;
        lastButton.Text = lastButton.Tag.ToString();
        lastButton.Tag = text;

        // Or whatever the logic is you need.
    }

    public void ButtonClicked(Button button){
        this.SwitchTagWithText(button);
        // Or whatever the logic is you need.
    }
}

好的,上面是不完整的,但希望主要思想能够通过 - 你有一个单独的类来处理标签值的读取/设置并确定是否应该禁用按钮。

所以在你的加载中,创建一个ButtonManager可以在整个表单代码中使用的新实例,所以让它成为一个字段。

于 2012-12-14T14:35:31.363 回答
0

您有一个按钮计数变量,每次单击都会增加该变量。所以你可以使用它:

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

    buttoncount++;

    SwitchTagWithText(thisButton);

    if (buttoncount == 1)
    {
        lastButton = thisButton;
    }
    else if (buttoncount == 2)
    {
        if (lastButton.Tag == thisButton.tag)
        {
            // Disable both buttons.
        }
        else
        {
            // Switch buttons back
        }

        buttoncount = 0;
    }
}
于 2012-12-14T14:35:34.433 回答