惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
J
Java Code Geeks
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
阮一峰的网络日志
阮一峰的网络日志
爱范儿
爱范儿
量子位
N
Netflix TechBlog - Medium
博客园 - 聂微东
博客园 - Franky
aimingoo的专栏
aimingoo的专栏
The Cloudflare Blog
T
The Blog of Author Tim Ferriss
MyScale Blog
MyScale Blog
Google DeepMind News
Google DeepMind News
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
C
Check Point Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC

博客园 - 石曼迪

.net core web api 发布填坑 Visual Studio 2026 开发 MAUI app 记录 Windows下搭建rustdesk远程服务 nginx 二级域名配置 .NET CORE 相关配置 frps-frpc配置 使用SuperSocket开发联网斗地主(四):出牌 使用SuperSocket开发联网斗地主(三):抢地主 使用SuperSocket开发联网斗地主(一):联网 SuperSocket服务端与客户端搭建 EXCEL脚本收藏 小程序开发记录 Python练习 Python基础总结 在centos7.6上部署.netcore 3.0 web程序 最简单的centos上安装Nginx办法 七牛网数据备份 solr搜索分词优化 Ionic异常及解决 狗日的腾讯
使用SuperSocket开发联网斗地主(二):发牌
石曼迪 · 2022-01-01 · via 博客园 - 石曼迪

实现思路:

将整幅牌数据和发牌数据都在服务端完成,客户端只存储自己的牌和底牌

一、服务端:

抽出一个牌类:

public class DouDiZhuGameCard
{
     public string CardColor { get; set; }
     public string CardName { get; set; }
     public int CardValue { get; set; }
}

分别表示花色,名称和值,值用来比较大小,然后把54张牌初始化进去。继续打乱这54张牌的顺序:

Random random = new Random();
var cards = DouDiZhuGameConfig.DoDiZhuCards;
for (int i = 0; i < cards.Count; i++)
{
     DouDiZhuGameCard temp;
     int j = random.Next(0, cards.Count - 1);
     temp = cards[j];
     cards[j] = cards[i];
     cards[i] = temp;
}

然后分发给3个玩家:

int playerIndex = 1;
foreach (var item in DouDiZhuGameConfig.UserList)
{
     item.Value.MyCards = cards.Skip((playerIndex-1)*17).Take(17).ToList();
     item.Value.MyCards.Sort((x, y) => x.CardValue.CompareTo(y.CardValue));

    item.Value.CommonCards= cards.Skip(51).Take(3).ToList();

    for (int i = 0; i < DouDiZhuGameConfig.MaxCard; i++)
     {
         item.Value.OtherPlayerCards.Add(new DouDiZhuGameCard { CardColor="",CardName="",CardValue=0});
     }

    response.Add(item.Key,item.Value);
     playerIndex++;
}

分给自己的是有数据的,用来在客户端显示,分给另外两家的是空数据(当然可以在客户端实现),另外把底牌也初始化,每个客户端都能看到。

然后根据scoketsession分发下去:

var result = GameManager.SendCardToPlayer();
foreach (var playerInfo in result)
{
     var play_session = webSocketServer.GetSessionByID(playerInfo.Key);
    
     ResponseModel<DouDiZhuUserInfoModel> res_sendCard = new ResponseModel<DouDiZhuUserInfoModel>();
     res_sendCard.Action = "sendcard";
     res_sendCard.Data = playerInfo.Value;

    if(session.SessionID==playerInfo.Key)//是我
     {
         res_sendCard.Data.MyCards = playerInfo.Value.MyCards;
         res_sendCard.Data.OtherPlayerCards = playerInfo.Value.OtherPlayerCards;
     }
     this.SendMessage(play_session, JsonConvert.SerializeObject(res_sendCard));

}

二、客户端:

在收到服务端通知的“startgame”之后显示每个玩家的牌:

player1=userData.OtherPlayerCards;

player2=userData.OtherPlayerCards;

player_me=userData.MyCards;

diPai=userData.CommonCards;

然后按照位置显示出来即可。

最终效果如图:

image

image

image

image

代码下载