核心class, 可以设置多少副牌,底牌数量及玩家数量
import android.support.annotation.NonNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
/**
* Created by jin on 22/06/2017.
*/
public class PokerCore {
// ♠ > ♥ > ♦ > ♣
// 2 > A > K > Q > J > 10 > 9 > 8 > 7 > 6 > 5 > 4 > 3
// sj(small joker) bj(big joker)
private String[] mType = new String[]{"♠", "♥", "♦", "♣" };
private String[] mNormalCard = new String[]{"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3" };
private String[] mExtraCard = new String[]{"sj", "bj" };
private List<String> mAllCards = new ArrayList<>();
private List<String> mTypeList = Arrays.asList(mType);
private List<String> mNormalCardList = Arrays.asList(mNormalCard);
private int mPackCount;
private int mCoverCardSize;
/**
* 设置多少副牌,至少一副
*
* @param packCount 多少副牌
* @param coverCardSize 多少张底牌
* @return
*/
public PokerCore(int packCount, int coverCardSize) {
this.mPackCount = packCount;
if (mPackCount < 1) {
mPackCount = 1;
}
this.mCoverCardSize = coverCardSize;
}
/**
* 动态创建54张牌
*
* @return
*/
private synchronized List<String> getAllCards() {
if (mAllCards == null || mAllCards.size() == 0) {
mAllCards.addAll(Arrays.asList(mExtraCard));
for (String type : mType) {
for (String card : mNormalCard) {
mAllCards.add(type + card);
}
}
}
return mAllCards;
}
/**
* 根据player人数分牌
*
* @param playerSize
* @return 返回数组第一个是底牌数组
*/
public List<List<String>> divide(int playerSize) {
List<List<String>> result = new ArrayList<>();
List<String> list = new ArrayList<>();
for (int i = 1; i <= mPackCount; i++) {
list.addAll(getAllCards());
}
//抽取底牌逻辑
List<String> coverCardList = pickRandomCard(list, mCoverCardSize);
result.add(coverCardList);
//分牌逻辑
int eachSize = list.size() / playerSize;
for (int i = 1; i <= playerSize - 1; i++) {
List<String> playerCardList = pickRandomCard(list, eachSize);
result.add(playerCardList);
}
//添加剩余的
sort(list);
result.add(list);
return result;
}
@NonNull
private List<String> pickRandomCard(List<String> totalList, int pickCardSize) {
List<String> randomList = new ArrayList<>();
for (int i = 1; i <= pickCardSize; i++) {
Random random = new Random();
int index = random.nextInt(totalList.size());
//Log.i("test", "get : " + index);
randomList.add(totalList.remove(index));
}
sort(randomList);
return randomList;
}
/**
* 按照花色大小排序
*
* @param list
*/
private void sort(List<String> list) {
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
//大小王情况
if (o1.startsWith("b") || o1.startsWith("s") && !o2.startsWith("b")) {
return -1;
}
if (o2.startsWith("b") || (o2.startsWith("s") && !o1.startsWith("b"))) {
return 1;
}
//普通情况
// A > K > Q > J > 10 > 9 > 8 > 7 > 6 > 5 > 4 > 3 > 2
// ♠ > ♥ > ♦ > ♣
try {
String type1 = String.valueOf(o1.charAt(0));
String card1 = o1.substring(1, o1.length());
String type2 = String.valueOf(o2.charAt(0));
String card2 = o2.substring(1, o2.length());
//先比较数字
int index1 = mNormalCardList.indexOf(card1);
int index2 = mNormalCardList.indexOf(card2);
if (index1 < index2) {
return -1;
} else if (index1 > index2) {
return 1;
} else {
//再比花色
index1 = mTypeList.indexOf(type1);
index2 = mTypeList.indexOf(type2);
if (index1 < index2) {
return -1;
} else if (index1 > index2) {
return 1;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
});
}
}