0

サイズ変更可能なグリッドを備えた三目並べプログラムがあります。グリッド内の水平、垂直、および両方の対角線の勝利コンボをチェックする 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 に変更するにはどうすればよいですか?

4

1 に答える 1

0

私はこのようにそれを解決しました:

    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
于 2013-03-02T07:56:49.243 に答える