public static final int ECARDCOLOR_WAN = 0;
public static final int ECARDCOLOR_TONG = 1;
public static final int ECARDCOLOR_TIAO = 2;
public static final int ECARDCOLOR_DONG = 3;
public static final int ECARDCOLOR_NAN = 4;
public static final int ECARDCOLOR_XI = 5;
public static final int ECARDCOLOR_BEI = 6;
public static final int ECARDCOLOR_ZHONG = 7;
public static final int ECARDCOLOR_FA = 8;
public static final int ECARDCOLOR_BAI = 9;

public static class Card {
private int cardColor;
private int cardFaceValue;

public Card() {
}

public Card(int color, int number) {
this.cardColor = color;
this.cardFaceValue = number;
}

public Card(Card c) {
this.cardColor = c.getCardColor();
this.cardFaceValue = c.getCardFaceValue();
}
    public int getCardColor() {
return this.cardColor;
}

public void setCardColor(int cardColor) {
this.cardColor = cardColor;
}

public int getCardFaceValue() {
return this.cardFaceValue;
}

public void setCardFaceValue(int cardFaceValue) {
this.cardFaceValue = cardFaceValue;
}

}
/**
* 发牌
* @param config : 发牌配置,都发哪些牌
* @param shuffleCnt : 洗牌次数
* @return
*/
public Card[][] shuffle(ShuffleConfig config, int shuffleCnt) {

logger.info("[ShuffleCards][shuffle] config="+new Gson().toJson(config));

int shufflePlayer = config.getShuffleCount();
logger.info("[ShuffleCards][shuffle] shufflePlayer ="+shufflePlayer);

//将发牌配置数组化
int[] shuffleConfig = initShuffleConfig(config);

if(this.cardLst.isEmpty()) {
List<Card> tmpCards = initCards(shuffleConfig);
this.cardLst.addAll(tmpCards);

}
List<Card> cards = new ArrayList<>();
List<Card> tmpCards = new ArrayList<>();
tmpCards.addAll(this.cardLst);

int size = tmpCards.size();
while(size > 0) {
int r = new Random().nextInt(size);
cards.add(tmpCards.get(r));
tmpCards.remove(r);
size = tmpCards.size();
}
logger.info("[ShuffleCards][shuffle] cardSize ="+size);

int tableDeckCnt = cards.size() - HANDCARDSCOUNT * shufflePlayer;
Card[] tableDeck = new Card[tableDeckCnt];
logger.info("[ShuffleCards][shuffle] tableDeckCnt ="+tableDeckCnt);

//发牌
Card[] arrCards = cards.toArray(new Card[cards.size()]);

Card[][] finalDeck = new Card[shufflePlayer+1][];
int index = 0;
while (index < shufflePlayer) {
Card[] playerADeck = new Card[HANDCARDSCOUNT];
System.arraycopy(arrCards, HANDCARDSCOUNT*index, playerADeck, 0, HANDCARDSCOUNT);
finalDeck[index] = playerADeck;
index ++;
}

System.arraycopy(arrCards, HANDCARDSCOUNT*index, tableDeck, 0, tableDeckCnt);
finalDeck[index] = tableDeck;
logger.info("[ShuffleCards][shuffle] finalDeck="+new Gson().toJson(finalDeck));

return finalDeck;
}
/**
* 将发牌配置数组化
* @param config
* @return
*/
public int[] initShuffleConfig(ShuffleConfig config) {
int[] szConfig = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
if(config.isShuffleWan())
szConfig[CardRule.ECARDCOLOR_WAN] = 1;
if(config.isShuffleTong())
szConfig[CardRule.ECARDCOLOR_TONG] = 1;
if(config.isShuffleTiao())
szConfig[CardRule.ECARDCOLOR_TIAO] = 1;
if(config.isShuffleDong())
szConfig[CardRule.ECARDCOLOR_DONG] = 1;
if(config.isShuffleNan())
szConfig[CardRule.ECARDCOLOR_NAN] = 1;
if(config.isShuffleXi())
szConfig[CardRule.ECARDCOLOR_XI] = 1;
if(config.isShuffleBei())
szConfig[CardRule.ECARDCOLOR_BEI] = 1;
if(config.isShuffleZhong())
szConfig[CardRule.ECARDCOLOR_ZHONG] = 1;
if(config.isShuffleFa())
szConfig[CardRule.ECARDCOLOR_FA] = 1;
if(config.isShuffleBai())
szConfig[CardRule.ECARDCOLOR_BAI] = 1;
return szConfig;
}

public List<Card> initCards(int[] shuffleConfig) {
List<Card> cards = new ArrayList<>();
for(int colorIndex=CardRule.ECARDCOLOR_WAN; colorIndex<=CardRule.ECARDCOLOR_TIAO; colorIndex++) {
if(shuffleConfig[colorIndex] == 0)
continue;
for(int valueIndex=CardRule.EMINCARD; valueIndex<=CardRule.EMAXCARD; valueIndex++) {
Card c = this.allCards[colorIndex*4*9+(valueIndex-1)*4];
for(int count=0; count<CardRule.ECARDNUMBER; count++) {
int r = cards.isEmpty() ? 0 : (new Random().nextInt(cards.size()));
cards.add(r, c);
}
}
}

int beginIndex = 9*3*4;
for(int colorIndex=CardRule.ECARDCOLOR_FENG_BEGIN; colorIndex<=CardRule.ECARDCOLOR_JIAN_END; colorIndex++) {
if(shuffleConfig[colorIndex] == 0)
continue;
Card c = this.allCards[beginIndex+(colorIndex-CardRule.ECARDCOLOR_FENG_BEGIN)*4];
for(int count=0; count<CardRule.ECARDNUMBER; count++) {
int r = cards.isEmpty() ? 0 : (new Random().nextInt(cards.size()));
cards.add(r, c);
}
}
return cards;
}
posted on 2019-07-26 16:11  jeolyli  阅读(296)  评论(0编辑  收藏  举报