连接4获胜方法

时间:2017-03-31 10:26:53

标签: c# forms

我已经创建了这种方法来检查水平匹配(然后我会在垂直和对角线上展开),但问题是我的方法"触发"只有当同一列上有5个图块时,我想在我有4时触发。这是我创建的方法。我使用的是1维数组。如果需要,我可以给你完整的代码。

  public partial class Form1 : Form
 {
     Button[] gameButtons = new Button[42]; //array of buttons for  markers(red and blue)
    bool blue = true; 
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.Text = "Connect 4";
        this.BackColor = Color.BlanchedAlmond;
        this.Width = 500;
        this.Height = 500;

        for (int i = 0; i < gameButtons.Length; i++)
        {
            int index = i;
            this.gameButtons[i] = new Button();
            int x = 50 + (i % 7) * 50;
            int y = 50 + (i / 7) * 50;

            this.gameButtons[i].Location = new System.Drawing.Point(x, y);
            this.gameButtons[i].Name = "btn" + (index + 1);
            this.gameButtons[i].Size = new System.Drawing.Size(50, 50);
            this.gameButtons[i].TabIndex = i;
            //this.gameButtons[i].Text = Convert.ToString(index);
            this.gameButtons[i].UseVisualStyleBackColor = true;
            this.gameButtons[i].Visible = true;

            gameButtons[i].Click += (sender1, ex) => this.buttonHasBeenPressed(sender1, index);
            this.Controls.Add(gameButtons[i]);
        }
    }
    {
        var pressedButton = (Button)sender;
        //getLocation(sender);

        if (pressedButton.BackColor == Color.BlanchedAlmond)
        {
            var newBackColor = blue ? Color.Blue : Color.Red;

            var buttonToChangeIndex = index;

            while (buttonToChangeIndex + 7 < gameButtons.Count() && gameButtons[buttonToChangeIndex + 7].BackColor == Color.BlanchedAlmond)
            {
                buttonToChangeIndex += 7;  // Set to the index to point to this button.
            }

            gameButtons[buttonToChangeIndex].BackColor = newBackColor;

            blue = !blue;

            winCon(sender,index);
        }

private void winCon(object sender, int index)
    {
        int xLocation = index % Width;
        int yLocation = index / Width;
        int j = xLocation + Width * yLocation;
        bool end = false;

        for (int k = 50; k < 150; k = k + 50)
        {
            if (j + k != j)
            {
                end = false;
            }
        }
        end = true;

        if (end == true)
        {
            if (gameButtons[j].BackColor == Color.Blue)
            {
                MessageBox.Show("There is a blue match");

            }
            if (gameButtons[j].BackColor == Color.Red)
            {
                MessageBox.Show("There is a red match");
            }
        }
}

编辑:添加了整个代码。 编辑2:我刚刚意识到。我的代码甚至没有工作,甚至应该使用5个瓷砖。我用鼠标点击哪里就得到了消息&#34;有一个蓝色/红色匹配&#34;。我只是测试错了。

1 个答案:

答案 0 :(得分:0)

我并不完全明白你想做什么,但j + k != j总是如此,也许你的意思是gameButtons[j + k] != gameButtons[j]而且它无论如何都不重要因为整个循环没有& #39; t做任何事情end = true;始终会被执行,并且下一个if语句的条件将被满足,也许你的意思是这样做:

private void winCon(object sender, int index)
{
    int xLocation = index % Width;
    int yLocation = index / Width;
    int j = xLocation + Width * yLocation;
    bool end = true;

    for (int k = 50; k < 150; k = k + 50)
    {
        if (gameButtons[j + k] != gameButtons[j])
        {
            end = false;
        }
    }
    if (end == true)
    {
        if (gameButtons[j].BackColor == Color.Blue)
        {
            MessageBox.Show("There is a blue match");

        }
        if (gameButtons[j].BackColor == Color.Red)
        {
            MessageBox.Show("There is a red match");
        }
    }
}

你的循环中的代码也只会被执行两次,对于k = 50和对于k = 100,对于k = 150,则不满足条件。
祝你好运