斗地主扑克发牌算法

核心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 mAllCards = new ArrayList<>();
    private List mTypeList = Arrays.asList(mType);
    private List 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 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> divide(int playerSize) {
        List> result = new ArrayList<>();
        List list = new ArrayList<>();
        for (int i = 1; i <= mPackCount; i++) {
            list.addAll(getAllCards());
        }
        //抽取底牌逻辑
        List coverCardList = pickRandomCard(list, mCoverCardSize);
        result.add(coverCardList);

        //分牌逻辑
        int eachSize = list.size() / playerSize;
        for (int i = 1; i <= playerSize - 1; i++) {
            List playerCardList = pickRandomCard(list, eachSize);
            result.add(playerCardList);
        }
        //添加剩余的
        sort(list);
        result.add(list);
        return result;
    }

    @NonNull
    private List pickRandomCard(List totalList, int pickCardSize) {
        List 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 list) {
        Collections.sort(list, new Comparator() {
            @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;
            }
        });
    }
}

转载于:https://my.oschina.net/u/1175512/blog/995575

你可能感兴趣的:(java,移动开发)