1)js版本
/**
* 基本思路:
* 回溯法: 先挑选中将,再依次拆接出3个,3个的...直到结束,那么说明当前可以胡牌
*/
let mahjong = [
"1T", "2T", "3T", "4T", "5T", "6T", "7T", "8T", "9T", // 桶
"1S", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", // 条
"1W", "2W", "3W", "4W", "5W", "6W", "7W", "8W", "9W", // 万
"DONG", "NAN", "XI", "BEI", // 风
"ZHONG", "FA", "BAI", // 箭
];
// 一张麻将牌,如: "2T", 转化为34张牌中的一个索引为1
function convert(s) {
for (let i = 0; i < 34; i++) {
if (mahjong[i] == s) {
return i;
}
}
return -1;
}
// 一副手牌
function hand_mj() {
// 13张手牌
this.mj = ["1T", "1T", "3T", "4T", "5T", "5T", "5T", "7T", "7T", "7T", "9T", "9T", "9T"];
// 对应的拍的索引: 0 1 2 3 4 5 6 7 8 0 1 2 4, 以方便计算当前手牌都有几张牌
this.mj_num = [];
// 34张牌,当前手牌中每张牌的数量
this.c = [];
}
// 基本数据初始化
hand_mj.prototype.init = function () {
// 34张牌, 每张数量都为0
for (let i = 0; i < 34; i++) {
this.c[i] = 0;
}
// 把每张牌转化为34张牌中的数字索引
for (let i = 0; i < 13; i++) {
// 如: 第1T对应第0张
this.mj_num[i] = convert(this.mj[i]);
}
// 统计13张牌中每张牌的数量. 统计完就知道0位置有2张, 1位置有2张...其它位置则为0
for (let i = 0; i < 13; i++) {
this.c[this.mj_num[i]]++;
}
console.log();
}
// 去掉将牌
hand_mj.prototype.check = function () {
for (let i = 0; i < 34; i++) {
// 如果当前位置,牌数目>=2,则可以当将,拆出来(AA)
if (this.c[i] >= 2) {
this.c[i] -= 2;
if (this.search(0)) {
this.c[i] += 2;
return true;
} else {
this.c[i] += 2;
}
}
}
return false;
}
// 递归深度
hand_mj.prototype.search = function (dep) {
// 去掉刻子(AAA)
for (let i = 0; i < 34; i++) {
if (this.c[i] >= 3) {
if (dep == 3) {
return true;
}
this.c[i] -= 3;
if (this.search(dep + 1)) {
this.c[i] += 3;
return true;
} else {
this.c[i] += 3;
}
}
}
// 去掉顺子(A A+1 A+2)
for (let i = 0; i < 34; i++) {
if (i % 9 <= 6 &&
this.c[i] >= 1 &&
this.c[i + 1] >= 1 &&
this.c[i + 2] >= 1) {
if (dep == 3) {
return true;
}
this.c[i]--;
this.c[i + 1]--;
this.c[i + 2]--;
if (this.search(dep + 1)) {
this.c[i]++;
this.c[i + 1]++;
this.c[i + 2]++;
return true;
} else {
this.c[i]++;
this.c[i + 1]++;
this.c[i + 2]++;
}
}
}
return false;
}
// 计算胡那些牌
hand_mj.prototype.cal_hu = function () {
let ret = [];
// 34张牌尝试加一张,看看胡不胡
for (let i = 0; i < 34; i++) {
// 尝试手牌加1张
this.c[i]++;
if (this.c[i] <= 4 && // 大于4张,不可能胡这张牌
this.check()) {
ret.push(mahjong[i]);
}
this.c[i]--;
}
return ret;
}
// 测试
let test_hand1 = new hand_mj();
test_hand1.init();
let s = new Date().getTime();
let ret = [];
for (let i = 0; i < 100000; i++) {
ret = test_hand1.cal_hu();
}
let e = new Date().getTime();
console.log(ret);
console.log("耗时:", (e - s))
2)java版本
HandCard.java
package com.my.mj_no_lz;
import java.util.ArrayList;
/**
* 自己的手牌
*/
public class HandCard {
/**
* 自己手牌中每一张牌的张数统计
*/
private int[] c = new int[]{
2, 0, 0, 0, 3, 0, 3, 0, 3, // 万
0, 0, 0, 0, 0, 0, 0, 1, 1, // 饼
0, 0, 0, 0, 0, 0, 0, 0, 0, // 条
0, 0, 0, 0, 0, 0, 0 // 东 西 南 北 中 发 白
};
public ArrayList getHuCards() {
// TODO 调用工具函数,计算手牌
ArrayList huCards = Logic.cal_hu(c);
return huCards;
}
}
Logic.java
package com.my.mj_no_lz;
import java.util.ArrayList;
public class Logic {
/**
* 计算当前胡牌
*
* @return
*/
public static ArrayList cal_hu(int[] c) {
ArrayList ret = new ArrayList<>();
// 每张牌挨个遍历,看是否胡这张牌
for (int i = 0; i < 34; i++) {
c[i]++;
if (c[i] >= 0 && c[i] <= 4 && check(c)) {
// TODO 胡第张牌
ret.add(i);
}
c[i]--;
}
return ret;
}
/**
* 检查当前牌手牌信息c是否胡牌
*
* @param c
* @return
*/
private static boolean check(int[] c) {
for (int i = 0; i < 34; i++) {
// 去掉将
if (c[i] >= 2) {
c[i] -= 2;
if (search(0, c)) {
c[i] += 2;
return true;
} else {
c[i] += 2;
}
}
}
return false;
}
/**
* 判断当前牌能不能组成扑
*
* @param dep
* @param c
* @return
*/
private static boolean search(int dep, int[] c) {
for (int i = 0; i < 34; i++) {
if (c[i] >= 3) {
if (dep == 3) {
return true;
}
c[i] -= 3;
if (search(dep + 1, c)) {
c[i] += 3;
return true;
} else {
c[i] += 3;
}
}
}
for (int i = 0; i < 31; i++) {
// TODO 注意连续性 9W和1饼是不能连续计算的
if ((i % 9 <= 6) && c[i] >= 1 && c[i + 1] >= 1 && c[i + 2] >= 1) {
if (dep == 3) {
return true;
}
c[i]--;
c[i + 1]--;
c[i + 2]--;
if (search(dep + 1, c)) {
c[i]++;
c[i + 1]++;
c[i + 2]++;
return true;
} else {
c[i]++;
c[i + 1]++;
c[i + 2]++;
}
}
}
return false;
}
}
Main.java
package com.my.mj_no_lz;
import java.util.ArrayList;
/**
* 不带癞子麻将胡牌算法思路:
* 1.一个34个元素的数组,里面标记每个麻将的牌的个数
* 2.添加一张牌
* 3.找出将(回溯)
* 4.找出扑(回溯)
* 5.如果剩余牌最终为0,那么说明能胡这张牌
*/
public class Main {
public static void main(String[] args) {
HandCard handCard = new HandCard();
long s = System.currentTimeMillis();
ArrayList huCards = handCard.getHuCards();
long e = System.currentTimeMillis();
System.out.println(huCards);
System.out.println("cost=" + (e - s));
}
}