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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