-2

関数のコードは次のとおりです。ここでは、CheckXWinner勝利の線を描画するためにフォームを参照する必要があります。

    public void CheckXWinner(Button[] buttonArray, Form1 frm)
    {
        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

このクラスの別の部分には、フォームに関連付けられたすべてのボタン クリックのハンドラーがあります。

    //Handle any button clicks
    private void button_click(object sender, EventArgs e)
    {
        Button b = (Button)sender;
        b.Text = "X";
        b.Enabled = false;
        CheckXWinner(buttonArray, Form1 frm);
    }

その呼び出しの Form1 部分にエラーがあります。これを修正するにはどうすればよいですか???

4

2 に答える 2

1

渡す場所ではForm1 frm、実際には Form1 のインスタンスへの参照を渡す必要があります。渡すという提案に対するあなたのコメントに基づいてthis、ボタンハンドラーは別のフォーム内で(Form1ではなく)宣言されているようです。その場合は、Form1 インスタンスへの参照を取得/保持し、それを渡す必要があります。

CheckXWinner(buttonArray, a_ref_to_form);

ただし、その実装を見ると、とにかくどこかCheckXWinnerを参照しているようには見えません!frm

CheckXWinner の宣言を次のように書き直します。

public void CheckXWinner(Button[] buttonArray)

そして、次のように呼び出します。

CheckXWinner(buttonArray);
于 2013-03-10T17:50:11.030 に答える
0

frm は、パラメーターとして渡すときにどこにも宣言されていません。

Form1 frm の代わりに「this」を渡します。

于 2013-03-10T17:54:42.093 に答える