C#网络版斗地主——地主权限的传递
2009-06-08 08:32:29 来源:WEB开发网(由于篇幅所限,这里只贴出与地主权限传递有关的代码)
public void AccpetClient1Data() //监听客户端1的消息
{
NetworkStream Ns1 = client1.GetStream();
string str1 = "";
while (true)
{
PokerGroup pg = new PokerGroup();
byte[] bytes1 = new byte[108];
Ns1.Read(bytes1, 0, 108);
str1 = Encoding.Default.GetString(bytes1);
(省略部分)
if (str1.StartsWith("AreYouLandLord"))
{
if (DConsole.LandLordNum == 1) //如果默认地主为服务器,那么根据文章开头的权限传递顺序图,server-client2-client1,client1为最后一个玩家,所以需要和牌重新来过
{
DConsole.Restart();
continue;
}
DConsole.player1.areYouLandLord = true; //当默认地主不是服务器时,设置服务器的叫地主权限为true,当timer控件读取到该值时,服务器的叫地主和不叫按钮马上就会显示出来.
continue;
}
if (str1.StartsWith("IamLandLord")) //客户端1为地主
{
DConsole.lblClient1Name.Text += "(地主)";
DConsole.lblClient1Name.ForeColor = System.Drawing.Color.Red;
SendDataForClient("ClientIsLandLord", 2); //通知客户端2,客户端1为地主
Thread.Sleep(sleep);
DConsole.PaintClient(20, 1);
SendDataForClient("LandLordPokers", DConsole.LandLordPokers, 1); //地主已选出,把牌发送给所有玩家
Thread.Sleep(sleep);
SendDataForClient("LandLordPokers", DConsole.LandLordPokers, 2);//地主已选出,把牌发送给所有玩家
DConsole.player1.SelectLandLordEnd(); //该方法的具体代码请看下文
continue;
}
}
}
/// <summary>
/// 循环接收客户端2的请求数据
/// </summary>
public void AccpetClient2Data()
{
NetworkStream Ns2 = client2.GetStream();
string str1 = "";
while (true)
{
PokerGroup pg = new PokerGroup();
byte[] bytes2 = new byte[108];
Ns2.Read(bytes2, 0, 108);
str1 = Encoding.Default.GetString(bytes2);
(省略部分)
if (str1.StartsWith("AreYouLandLord")) //客户端2不要地主
{
if (DConsole.LandLordNum == 2) //如果默认地主为客户端1,那么根据文章开头的权限传递顺序图,client1-server-client2,client2为最后一个玩家,所以需要和牌重新来过
{
DConsole.Restart();
continue;
}
SendDataForClient("AreYouLandLord", 1); //如果默认地主不是客户端1的话,按照顺序把叫地主权限传递给client1,client1的处理程序上文已经列出
continue;
}
if (str1.StartsWith("IamLandLord") ) //客户端2为地主
{
DConsole.lblClient2Name.Text += "(地主)";
DConsole.lblClient2Name.ForeColor = System.Drawing.Color.Red;
SendDataForClient("LandLordPokers", DConsole.LandLordPokers, 1);
SendDataForClient("LandLordPokers", DConsole.LandLordPokers, 2);
SendDataForClient("ClientIsLandLord", 1);
DConsole.PaintClient(20, 2);
DConsole.player1.SelectLandLordEnd();
continue;
}
}
}
private void btnNotLandLord_Click(object sender, EventArgs e)
{
this.player1.areYouLandLord = false;
this.player1.isLandLord = false;
this.btnNeedLandLord.Visible = false;
this.btnNotLandLord.Visible = false;
if (this.server != null) //玩家是服务端
{
if (DConsole.LandLordNum == 3) 如果client2是默认地主,根据权限传递顺序client2-client1-server,server为最后玩家,如果server不叫地主,只能和牌重来
{
DConsole.Restart();
return;
}
this.server.SendDataForClient("AreYouLandLord", 2); //如果client2不是默认地主,就把权限传递给client2
}
if (this.client != null) //如果玩家是客户端
{
this.client.SendDataForServer("AreYouLandLord");//告诉服务器自己不叫
}
}
系列文章:
C#写的网络版斗地主
C#网络版斗地主——网络部分实现
C#网络版斗地主——出牌权限的传递
更多精彩
赞助商链接