【问题标题】:Showing a winline from a button array显示按钮数组中的赢线
【发布时间】:2013-03-27 17:05:28
【问题描述】:

我有一个带有可调整大小网格的井字游戏。我有一个 CheckXWinner() 函数,用于检查网格中的水平、垂直和对角线获胜组合。获胜检查效果很好,但是在某个组合获胜后,我需要通过将它们变成不同的颜色来突出显示这些获胜按钮,我会说 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

一旦达到中奖次数,我该如何返回并将这些按钮的 .backcolor 属性更改为 IndiaRed?

【问题讨论】:

    标签: c# arrays tic-tac-toe


    【解决方案1】:

    我是这样解决的:

        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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 1970-01-01
      • 2021-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多