Java 纸牌游戏 牛牛 逻辑代码 实现

源代码链接
package yan.study.game.puke;

import cn.yan.study.utils.YanStrUtils;
import cn.yan.study.utils.cache.ConcurrentHashMapCacheUtils;

import java.util.*;

public class NiuNiuGame {

    /**
     * 获取一手牌的牛
     */
    public static NiuResult getNiuResult(List personCards) {
        /**
         * 先把大于等于10的拿出去
         * 再找三个加起来等于10的倍数的
         * 再找两个加起来等于10的倍数的
         * 最后算牛几
         */
        NiuResult result = new NiuResult();
        List moreThanNine = new ArrayList();
        List lessThanTen = new ArrayList();
        Card heitao9 = new Card(Card.POKER_COLOR_LIST.get(0), Card.POKER_VALUE_LIST.get(5));
        Card fanpianA = new Card(Card.POKER_COLOR_LIST.get(3), Card.POKER_VALUE_LIST.get(0));
        for (Card card : personCards) {
            if (card.compareTo(heitao9) > 0 && card.compareTo(fanpianA) < 0) {
                moreThanNine.add(card);
            } else {
                lessThanTen.add(card);
            }
        }

        Integer niuNumber = getNiuNumber(lessThanTen);
        result.setNiuNumber(niuNumber);

        List sortCards = new ArrayList();
        Collections.sort(moreThanNine, Collections.reverseOrder());
        sortCards.addAll(moreThanNine);

        Collections.sort(lessThanTen, Collections.reverseOrder());
        List aList = new ArrayList(); // 记录A的牌
        for (Card card : lessThanTen) {
            if (card.getValue().equals(Card.POKER_VALUE_LIST.get(0))) {
                aList.add(card);
            }
        }
        if (aList.size()>0) {
            lessThanTen.removeAll(aList);
            lessThanTen.addAll(aList);
        }

        sortCards.addAll(lessThanTen);
        result.setCardSortList(sortCards);

        return result;
    }


    /**
     * 获取牛的数字
     */
    private static int getNiuNumber(List lessThanTen) {
        if (lessThanTen.size() == 0) {
            return 10;
        }
        if (lessThanTen.size() == 1) {
            return getOneCardNum(lessThanTen.get(0));
        }
        if (lessThanTen.size() == 2) {
            return getNiuFrom2(lessThanTen);
        }
        if (lessThanTen.size() == 3) {
            return getNiuFrom3(lessThanTen);
        }
        if (lessThanTen.size() == 4) {
            return getNiuFrom4(lessThanTen);
        }
        if (lessThanTen.size() == 5) {
            return getNiuFrom5(lessThanTen);
        }
        return -1;

    }

    /**
     * 从两张牌里面找牛
     */
    private static int getNiuFrom2(List lessThanTen) {
        if (lessThanTen.size() == 2) {
            int niuNumber = (getOneCardNum(lessThanTen.get(0)) + getOneCardNum(lessThanTen.get(1))) % 10;
            if (niuNumber == 0) {
                return 10;
            }
            return niuNumber;

        } else {
            return -1;
        }
    }

    /**
     * 从三张里面找牛
     */
    private static int getNiuFrom3(List lessThanTen) {
        if (lessThanTen.size() == 3) {
            // 3
            int niuNumber = (getOneCardNum(lessThanTen.get(0)) + getOneCardNum(lessThanTen.get(1)) + getOneCardNum(lessThanTen.get(2))) % 10;
            if (niuNumber == 0) {
                return 10;
            }

            // 2+1
            List> subListCards2 = YanCollectionUtils.getAllSubList(lessThanTen, 2);
            for (List listCard : subListCards2) {
                niuNumber = getNiuFrom2(listCard);
                if (niuNumber == 10) {
                    List subList = YanCollectionUtils.getSubList(lessThanTen, listCard);
                    return getOneCardNum(subList.get(0));
                }
            }

            return -1;

        } else {
            return -1;
        }
    }

    /**
     * 4账牌里面找牛  3 + 1 2 + 2
     */
    private static int getNiuFrom4(List lessThanTen) {
        if (lessThanTen.size() == 4) {
            // 3 + 1
            List> subListCards3 = YanCollectionUtils.getAllSubList(lessThanTen, 3);
            for (List listCard : subListCards3) {
                int niuNumber = getNiuFrom3(listCard);
                if (niuNumber == 10) {
                    Card card = YanCollectionUtils.getSubList(lessThanTen, listCard).get(0);
                    return getOneCardNum(card);
                }
            }

            // 2 + 2
            List> subListCards2 = YanCollectionUtils.getAllSubList(lessThanTen, 2);
            for (List listCard : subListCards2) {
                int niuNumber = getNiuFrom2(listCard);
                if (niuNumber == 10) {
                    List subList = YanCollectionUtils.getSubList(lessThanTen, listCard);
                    return getNiuFrom2(subList);
                }
            }

            return -1;
        } else {
            return -1;
        }
    }

