簡體   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