news 2026/9/5 18:05:19

智能麻将出牌组件

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
智能麻将出牌组件

开篇引言​

麻将作为一款风靡全球的策略性游戏,其复杂的规则和多变的牌局给玩家带来了无尽乐趣。在数字化时代,运用编程技术为麻将游戏赋予智能,实现自动出牌功能,不仅能提升玩家体验,还能深入探索算法在博弈游戏中的应用。今天,就和大家分享我如何使用 Java 编写一个智能麻将出牌组件的过程。​

项目地址:阿斗/mj-outCard - 码云 - 开源中国

1. 麻将规则简述​

简单介绍麻将的基本规则,如牌型(顺子、刻子、对子等)、胡牌方式、不同花色牌的作用等。强调这些规则是后续智能出牌算法设计的基础。例如,因为有顺子和刻子的牌型,所以在计算出牌策略时,需要考虑如何通过出牌来促进这些牌型的形成或完善。​

11-19为1万-9万,21-29为1条到9条,31-39为1筒到9筒,41-47为东西南北中发白,51-58为梅兰竹菊春夏秋冬

2. 技术选型 - Java 的优势​

说明选择 Java 作为开发语言的原因。如 Java 强大的跨平台性,方便组件能在不同操作系统的麻将游戏中集成;丰富的类库,在处理牌的逻辑、数据结构和算法实现时能提供便捷工具;良好的面向对象特性,有利于构建清晰、可维护的代码结构等。​

3.主要的public方法

public class MahjongIntelligentModule { private Mahjong mahjong; private volatile static MahjongIntelligentModule singleton; /** * 智能模块控制方法 * * @param handCards 手牌 * @param outCards 已出的牌 * @param laiZiCards 癞子牌 * @param handCard 本次需要出的牌 * @param queYiMenValue 缺一门牌值 * @param lastOutCard 上一次出的牌 * @param noOutCardList 不能出的牌 * @return 可以出的牌值 */ public MahjongIntelligentModule(List<Integer> handCards, List<Integer> outCards, List<Integer> laiZiCards, int handCard, int queYiMenValue, int lastOutCard, List<Integer> noOutCardList) { …… } /** * 通过智能算法获取可以出的牌值,判断出牌后听牌最大的可能性,通过outCards判断出这张牌是否还有可能胡的牌 * * @return 可以出的牌值 */ public int getOutCard() { …… } /** * 判断是否可以碰牌,通过牌型判断和outCards判断碰后是否会影响原来的牌型,加入影响则返回false,否则返回true * * @return 是否可以碰牌 */ public boolean getPengCard() { …… } /** * 判断是否可以杠牌,通过牌型判断和outCards判断杠后是否会影响原来的牌型,加入影响则返回false,否则返回true * * @return 是否可以杠牌 */ public boolean getGangCard(Integer cardType) { …… } }

4. 智能出牌概率计算

进张概率计算​; 拆对、刻、顺概率评估;综合出牌概率公式应用​

5. 测试与验证​

5.1 出牌测试

List<Integer> handCards = new ArrayList<>(Arrays.asList(12,13,14,15,16,17,24,26,32,33,35,37,38,26)); MahjongIntelligentModule mahjongIntelligentModule = new MahjongIntelligentModule(handCards,new ArrayList<>(Arrays.asList() ),new ArrayList<>(),-1,-1,-1,new ArrayList<>()); int outcard = mahjongIntelligentModule.getOutCard(); System.out.println("需要出的牌:"+outcard);


CardsInformationInfo{cardsInformations=……, outCard=24, maxChance=9.79}
需要出的牌:24

5.2 碰牌测试

List<Integer> handCards = new ArrayList<>(Arrays.asList(11,12,12,13,13,14,17)); int lastoutcard = 12; MahjongIntelligentModule mahjongIntelligentModule = new MahjongIntelligentModule(handCards,new ArrayList<>(Arrays.asList() ),new ArrayList<>(),-1,-1,lastoutcard,new ArrayList<>()); boolean pengcard = mahjongIntelligentModule.getPengCard(); System.out.println("碰的牌为:"+lastoutcard+"\t是否能碰:"+pengcard);

碰的牌为:12 是否能碰:false

