1

つまり、これはTicTacToeゲームであり、App.configで設定することでボタングリッドのサイズが変化します。これは、CheckXWinnerXが勝った場合、勝った行のコンボを強調表示し、ボタンを赤に変える私の機能です。勝利パターンの最初から最後まで描画される動的な線画(勝利コンボを通る線)を追加したいと思います。MSDNでは、Microsoft.VisualBasic.PowerPacks.VSの参照を追加するように指示されています。using Microsoft.VisualBasic.PowerPacks;次に、線を引くコードの例を次に示します。

ShapeContainer canvas = new ShapeContainer();
LineShape theLine = new LineShape();
// Set the form as the parent of the ShapeContainer.
canvas.Parent = this;
// Set the ShapeContainer as the parent of the LineShape.
theLine.Parent = canvas;
// Set the starting and ending coordinates for the line.
theLine.StartPoint = new System.Drawing.Point(0, 0);
theLine.EndPoint = new System.Drawing.Point(640, 480);

CheckXWinner関数のコードは次のとおりです。

public void CheckXWinner(Button[] buttonArray)
    {

        int arrLength = buttonArray.Length; 
        int root = (int)Math.Sqrt(Convert.ToDouble(arrLength));
        bool winner = false;//variable to keep the computer from going when Xwins
        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();
                            winner = true; //sets winner to true so computer does not take turn 
                        }
                    }//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();
                            winner = true; 
                        }
                    }//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();
                            winner = true; 
                        }
                    }//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();
                            winner = true; 
                        }
                    }//end of vert win                        
                }//end of for j loop
        }//end of for loop
        CheckDraw();
        if (winner == false)
        {
            ComputerGoes(buttonArray);
        };

    }//end of CheckXWinner

線画のコードのフォームへの参照を取得するにはどうすればよいですか?その後、勝ち線を赤くするためのループにあるポイントを呼び出すだけです。ありがとう!

4

0 に答える 0