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. 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. My code isn't even working as it should even with 5 tiles. 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;
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 :
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.
good luck
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.