List<Integer> handCards = new ArrayList<>(Arrays.asList(11,12,12,12,13,17,17)); int lastoutcard = 12; MahjongIntelligentModule mahjongIntelligentModule = new MahjongIntelligentModule(handCards,new ArrayList<>(Arrays.asList() ),new ArrayList<>(),-1,-1,lastoutcard,new ArrayList<>()); boolean pengcard = mahjongIntelligentModule.getPengCard(); System.out.println("碰的牌为:"+lastoutcard+"\t是否能碰:"+pengcard);

CardsInformationInfo{cardsInformations=……, outCard=17, maxChance=2.63}
出牌:17
碰的牌为:12 是否能碰:true

5.3吃牌测试

List<Integer> handCards = new ArrayList<>(Arrays.asList(11,12,12,12,13,17,17)); int lastoutcard = 12; MahjongIntelligentModule mahjongIntelligentModule = new MahjongIntelligentModule(handCards,new ArrayList<>(Arrays.asList() ),new ArrayList<>(),-1,-1,lastoutcard,new ArrayList<>()); List<Integer> chiList = mahjongIntelligentModule.getChiCard(); System.out.println("上加出的牌为:"+lastoutcard+"\t能吃的牌:"+chiList);

CardsInformationInfo{cardsInformations=……, outCard=17, maxChance=-0.15000000000000002}
出牌:17
上加出的牌为:12 能吃的牌:[11, 12, 13]

List<Integer> handCards = new ArrayList<>(Arrays.asList(12,13,14,15,16,17,18)); int lastoutcard = 12; MahjongIntelligentModule mahjongIntelligentModule = new MahjongIntelligentModule(handCards,new ArrayList<>(Arrays.asList() ),new ArrayList<>(),-1,-1,lastoutcard,new ArrayList<>()); List<Integer> chiList = mahjongIntelligentModule.getChiCard(); System.out.println("上家出的牌为:"+lastoutcard+"\t能吃的牌:"+chiList);

上家出的牌为:12 能吃的牌:null

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/5 18:02:47

C#解析通达信本地数据文件:高效获取股票代码与基础信息

简介&#xff1a;本资源是一套基于C#实现通达信股票代码实时获取的完整桌面应用工程&#xff0c;面向.NET开发者及量化交易初学者&#xff0c;解决在无官方API条件下通过剪贴板机制自动捕获通达信当前选中股票代码的技术难题。项目包含27个文件&#xff0c;涵盖7个核心C#源码&a…

作者头像 李华
网站建设 2026/9/5 18:00:38

Umi-OCR:免费离线 OCR 一次搞定截图、批量图片与扫描 PDF

Umi-OCR&#xff1a;免费离线 OCR 一次搞定截图、批量图片与扫描 PDF 【免费下载链接】Umi-OCR OCR software, free and offline. 开源、免费的离线OCR软件。支持截屏/批量导入图片&#xff0c;PDF文档识别&#xff0c;排除水印/页眉页脚&#xff0c;扫描/生成二维码。内置多国…

作者头像 李华
网站建设 2026/9/5 17:57:06

MATLAB实现DSSS通信系统仿真:从原理到误码率验证

简介&#xff1a;本资源是一套面向电子工程、通信工程及计算机相关专业本科生的毕业设计与课程作业实践方案&#xff0c;聚焦直接序列扩频&#xff08;DSSS&#xff09;通信系统的核心原理与MATLAB仿真实现。资源完整覆盖扩频序列生成&#xff08;M序列、Walsh码&#xff09;、…

作者头像 李华
网站建设 2026/9/5 17:54:44

QN8035 FM收音机硬件量产级参考设计详解

简介&#xff1a;本资源是一套完整的QN8035 FM/AM收音机硬件开发参考方案&#xff0c;面向嵌入式硬件工程师、电子设计爱好者及高校电子类专业学生&#xff0c;解决收音机模块选型、射频电路设计、音频功放匹配与数码管驱动实现等典型工程问题。压缩包共含10余个核心文件&#…

作者头像 李华
网站建设 2026/9/5 17:51:32

SpringBoot+Vue3前后端分离员工考勤管理系统:权限设计与实践详解

SpringBoot、Vue3、前后端分离&#xff0c;这三个词放在一起&#xff0c;基本就是当前 Java Web 课程设计和毕设项目里出现频率最高的组合之一了。如果再加上“员工考勤管理系统”&#xff0c;一个典型的后台管理业务闭环就出来了&#xff1a;部门员工管理、考勤打卡、请假审批…

作者头像 李华