C# 显示按钮数组中的winline

C# 显示按钮数组中的winline,c#,arrays,tic-tac-toe,C#,Arrays,Tic Tac Toe,我有一个tic-tac-toe程序,有一个可重新调整大小的网格。我有一个CheckXWinner()函数,用于检查网格中的水平、垂直和两个对角线获胜组合。win check非常有效,但在某个组合获胜后,我需要通过将这些获胜按钮变成不同的颜色来突出显示它们,我会说是IndiaRed,最好在它们之间画一条线。不过我很高兴至少能让它们变成红色 public void CheckXWinner(Button[] buttonArray) { int arrLength

我有一个tic-tac-toe程序,有一个可重新调整大小的网格。我有一个CheckXWinner()函数,用于检查网格中的水平、垂直和两个对角线获胜组合。win check非常有效,但在某个组合获胜后,我需要通过将这些获胜按钮变成不同的颜色来突出显示它们,我会说是IndiaRed,最好在它们之间画一条线。不过我很高兴至少能让它们变成红色

public void CheckXWinner(Button[] buttonArray)
{          
        int arrLength = buttonArray.Length; 
        int hCount = 0;
        int vCount = 0;
        int d1Count = 0;
        int d2Count = 0;
        int root = (int)Math.Sqrt(Convert.ToDouble(arrLength));  

        for (int i = 0;  i < root;  i++)
        {
            //Sets the counter for the winners back to zero
            d2Count = 0;
            d1Count = 0;
            hCount = 0;
            vCount = 0;
                for(int j = 0;  j < root; j++)
                {
                    //increments the appropriate counter if the button contains an X
                    if (buttonArray[ (i * root) + j ].Text == "X")
                        hCount++;

                    if (buttonArray[ j + (j * root)].Text == "X")
                        d1Count++;

                    if (buttonArray[(j * (root - 1)) + (root - 1)].Text == "X")
                        d2Count++;

                    if (buttonArray[ i + (root * j)].Text == "X")
                        vCount++;

                }
                //if the counter reaches the amount needed for a win, show win message
                if ( hCount == root )
                { 
                    MessageBox.Show("Horizontal winner found !");
                    break;
                }
                else if ( vCount == root )
                { 
                    MessageBox.Show("Virtical winner found !");
                    break;
                }
                else if ( d1Count == root )
                {
                    MessageBox.Show("Diagonal winner found !");
                    break;
                }
                else if (d2Count == root)
                {
                    MessageBox.Show("Diagonal winner #2 found !");
                    break;
                }
        }//end of for loop
   }//end of CheckXWinner
public void CheckXWinner(按钮[]buttonArray)
{          
int arrLength=buttonArray.Length;
int hCount=0;
int vCount=0;
int d1计数=0;
int d2计数=0;
int root=(int)Math.Sqrt(Convert.ToDouble(arrLength));
for(int i=0;i
一旦达到中奖计数,我如何返回并将这些按钮的.backcolor属性更改为IndiaRed?

我是这样解决的:

    public void CheckXWinner(Button[] buttonArray)
    {          
        int arrLength = buttonArray.Length; 
        int root = (int)Math.Sqrt(Convert.ToDouble(arrLength));  

        for (int i = 0;  i < root;  i++)
        {
            //Sets the counter for the winners back to zero
            int d2Count = 0;
            int d1Count = 0;
            int hCount = 0;
            int vCount = 0;
                for(int j = 0;  j < root; j++)
                {
                    //increments the appropriate counter if the button contains an X
                    //Horizonal win
                    if (buttonArray[(i*root) + j].Text == "X")
                    {
                        hCount++;
                        if (hCount == root)
                        {
                            for (int z = (root - 1); z >= 0; z--)
                            {
                                buttonArray[(i*root) + z].BackColor = Color.IndianRed;
                            }
                            Xwins();
                        }
                    }//end of Horizonal win

                    //Left to right diagonal
                    if (buttonArray[j + (j*root)].Text == "X")
                    {
                        d1Count++;
                        if (d1Count == root)
                        {
                            for (int z = (root - 1); z >= 0; z--)
                            {
                                buttonArray[z + (z * root)].BackColor = Color.IndianRed;
                            }
                            Xwins();
                        }
                    }//end of LTR win

                    //Right to left diagonal
                    if (buttonArray[(j*(root - 1)) + (root - 1)].Text == "X")
                    {
                        d2Count++;
                        if (d2Count == root)
                        {
                            for (int z = (root - 1); z >= 0; z--)
                            {
                                buttonArray[(z*(root - 1)) + (root - 1)].BackColor = Color.IndianRed;
                            }
                            Xwins();
                        }
                    }//end of RTL win

                    //Vertical win
                    if (buttonArray[i + (root*j)].Text == "X")
                    {
                        vCount++;
                        if (vCount == root)
                        {
                            for (int z = (root - 1); z >= 0; z--)
                            {
                                buttonArray[i + (root*z)].BackColor = Color.IndianRed;
                            }
                            Xwins();
                        }
                    }//end of vert win

                }//end of for j loop
        }//end of for loop

    }//end of CheckXWinner
public void CheckXWinner(按钮[]buttonArray)
{          
int arrLength=buttonArray.Length;
int root=(int)Math.Sqrt(Convert.ToDouble(arrLength));
for(int i=0;i=0;z--)
{
buttonArray[(i*根)+z]。背景色=颜色。印度红;
}
Xwins();
}
}//横向胜利的结束
//从左到右对角线
如果(按钮数组[j+(j*根)]。文本==“X”)
{
d1Count++;
if(d1Count==根)
{
对于(int z=(根-1);z>=0;z--)
{
buttonArray[z+(z*根)]。背景色=颜色。印度红;
}
Xwins();
}
}//LTR胜利结束
//从右到左对角线
if(buttonArray[(j*(root-1))+(root-1)]。Text==“X”)
{
d2Count++;
if(d2Count==根)
{
对于(int z=(根-1);z>=0;z--)
{
buttonArray[(z*(root-1))+(root-1)]。背景色=颜色。印度红;
}
Xwins();
}
}//RTL胜利结束
//垂直胜利
if(buttonArray[i+(root*j)]。Text==“X”)
{
vCount++;
if(vCount==根)
{
对于(int z=(根-1);z>=0;z--)
{
buttonArray[i+(根*z)]。背景色=颜色。印度红;
}
Xwins();
}
}//胜利的终点
}//j循环的结束
}//循环结束
}//支票赢家结束