繁体   English   中英

连接4获胜方法

[英]Connect 4 winning method

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

  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个瓷砖。 无论我用鼠标点击哪里,都会收到消息“有蓝/红匹配”。 我只是测试错了。

我不太明白你想做什么,但j + k != j总是真的也许你的意思是gameButtons[j + k] != gameButtons[j]并且无论如何它都无所谓因为整个循环没有做任何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,条件不满足。
祝好运

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM