0

接続された 2 つのクライアントがゲームをプレイできる Tic Tac Toe ゲームを含むクライアント/サーバー アプリケーションを作成しています。3 x 3 グリッドは、動的に作成された 9 つのボタンで構成されています。最初のクライアントがグリッドのボタンをクリックすると、ボタンが無効になり、コンテンツに「X」が表示されます。値はサーバーに送信され、次に接続されている他のクライアントに送信されます。クライアントが受け取った値に基づいて、同じボタンを無効にし、コンテンツを「X」に変更したいと考えています。

私が抱えている問題は、クライアント側で動的に作成されたボタンを見つけることです。どんな助けでも大歓迎です!

 //Dynamically created 9 buttons on the client
 private void initBoard(int rank)
    {

        board = new tttBoard(rank);
        boardGrid.Children.Clear();
        boardGrid.Rows = rank;
        for (int i = 0; i < rank; i++)
        {
            for (int j = 0; j < rank; j++)
            {
                newButton = new Button();
                newButton.Tag = new Point(i, j);
                newButton.Name = "b" + i.ToString() + j.ToString();
                newButton.Content = newButton.Tag;
                boardGrid.Children.Add(newButton);
            }
        }
    }

//Method that receives data - CheckButton called method within this
public void OnDataReceived(IAsyncResult ar)
    {
        try
        {
            SocketPacket sckID = (SocketPacket)ar.AsyncState;
            int iRx = sckID.thisSocket.EndReceive(ar);
            char[] chars = new char[iRx];
            Decoder d = Encoding.UTF8.GetDecoder();
            int charLen = d.GetChars(sckID.dataBuffer, 0, iRx, chars, 0);
            szData = new String(chars);
            this.Dispatcher.Invoke((Action)(() =>
            {
                if(szData.Contains("Clicked button : "))
                {
                    return;
                }
                else
                    lbxMessages.Items.Add(txtMessage.Text + szData);
            }));

            ClickButton();

            WaitForData();
        }
        catch (ObjectDisposedException)
        {
            Debugger.Log(0, "1", "\n OnDataRecieved: Socket has been closed\n");
        }
        catch(SocketException se)
        {
            MessageBox.Show(se.Message);
        }
    }

//based on the message received from the server, I check to see if 
//it contains "Clicked button: " and a value that I use to locate the correct 
//button to disable and change content to 'x' to represent the move made by the 
//other client

public void ClickButton()
    {
        if (szData.Contains("Clicked button : "))
        {
            value = szData.Substring(17, 1);
        }
        this.Dispatcher.Invoke((Action)(() =>
        {
            btnName = "b0" + value;
            object item = grdClient.FindName(btnName);//boardGrid.FindName(btnName);
            if (item is Button)
            {
                Button btn = (Button)item;
                btn.IsEnabled = false;
            }
        }));
    }
4

1 に答える 1

1

クライアントコードを所有していますか? もしそうなら、あなたはそれを必要以上に難しくしているように聞こえます. 三目並べの各位置にあるボタンへの参照を含む 3x3 配列を使用して、実装を単純化してみませんか。あとは、クライアント メッセージからボタンの座標を抽出し、他のクライアントの同じ位置にあるボタンを更新するだけです。

たとえば、"clicked button : 2, 1" というメッセージは次のように変換されます。

buttonArray[2,1].Enabled = false;

または、ボタンに対して必要なその他のこと。

于 2012-12-12T18:10:39.490 に答える