普通麻将的出牌AI如果不是要求特别高的话,其实蛮容易实现的,毕竟大多数人打牌都只是看自己的手牌。
所以作为简易版的AI,出牌的策略只要奔着胡牌去就可以了。我们能想到的就是把相邻或相同的牌凑到一起,把单独的牌打出去。以这个思路打牌,就会慢慢接近听牌至最终的胡牌。
我们简单举个例子,我们有1万2万,那么我们认为其打出去的优先级要高于单独的牌,因为其只需要1个三万就可以凑成一组了。
这种思路实际很像我们上一章讲的多赖子判胡的优化算法——插空法,如果没有看过上一博客的可以先看http://blog.csdn.net/sm9sun/article/details/77774722
我们看这里
for (var i = 0; i < 4; i++) {var needhun = 0;for (var j = 0; j < 4; j++) {needhun += j == i ? getneedhun(arr, j,false) : getneedhun(arr, j,true);}if (needhun <= Huncount) {return true;}}
我们通过needhun是否大于本身拥有的混牌(赖子)来判断是否可以胡,那么其实这个needhun就可以代表不同牌型胡牌的难度,即needhun值越小其越容易胡,当needhun值为1时(手牌数为3n+1),其处于听牌阶段。
那么这就很简单了,我们只需要在此基础上返回这个needhun,然后在出牌时看出那张牌返回的needhun最小,其就是我们的最佳策略选择。
function get_needhun_for_hu(old_arr, Hun, special) {var fmin_data = function (data1, data2) {return data1.needhun > data2.needhun ? data2 : data1;};var del_list = function (old_arr, i, j, data) {var arr = old_arr.concat();for (var k = 0; k < 3; k++) {if (arr[i + k] > 0) {arr[i + k]--;}else {data.needhun++;}}return dfs(arr, i, j, data);};var del_same = function (old_arr, i, j, data) {var arr = old_arr.concat();arr[i] %= 3;switch (arr[i]) {case 0: {break;}case 1: {if (data.hasjiang) { data.needhun += 2; }else { data.needhun++; data.hasjiang = true; }break;}case 2: {if (data.hasjiang) { data.needhun += 1; }else { data.hasjiang = true; }break;}}arr[i] = 0;return dfs(arr, i + 1, j, data);};var dfs = function (arr, i, j, data) {if (i > j) {if (!data.hasjiang) {data.needhun += 2;}return data;}if (i % 9 == 6 && i < 27 && arr[i + 1]%3 == 1 && arr[i + 2]%3 == 1)//8 9特殊情况,此时应该补个7{return del_list(arr, i, j, data);}else if (arr[i] == 0) {return dfs(arr, i + 1, j, data);}else if (i % 9 < 7 && i < 27 && (arr[i + 1] > 0 || arr[i + 2] > 0)) {var tmp1 = del_list(arr, i, j, { needhun: data.needhun, hasjiang: data.hasjiang });var tmp2 = del_same(arr, i, j, { needhun: data.needhun, hasjiang: data.hasjiang });return fmin_data(tmp1, tmp2);}else { return del_same(arr, i, j, data); }};var getneedhun = function (old_arr, type, hasjiang) {var data = {needhun: 0,hasjiang: hasjiang};var arr = old_arr.concat();var i, j;switch (type) {case 0: { i = 0; j = 8; break; }case 1: { i = 9; j = 17; break; }case 2: { i = 18; j = 26; break; }case 3: { i = 27; j = 33; break; }}data = dfs(arr, i, j, data);return data.needhun;};
var arr = old_arr.concat();var HunCount = 0; if (Hun >= 0) { HunCount = arr[Hun]; arr[Hun] = 0; }var count = 0;for (var i = 0; i < arr.length; i++) {count += arr[i];}var min_needhun = 0x1f;if (special.H_7pair&&count + HunCount == 14) {var needhun = 0;for (var i = 0; i < arr.length; i++) {var c = arr[i];if (c % 2==1) {needhun+=2;}}if (needhun<min_needhun) {min_needhun = needhun;}}if (special.H_13one&&count + HunCount == 14) {var ones = [0, 8, 9, 17, 18, 26, 27, 28, 29, 30, 31, 32, 33];for (var i = 0; i < ones.length; ++i) {if (arr[ones[i]] == 0) {needhun+=2;}}if (needhun < min_needhun) {min_needhun = needhun;}}for (var i = 0; i < 4; i++) {var needhun = 0;for (var j = 0; j < 4; j++) {needhun += getneedhun(arr, j, j != i);}if (needhun < min_needhun) {min_needhun = needhun;}}return min_needhun - HunCount;}
注:七小对&十三幺相对来说需要的混子数要少,原因在于当手牌过散时每张废牌都意味着需要2张混牌,而七小对&十三幺则只把废牌当作所需要替换的1张牌处理,这使得大部分散牌更适用于七小对&十三幺。故needhun需要+2以作平衡
还有就是如果当前处于可以听牌的阶段(needhun==1),那么可以在此之前选择听口最多的或者剩余牌最多的等策略
var ret_needhun = 0x1a;var ret_pai = list[0];var ret_tinglist = [];for (var k = 0; k < list.length; k++) {arr[list[k]]--;var Tinglist = new Array();var canting = CanTingPai(arr, hun, Tinglist, special);if (canting){//听牌数比对,也可以按其他方式比对,比如所听的牌接下来的剩余牌if (ret_tinglist.length < Tinglist.length){ret_tinglist = Tinglist;ret_needhun = 1;ret_pai = list[k];}}else if (ret_tinglist.length==0){var needhun = get_needhun_for_hu(arr, hun, special);if (needhun < ret_needhun) {ret_needhun = needhun;ret_pai = list[k];}}arr[list[k]]++;}return ret_pai;}
关于判听的算法有很多,大家可以参考我的前几篇麻将算法博客。
如果不是听牌的状态且needhun和ret_needhun相同时可以根据不同的麻将玩法再融入自己的想法策略DIY
比如:优先打风牌、优先打19等。
else if (needhun == ret_needhun){if (list[k] > 26)//风牌优先打{ret_needhun = needhun;ret_pai = list[k];}if ((list[k] % 9 < 1 || list[k] % 9 > 7 )&& ret_pai<=26)//边牌优先打{ret_needhun = needhun;ret_pai = list[k];}}
再比如:优先打无关联单一的牌
exports.is_nexus = function(i, arr) {if (i > 26) {return arr[i] > 0;}else if (i % 9 == 8) {return arr[i] > 0 || arr[i - 1] > 0 || arr[i - 2] > 0;}else if (i % 9 == 7) {return arr[i] > 0 || arr[i - 1] > 0 || arr[i - 2] > 0 || arr[i + 1] > 0;}else if (i % 9 == 0) {return arr[i] > 0 || arr[i + 1] > 0 || arr[i + 2] > 0;}else if (i % 9 == 1) {return arr[i] > 0 || arr[i + 1] > 0 || arr[i + 2] > 0 || arr[i - 1] > 0;}else {return arr[i] > 0 || arr[i + 1] > 0 || arr[i + 2] > 0 || arr[i - 1] > 0 || arr[i - 2] > 0;}}
完整方法代码:
exports.GetRobotChupai = function (list, special, hun) {if (hun == null) {hun = -1;}var arr = [];var Tingobj = [];for (var i = 0; i < special.mj_count; i++) {arr[i] = 0;}for (var j = 0; j < list.length; j++) {Tingobj[j] = {};if (arr[list[j]] == null) {arr[list[j]] = 1;}else {arr[list[j]]++;}}var ret_needhun = 0x1a;//假设所有牌都需要2个混子补缺,即:13*2=26var ret_pai = list[0];var ret_tinglist = [];var has_single = false;for (var k = 0; k < list.length; k++) {if (list[k] == hun) {continue;}arr[list[k]]--;var Tinglist = new Array();var canting = exports.CanTingPai(arr, hun, Tinglist, special);if (canting) {//听牌数比对,也可以按其他方式比对,比如所听的牌接下来的剩余牌if (ret_tinglist.length < Tinglist.length) {ret_tinglist = Tinglist;ret_needhun = 1;ret_pai = list[k];}}else if (ret_tinglist.length == 0) {var needhun = get_needhun_for_hu(arr, hun, special);if (!exports.is_nexus(list[k], arr))//如果是单一的手牌优先考虑{if (!has_single)//如果之前没有过单一的牌{ret_needhun = needhun;ret_pai = list[k];}has_single = true;if (list[k] > 26)//风牌优先打{ret_needhun = needhun;ret_pai = list[k];}if ((list[k] % 9 < 1 || list[k] % 9 > 7) && ret_pai <= 26)//边牌优先打{ret_needhun = needhun;ret_pai = list[k];}}else if (!has_single)//如果不是单一的手牌,且之前也没有过单一的牌{if (needhun < ret_needhun)//判断此张牌需要的混牌数{ret_needhun = needhun;ret_pai = list[k];}else if (needhun == ret_needhun) {if (list[k] > 26)//风牌优先打{ret_needhun = needhun;ret_pai = list[k];}if ((list[k] % 9 < 1 || list[k] % 9 > 7) && ret_pai <= 26)//边牌优先打{ret_needhun = needhun;ret_pai = list[k];}}}}arr[list[k]]++;}return ret_pai;}
如果你的手牌list是根据抓牌的顺序而没有进行过排序的话,也可以根据抓牌的顺序作为优先级参考等。
测试截图:
真实人机对战测试:(防作弊自摸胡、带赖子)