This article is a summary of the C # Network version of the landlord.
First the server uses a random number to select the default landlord. Then send the message to the selected player. The player receives the information and displays two buttons called "Landlord", "not called". If you choose not to call, you can call the landlord's permission to send to another player, if the other two players do not, the implementation of the restart () method of licensing.
The order in which the landlord's permission is passed is shown in the following illustration:
Specific implementation methods:
The server gets a random number of 1-3, 1 for the server is the default landlord (the default landlord is randomly selected the first to have permission to call the landlord), 2 represents CLIENT1 is the default landlord, 3 represents client2 is the default landlord. Only the server side can select the landlord, so this method in the server class
public void SendOrder(int Num)
{
switch (Num)
{
case 1:
DConsole.player1.areYouLandLord = true; //把叫地主权限给自己
break;
case 2:
this.SendDataForClient("AreYouLandLord", 1); //传递叫地主权限给client1
break;
case 3:
this.SendDataForClient("AreYouLandLord", 2); //传递叫地主权限给client2
break;
}
}
The client's circular receive data program analyzes the data sent by the server in the client class:
(The code is long, so only a portion of the connection to the landlord's permission is posted)
public void Acceptserverdata ()
{
NetworkStream Ns = client. GetStream ();
String str = "";
while (true)
{
byte[] bytes = new byte[108];
Ns.read (bytes, 0, 108);
str = Encoding.Default.GetString (bytes);
(omitted part)
if (str. StartsWith ("Areyoulandlord"))//If the server sends the message to the client, the client gets the landlord permission
{
DConsole.player1.areYouLandLord = true;//timer When the control detects that the property value is true, it shows the landlord and not the button
continue;
}
if (str. StartsWith ("Landlordpokers"))//Get 3 cards from the landlord that the server sends to the client, and the transfer of the landlord's permission after receiving the three cards ends the
{
Pokergroup pokers = new Pokergroup ();
str = str. Replace ("Landlordpokers", "");
byte[] Bytepg = Encoding.Default.GetBytes (str);
Pokers. Getpokergroup (BYTEPG);
Dconsole.landlordpokers = pokers///Save the received landlord card
DConsole.player1.SelectLandLordEnd ()//The method displays the landlord card in the center of the window and then Whether he is a landlord or not, if the landlord will add the landlord card to his own group. For the specific code of the method, see
Continue;
}
if (str. StartsWith ("Clientislandlord"))//Another client is the landlord
{
dconsole.lbLclient2name.text + = "(landlord)";
DConsole.lblClient2Name.ForeColor = System.Drawing.Color.Red;
Dconsole.paintclient (20);
Continue;
}
if (str. StartsWith ("Serverislandlord"))//server is the landlord
{
DConsole.lblClient1Name.Text + = "(landlord)";
DConsole.lblClient1Name.ForeColor = System.Drawing.Color.Red;
Dconsole.paintserver (20);
Continue;
}
(omitted after)
}
}