[英]Connect 4 winning method
I've created this method to check for horizontal matches (then I will expand in vertical and diagonally) but the problem is that my method "triggers" only when there are 5 tiles on the same column, and I want to trigger when I have 4. This is the method I created. 我已经创建了这个方法来检查水平匹配(然后我会在垂直和对角线上展开)但问题是我的方法只在同一列上有5个图块时“触发”,我想在我有时触发这是我创建的方法。 I'm using an 1 dimensional array. 我正在使用一维数组。 I can give you the whole code if you need it. 如果你需要,我可以给你整个代码。
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");
}
}
}
Edit: Added the whole code. 编辑:添加了整个代码。 Edit 2: I just realised. 编辑2:我刚刚意识到。 My code isn't even working as it should even with 5 tiles. 我的代码甚至没有工作,甚至应该使用5个瓷砖。 Where ever I click with my mouse I get the message " There is a blue/red match". 无论我用鼠标点击哪里,都会收到消息“有蓝/红匹配”。 I was just testing it wrong. 我只是测试错了。
I didn't quite understand what you want to do but j + k != j
is always going to be true maybe you mean gameButtons[j + k] != gameButtons[j]
and it doesn't matter anyway because the whole loop doesn't do anything end = true;
我不太明白你想做什么,但j + k != j
总是真的也许你的意思是gameButtons[j + k] != gameButtons[j]
并且无论如何它都无所谓因为整个循环没有做任何end = true;
is always going to be executed and the condition on the next if statement is going to be met maybe you mean to do this : 总是会被执行,并且下一个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");
}
}
}
also the code inside your loop is only going to be executed twice, for k=50 and for k=100, for k=150 the condition is not met. 你的循环中的代码也只会执行两次,对于k = 50和k = 100,对于k = 150,条件不满足。
good luck 祝好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.