This is for a tic tac toe game where the grid can be sized to any number (3x3 4x4 8x8 etc)
When the form loads, a method reads the grid size and populates the form with that many buttons in a 1 dimensional Button Array. The Array is called buttonArray.
With just using the 1-dimensional buttonArray and without using LINQ, how can i compare the .Text values of buttons in buttonArray to see if they are either "X" or "O" for a CheckWinner() function.
After the button grid is created, I have an event handler for button clicks :
private void button_click(object sender, EventArgs e)
{
Button b = (Button)sender;
b.Text = "X";
b.Enabled = false;
CheckWinner(buttonArray);
}
I am then calling my CheckWinner function and passing in the array buttonArray.
Again, I am just looking for a primitive way to check/compare values without using LINQ. If I know the length of each side of the grid, I can say that for Horizontal win lines, I am looking for that many buttons in a row with "X" as their .Text property.
So if I have a grid of 5x5, and I have 25 buttons in buttonArray, how can i check every 5 starting at the 0 index of the grid for their .Text values being "X" and then print a message if 5 in a row are the same, or "X" in this case.
for (int z = 0; z < root; z++) //increments the column to start on
{
vCount = 0; //Starts the win counter at zero when moving to the next column
for (int i = z; i < arrLength; i=i+root) //starts at the first column and increments it by root to test the next button in the column
{
string bText = buttonArray[i].Text;
if (bText == "X")
vCount++;
if (vCount == root)
{
MessageBox.Show("Vertical row winner found !");
break;
}
}
}//end of vertical column test
I did the vertical test like that? But I think combining them into one would def be better.
Assuming that you want to check if Button.Text == "X"
in every element of a horizontal row
. Following is a very basic way...
public void CheckWinner(Buttons[] buttonArray)
{
int arrLength = buttonArray.Length;
int hCount = 0;
//int vCount = 0;
int root = (int)Math.Sqrt(Convert.ToDouble(arrLength));
for (int i = 0; i < arrLength ; i++)
{
string bText = buttonArray[i].Text;
hCount = i % root == 0? 0 : hCount;
if(bText == "X")
hCount++;
if(hCount == root)
{
Console.WriteLine("Horizontal row winner found !");
break;
}
}
}
To check virtical and/or horizontal winner:
public void CheckWinner(Buttons[] buttonArray)
{
int arrLength = buttonArray.Length;
int hCount = 0;
Int vCount = 0;
int root = (int)Math.Sqrt(Convert.ToDouble(arrLength));
for (int i = 0; i < root; i++)
{
hCount = 0;
vCount = 0;
for(int j = 0; j < root; j++)
{
if(buttonArray[ (i * root) + j ].Text == "X")
hCount++;
if(buttonArray[ i + (root * j) ].Text == "X")
vCount++;
}
if( hCount + vCount == 2 * root)
{
Console.WriteLine("Horizontal and Virtical winner found !");
break;
}
else if ( hCount == root )
{
Console.WriteLine("Horizontal winner found !");
break;
}
else if ( vCount == root )
{
Console.WriteLine("Virtical winner found !");
break;
}
}
}