    /**
     * 从五张里面找牛 3+2 2+2+1
     */
    private static int getNiuFrom5(List lessThanTen) {
        if (lessThanTen.size() == 5) {
            // 3+2
            List> subListCards3 = YanCollectionUtils.getAllSubList(lessThanTen, 3);
            for (List listCard : subListCards3) {
                int niuNumber = getNiuFrom3(listCard);
                if (niuNumber == 10) {
                    List subList = YanCollectionUtils.getSubList(lessThanTen, listCard);
                    return getNiuFrom2(subList);
                }
            }

            // 2+2+1
            List> subListCards2 = YanCollectionUtils.getAllSubList(lessThanTen, 2);
            for (List listCard : subListCards2) {
                int niuNumber = getNiuFrom2(listCard);
                if (niuNumber == 10) {
                    List subList = YanCollectionUtils.getSubList(lessThanTen, listCard);
                    niuNumber = getNiuFrom2(subList);
                    if (niuNumber == 10) {
                        Card card = YanCollectionUtils.getSubList(lessThanTen, listCard).get(0);
                        return getOneCardNum(card);
                    }
                }
            }
            return -1;
        } else {
            return -1;
        }
    }

    /**
     * 取一张的值
     */
    private static int getOneCardNum(Card card) {
        if (card.getValue().equals(Card.POKER_VALUE_LIST.get(0))) {
            return 1;
        }
        return Integer.valueOf(card.getValue());
    }

    /**
     * 创建游戏
     */
    public static String createGame(int count, List names) {

        if (count > names.size()) {
            throw new RuntimeException("params is error");
        }

        String gameUUid = YanStrUtils.getYanString("niuniuGame");
        List gamePersonList = new ArrayList();
        for (int i = 0; i< count; i++) {
            Person person = new Person(names.get(i));
            gamePersonList.add(person);
        }

        ConcurrentHashMapCacheUtils.setCache(gameUUid, gamePersonList);
        return gameUUid;
    }


    /**
     * 一次发完所有的牌
     */
    public static void sendCard(String gameUUid) {
        List pokers = PokerUtils.getNewPokers();
        pokers = PokerUtils.deleteCard(Arrays.asList(new Card(Card.POKER_COLOR_LIST.get(1), Card.MAX_KING), new Card(Card.POKER_COLOR_LIST.get(0), Card.SMALL_KING)), pokers);
        PokerUtils.Shuffle(pokers);
        List gamePersonList = (List) ConcurrentHashMapCacheUtils.getCache(gameUUid);
        int personCount = gamePersonList.size();
        for (int cardCount = 0; cardCount < 5; cardCount++) {
            for (int index = 0; index < personCount; index++) {
                List cardList = gamePersonList.get(index).getCardList();
                cardList.add(pokers.get(0));
                pokers.remove(0);
            }
        }
        ConcurrentHashMapCacheUtils.setCache(gameUUid, gamePersonList);
    }

    public static void showResult(String gameUUid) {
        List gamePersonList = (List) ConcurrentHashMapCacheUtils.getCache(gameUUid);
        // TODO 排序失败 以后有时间再玩
        Collections.sort(gamePersonList, new Comparator() {
            public int compare(Person o1, Person o2) {
                List personOneCards = o1.getCardList();
                List personTwoCards = o2.getCardList();

                NiuResult oneResult = getNiuResult(personOneCards);
                NiuResult twoResult = getNiuResult(personTwoCards);

                return oneResult.compareTo(twoResult);
            }
        });

        Person winPerson = gamePersonList.get(0);
        System.out.println("this is winner ...");
        showPerson(winPerson);


        System.out.println("all person info is ....");
        for (int index =0; index < gamePersonList.size(); index++) {
            Person person = gamePersonList.get(index);
            showPerson(person);
        }
    }


    public static void showPerson(Person person) {
        System.out.println("person name is : " + person.getName());
        System.out.println("card is :");
        for (Card card : person.getCardList()) {
            System.out.println(card);
        }
        System.out.println("result is : " + getNiuResult(person.getCardList()));
    }



    public static void main(String[] args) {

        String gameUUid = createGame(4, Arrays.asList("yan", "test", "root", "system"));
        sendCard(gameUUid);
        showResult(gameUUid);
    }

    private static class NiuResult implements Comparable {
        /**
         * 牛的数目
         */
        private Integer niuNumber;
        /**
         * 有序手牌
         */
        private List cardSortList;

        public Integer getNiuNumber() {
            return niuNumber;
        }

        public void setNiuNumber(Integer niuNumber) {
            this.niuNumber = niuNumber;
        }

        public List getCardSortList() {
            return cardSortList;
        }

        public void setCardSortList(List cardSortList) {
            this.cardSortList = cardSortList;
        }

        /**
         * 先判断牛的大小 在判断牌的大小 最后比最大的牌的花色
         * @param o
         * @return
         */
        public int compareTo(Object o) {
            if (!(o instanceof NiuResult)) {
                return -1;
            }
            NiuResult niuResult = (NiuResult)o;

            if (this.getNiuNumber() != niuResult.getNiuNumber()) {
                return niuResult.getNiuNumber() - this.getNiuNumber();
            }

            return PokerUtils.commCompareTo(niuResult.getCardSortList(), this.getCardSortList());
        }

        @Override
        public String toString() {
            return "NiuResult{" +
                    "niuNumber=" + niuNumber +
                    ", cardSortList=" + cardSortList +
                    '}';
        }
    }
}

你可能感兴趣的:(java,学习)