麻将胡牌算法的Java实现

最近一个棋牌游戏项目中涉及对麻将胡牌的判定,网上搜了搜虽然看到一些算法,但是感觉都不尽如人意,一般麻将的胡牌为1对和4组三张牌的连牌,所以在网上搜到的算法往往都死死的为了这个目的来实现,而且多数没有考虑到对百塔牌的支持,下面贴上代码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.util.Arrays;

/**
 * 单张麻将牌
 *
 * @author 奔跑 QQ:361817468
 */
public class MahjongTile
{

    public static int MAHJONG_TILE_TYPE_TEN_THOUSAND = 1;
    public static int MAHJONG_TILE_TYPE_PIE = 2;
    public static int MAHJONG_TILE_TYPE_STRIP = 3;
    public static int MAHJONG_TILE_TYPE_WIND = 4;
    public static int MAHJONG_TILE_TYPE_MESS = 5;
    public static int MAHJONG_TILE_TYPE_FLOWER = 6;
    
    /**
     * 标准麻将的各种牌的名称,该名称为一个三维数组,第一维为各套独立的名称
     * 第二维为每套名称中的不同类别,例如万和桶九属于不同类型的牌
     * 第三维维具体的名称
     */
    public final static String[][][] STANDARD_MAHJONG_NAMES = {
    
        new String[][]{
            {"一万","二万","三万","四万","五万","六万","七万","八万","九万"},
            {"一桶","二桶","三桶","四桶","五桶","六桶","七桶","八桶","九桶"},
            {"一条","二条","三条","四条","五条","六条","七条","八条","九条"},
            {"东风","南风","西风","北风"},
            {"红中","发财","白板"},
            {"春","夏","秋","冬","梅","兰","竹","菊"}
        },
        new String[][]{
            {"一万","二万","三万","四万","五万","六万","七万","八万","九万"},
            {"一饼","二饼","三饼","四饼","五饼","六饼","七饼","八饼","九饼"},
            {"一条","二条","三条","四条","五条","六条","七条","八条","九条"},
            {"东风","南风","西风","北风"},
            {"红中","发财","白板"},
            {"春","夏","秋","冬","梅","兰","竹","菊"}
        }
    };

    private final int type;
    private final int typeId;
    private final int uniqueId;

    public MahjongTile(String name) throws MahjongTileInitWrongTypeAndTypeIdException, MahjongTileInitWrongNameException
    {
        for (String[][] standardMahjongName : STANDARD_MAHJONG_NAMES)
        {
            for (int j = 0; j < standardMahjongName.length; j++)
            {
                for (int k = 0; k < standardMahjongName[j].length; k++)
                {
                    if (standardMahjongName[j][k].equals(name))
                    {
                        this.type = j + 1;
                        this.typeId = k + 1;
                        this.uniqueId = computeUniqueId(type, typeId);
                        return;
                    }
                }
            }
        }
        throw new MahjongTileInitWrongNameException(name);
    }
    
    public MahjongTile(int type, int typeId) throws MahjongTileInitWrongTypeAndTypeIdException
    {
        this.uniqueId = computeUniqueId(type, typeId);
        this.type = type;
        this.typeId = typeId;
    }

    private void initCheck(int type, int typeId) throws MahjongTileInitWrongTypeAndTypeIdException
    {
        if (STANDARD_MAHJONG_NAMES[0].length < type || type < 1)
        {
            throw new MahjongTileInitWrongTypeAndTypeIdException(type, typeId, true);
        }
        else if (STANDARD_MAHJONG_NAMES[0][type - 1].length < typeId || typeId < 1)
        {
            throw new MahjongTileInitWrongTypeAndTypeIdException(type, typeId, false);
        }
    }

    private int computeUniqueId(int type, int typeId) throws MahjongTileInitWrongTypeAndTypeIdException
    {
        initCheck(type, typeId);
        if (type == MAHJONG_TILE_TYPE_TE
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值