C#,有两个按钮启动一个方法的问题

时间:2017-09-07 15:36:10

标签: c# winforms button

如果你来自美国,并且你曾经去过Cracker Barrel,那么你可能已经玩过你必须跳钉的棋盘游戏,直到你只剩下一个。它类似于中国的跳棋,只有金字塔或三角形。

我有一个制作按钮的表单并将它们添加到表单中,我有一个“TheBoard”类,其中包含跳转如何在表单上工作的所有规则。在我的表单中,我还有一个按钮点击方法,需要运行所有这些。

我似乎碰到了一堵砖墙。我无法弄清楚让它接受第二次点击的逻辑,以便遍历董事会类中的整个if语句。我在board类中移动方法的参数需要int x,这是您单击作为参数的按钮。我觉得我错过了下半场的举动。如何使用我的移动方法来注册两个按钮点击(挂钉的起始位置和挂钉的结束位置)?

表格代码:

public partial class Form1 : Form
{
    private Button[] btn = new Button[15];
    private TheBoard myboard = new TheBoard();

    public Form1()
    {
        InitializeComponent();

        int buttonsPerRow = 1;
        int index = 0;

        while (index < btn.Length)
        {
            int increment = this.Width / (buttonsPerRow + 1);

            for (int j = 1; j <= buttonsPerRow; j++)
            {
                btn[index] = new Button
                {
                    //other style elements of the button
                    Name = "btn" + index
                } 


                btn[index].Click += new EventHandler(this.My_Click);
                Controls.Add(btn[index]);

                index++;
            }

            buttonsPerRow++;
        }
    }

    private void My_Click(object sender, EventArgs e) {

    myboard.getValues();
    Button b = (Button)sender;
    string bName = b.Name;
    // Now pull off the btn
    string num = bName.Substring(3, bName.Length - 3);
    // Parsing the number to an int
    int x = Int32.Parse(num);
    myboard.move(x);

    int[] color = myboard.getValues();
    for (int i = 0; i < 15; i++)
    {
        color = myboard.getValues();
        if (color[i] == TheBoard.hasPeg)
        {
            btn[i].BackColor = System.Drawing.Color.Yellow;
        }
        else
            btn[i].BackColor = System.Drawing.Color.Black;
    }//for



    }
}

TheBoard类代码:

class TheBoard
{
  static public int hasPeg = 100;
    static public int noPeg = 50;
    private int[] board;
    private int firstMove; //1st click

    public TheBoard()
    {
        board = new int[15];
        board[0] = noPeg;
        for(int i = 1; i < 15; i++)
        {
            board[i] = hasPeg;
        }
        firstMove = -1; //giving last move a location, starting it at the beginning
    }

    public int move(int x)
    {
        if(firstMove == -1)
        {
            firstMove = x;
            return 0;
        }
        // blank at 0


        // if you click a blank your 1st move
        if (firstMove == noPeg)
        {
            Console.WriteLine("You cant move if there isn't a peg.");
            return 666;
        }


        // first---------------------------------------middle-----------------------end
        if (firstMove == 1 && board[0] == hasPeg && board[3] == hasPeg && board[6] == noPeg)
        {
            RemovePeg(board[0], board[3], board[6]);
            return 0;
        }
        if (firstMove == 1 && board[0] == hasPeg && board[2] == hasPeg && board[4] == noPeg)
        {
            RemovePeg(board[0], board[2], board[4]);
            return 0;
        }
        //etc for remaining firstMove possibilities

        firstMove = -1;
        return 5;
    }

    private int RemovePeg(int first, int second, int goal) {
        board[goal] = hasPeg;
        board[first] = noPeg;
        board[second] = noPeg;
        return 0;
    }

    public int[] getValues()
    {
        return board;
    }
}

1 个答案:

答案 0 :(得分:0)

我查看了代码,我想我理解你的问题;你可以选择起始钉,但你没有办法选择它应该去的地方。只需对代码进行最少的编辑,我会将第一个按钮单击存储在全局变量中,然后单击第二个按钮就知道它是第二个按钮并使用这两个信息启动电路板移动(并重置全局变量)。 / p>