在C#windows窗体应用程序中连接4

在C#windows窗体应用程序中连接4,c#,forms,C#,Forms,我一直在尝试在C#中的windows窗体应用程序中创建connect 4(游戏),但我正在努力创建一种方法,以便在用户点击时硬币实际上会掉落而不是停留。我也不知道我应该把胜利条件法放在哪里。以下是我目前的代码: namespace ConnectFour { public partial class Form1 : Form { Button[] gameButtons = new Button[42]; //array of buttons for markers(red and bl

我一直在尝试在C#中的windows窗体应用程序中创建connect 4(游戏),但我正在努力创建一种方法,以便在用户点击时硬币实际上会掉落而不是停留。我也不知道我应该把胜利条件法放在哪里。以下是我目前的代码:

namespace ConnectFour
{
public partial class Form1 : Form
{
    Button[] gameButtons = new Button[42]; //array of buttons for markers(red and blue)
    bool blue = true; //blue is set to true if the next marker is to be a blue

    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]);
        }
    }
    private void buttonHasBeenPressed(object sender, int i)
    {
        if (((Button)sender).BackColor == Color.BlanchedAlmond)
        {
            if (blue == true)
            {
                ((Button)sender).BackColor = Color.Red;
            }
            else
            {
                ((Button)sender).BackColor = Color.Blue;
            }
            blue = !blue;
        }
    }
    private void fourInARow(int a, int b, int c,int d)
    {
        if (gameButtons[a].BackColor == gameButtons[b].BackColor && gameButtons[a].BackColor == gameButtons[c].BackColor && gameButtons[a].BackColor==gameButtons[d].BackColor)
        {
            if (gameButtons[a].BackColor == Color.Blue)
            {
                MessageBox.Show("the winner is player 1");
            }
            else
            {
                MessageBox.Show("the winner is player 2");
            }
        }
    }
}
名称空间连接四
{
公共部分类Form1:Form
{
按钮[]游戏按钮=新按钮[42];//标记的按钮数组(红色和蓝色)
bool blue=true;//如果下一个标记为蓝色,则将blue设置为true
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
this.Text=“Connect 4”;
this.BackColor=Color.BlanchedAlmond;
这个。宽度=500;
这个。高度=500;
对于(int i=0;i此。按钮被按下(发送者1,索引);
this.Controls.Add(gameButtons[i]);
}
}
私有无效按钮已按下(对象发送器,int i)
{
if(((按钮)sender.BackColor==Color.BlanchedAlmond)
{
如果(蓝色==真)
{
((按钮)发送器)。背景色=颜色。红色;
}
其他的
{
((按钮)sender.BackColor=Color.Blue;
}
蓝色=!蓝色;
}
}
私有void fourInARow(int a、int b、int c、int d)
{
如果(游戏按钮[a]。背景色==游戏按钮[b]。背景色和游戏按钮[a]。背景色==游戏按钮[c]。背景色和游戏按钮[a]。背景色==游戏按钮[d]。背景色)
{
如果(游戏按钮[a]。背景色==Color.Blue)
{
MessageBox.Show(“获胜者是玩家1”);
}
其他的
{
MessageBox.Show(“获胜者是玩家2”);
}
}
}
}

}

您现在可能已经明白了,但我要做的是查看每个
按钮的
背景色,在用户按下按钮的下方,直到我们点击底部一行,或者我们找到一个没有“BlanchedAlmond”
背景色的
按钮,然后这就是我们更改的按钮

要在另一个按钮的正下方找到一个
按钮
,我们只需查看
按钮
,它的
索引比当前的
索引
多7个。由于我们在
index
参数中将
按钮
对象的索引传递给我们的函数,所以我们可以使用它

下面是一个注释代码示例:

private void ButtonHasBeenPressed(object sender, int index)
{
    // Get the button that the user pressed
    var pressedButton = (Button)sender;

    // Only do something if they clicked a "neutral" button
    if (pressedButton.BackColor == Color.BlanchedAlmond)
    {
        // The backcolor will be set based on whether or not blue is true or false
        var newBackColor = blue ? Color.Red : Color.Blue;

        // Get the index of the button that the user clicked
        var buttonToChangeIndex = index;

        // From where the user clicked, look down at each button below (index + 7)
        // until we find the last button that has a BlanchedAlmond backcolor
        while (buttonToChangeIndex + 7 < gameButtons.Count() && 
               gameButtons[buttonToChangeIndex + 7].BackColor == Color.BlanchedAlmond)
        {
                buttonToChangeIndex += 7; // Set to the index to point to this button
        }

        // Now we set that button's backcolor
        gameButtons[buttonToChangeIndex].BackColor = newBackColor;

        // Flip our blue flag
        blue = !blue;
    }
}
private void按钮被按下(对象发送者,int索引)
{
//获取用户按下的按钮
var pressedButton=(按钮)发送器;
//只有当他们点击“中立”按钮时,才可以采取行动
如果(按Button.BackColor==Color.BlanchedAlmond)
{
//背景色将根据蓝色是否为真或假进行设置
var newBackColor=blue?Color.Red:Color.blue;
//获取用户单击的按钮的索引
var ButtonChangeIndex=指数;
//从用户单击的位置向下查看下面的每个按钮(索引+7)
//直到我们找到最后一个有白杏仁色背景的按钮
而(ButtonChangeIndex+7
使用一维数组来保存按钮的事实让事情变得有点困难。因为这个“连接四”游戏的布局是二维的,所以制作二维阵列似乎是合乎逻辑的。这将有助于在列中“删除”。您当前的代码将需要转换一维数组中的哪些单元格组成一列,即用户单击的单元格

下面的代码通过使用二维数组和获取给定列中下一个可用空间的行索引的方法来解决这个“删除”问题。此方法返回给定列中下一个可用的打开插槽索引,从底部开始向上,如果列已满,则返回-1

你应该认真考虑制作一个令牌/标记/按钮,然后另一个类是这些令牌的游戏板。查看水平、垂直和对角是否有4种连续颜色的逻辑可能很复杂。然而,这种逻辑完全适合GameBoard对象。此外,“GameBoard”对象可以跟踪所有动作等

这将极大地解放您的代码,使其专注于表单的视觉方面,而不必担心游戏的逻辑。我希望这是有道理的

Button[,] gameButtons = new Button[7,6]; //array of buttons for markers(red and blue)
bool blue = true; //blue is set to true if the next marker is to be a blue

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;
  int x;
  int y;

  for (int row = 0; row < gameButtons.GetLength(0); row++) {
    x = 50 + (row % 7) * 50;
    for (int col = 0; col < gameButtons.GetLength(1); col++) {
      y = 50*col + 50;
      Button newButton = new Button();
      newButton.Location = new System.Drawing.Point(x, y);
      newButton.Name = "btn" + (row + col + 1);
      newButton.Size = new System.Drawing.Size(50, 50);
      newButton.TabIndex = row + col;
      newButton.UseVisualStyleBackColor = true;
      newButton.Visible = true;
      newButton.Click += (sender1, ex) => this.buttonHasBeenPressed(sender1);
      gameButtons[row, col] = newButton;
      Controls.Add(gameButtons[row,col]);
    }
  }
}

private void buttonHasBeenPressed(object sender) {
  Button buttonClicked = (Button)sender;
  int col = buttonClicked.Location.X / 50 - 1;
  int targetRow = GetButtonRow(col);
  if (targetRow >= 0) {
    if (blue == true) {
      gameButtons[col, targetRow].BackColor = Color.Red;
    } else {
      gameButtons[col, targetRow].BackColor = Color.Blue;
    }
    blue = !blue;
  }
}

public int GetButtonRow(int colIndex) {
  Button curButton;
  for (int row = gameButtons.GetLength(1) - 1; row >= 0; row--) {
    curButton = gameButtons[arrayColIndex, row];
    if (curButton.BackColor != Color.Red && curButton.BackColor != Color.Blue) {
      return row;
    }
  }
  return -1;
}
按钮[,]游戏按钮=新按钮[7,6]//标记的按钮阵列(红色和蓝色)
布尔蓝=真//如果下一个标记为蓝色,则蓝色设置为true
公共表格1(){
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e){
this.Text=“Connect 4”;
this.BackColor=Color.BlanchedAlmond;
这个。宽度=500;