1. 程式人生 > >使用列舉類,抽象類進行遍歷實現類,匹配響應的牌型(十三水特殊牌型)

使用列舉類,抽象類進行遍歷實現類,匹配響應的牌型(十三水特殊牌型)


public abstract class SpecialPokerRegularSss implements Comparator<PokerHolderSss>{

public enum SpecialType{

TRIPLET_SEQUENCE(new SpecialPokerRegularSssTripletSequence()), // 三順子
TRIPLET_SAME_COLOR(new SpecialPokerRegularSssTripletSameColor()), // 三同花
SIX_HALF(new SpecialPokerRegularSssSixHalf()), // 六對半
FIVE_PAIR_THREE_STRIP(new SpecialPokerRegularSssFivePairThreeStrip()), // 五對三條
FOUR_SET_TRIPLET_STRIP(new SpecialPokerRegularSssFourSetTripletStrip()), // 四套三條
ALL_SAME_COLOR(new SpecialPokerRegularSssAllSameColor()), // 湊一色
ALL_SMALL(new SpecialPokerRegularSssAllSmall()), // 全小
ALL_BIG(new SpecialPokerRegularSssAllBig()), //全大
THREE_POINTS_WORLD(new SpecialPokerRegularSssTripletPointsWorld()), // 三分天下
TRIPLET_SAME_COLOR_SEQUENCE(new SpecialPokerRegularSssTripletSameColorSequence()), //三同花順
TWELVE_ROYALTY(new SpecialPokerRegularSssTwelveRoyalty()), // 十二皇族
A_DRAGON(new SpecialPokerRegularSssADragon()), // 一條龍
SUPERME_DRAGON(new SpecialPokerRegularSssSupermeDragon()); // 至尊清龍

private final SpecialPokerRegularSss specialPokerRegularSss;
private static final Map<Integer, SpecialType> SPECIAL_TYPE_MAP;

private SpecialType(SpecialPokerRegularSss specialPokerRegularSss) {
this.specialPokerRegularSss = specialPokerRegularSss;
}


public SpecialPokerRegularSss getSpecialPokerRegularSss() {
return specialPokerRegularSss;
}

static {
SPECIAL_TYPE_MAP = new HashMap<>(SpecialType.values().length);
for (final SpecialType item : SpecialType.values()) {
SpecialType.SPECIAL_TYPE_MAP.put(item.ordinal(), item);
}
}


public static SpecialPokerRegularSss getSpecialTypeFrom(Integer ordinal) {
return SpecialType.SPECIAL_TYPE_MAP.get(ordinal).getSpecialPokerRegularSss();
}
}

public static SpecialPokerRegularSss getSpecialPokerRegularsFrom(List<Poker> pokers) throws BizException {
return SpecialPokerRegularSss.getSpecialPokerRegularsFrom(pokers == null ? new Poker[0] : pokers.toArray(new Poker[pokers.size()]));
}

public SpecialPokerRegularSss() {

}

//  獲取所有SpecialPokerRegularSss.class的抽象類的實現類  最後用canPlay()方法匹配響應的實現類

//因為是十三水的特殊牌型 ,所以從最大的至尊清青龍開始 

public static SpecialPokerRegularSss getSpecialPokerRegularsFrom(Poker... pokers) throws BizException {
pokers = pokers == null ? Poker.EMPTY_POKERS : pokers;
for(int index = SpecialType.values().length - 1; index >=0; index--){
SpecialPokerRegularSss specialPoker =  SpecialType.getSpecialTypeFrom(index);
if(specialPoker != null && specialPoker.canPlay(pokers)){
return specialPoker;
}
}
return null;
}

@Override
public final boolean canPlay(Poker[] pokers) throws BizException {
final PokerHolderSss pokerHolderSss = new PokerHolderSss(pokers);
return this.canPlay(pokerHolderSss);
}


protected abstract boolean canPlay(PokerHolderSss pokerHolderSss);


}

實現類 

舉例,這是其中一個,每個都是這樣實現,就能自動匹配到實現類的canPlay方法

class SpecialPokerRegularSssADragon extends SpecialPokerRegularSss { // 一條龍


@Override
protected boolean canPlay(PokerHolderSss pokerHolderSss) { // A ~ K
return pokerHolderSss.getValueCount() == pokerHolderSss.getPokerCount();
}


}