Java基础集合案例一【斗地主】

package demo;

import java.util.*;

public class CardGame {

    ArrayList<String> poker = new ArrayList<>();
    List<String> pokerBottom = List.of("大王", "小王");
    List<String> pokerColors = List.of("♥", "♣", "♠", "♦");
    List<String> pokerNumbers = List.of("2", "3", "4", "5", "6", "7", "8", "9","10", "J", "Q", "K", "A");
    HashMap<String, List<String>> userHashMap = new HashMap<>();


    //得到一副完整的牌
    public List<String> getPoker() {
        for (String s : pokerColor) {
            for (String s1 : pokerValue) {
                poker.add(s + s1);
            }
        }
        poker.addAll(pokerBottom);
        return poker;
    }

    //洗牌
    public List<String> shuffle(List<String> list) {
        Collections.shuffle(list);
        return list;
    }

    //底牌
    public List<String> getBottomCards(List<String> list) {
        Random r = new Random();
        int i1 = r.nextInt(54);
        int i2 = r.nextInt(53);
        int i3 = r.nextInt(52);
        List<String> bottomCards = List.of(list.get(i1), list.get(i2), list.get(i3));
        poker.removeAll(bottomCards);
        return bottomCards;
    }

    //发牌
    public HashMap<String, List<String>> deal(String str1, String str2, String str3) {
        ArrayList<String> arrayList1 = new ArrayList<>();
        ArrayList<String> arrayList2 = new ArrayList<>();
        ArrayList<String> arrayList3 = new ArrayList<>();
        for (int i = 0; i < poker.size(); i++) {
            if (i % 3 == 0) {
                arrayList1.add(poker.get(i));
                userHashMap.put(str1, arrayList1);
            } else if (i % 3 == 1) {
                arrayList2.add(poker.get(i));
                userHashMap.put(str2, arrayList2);
            } else {
                arrayList3.add(poker.get(i));
                userHashMap.put(str3, arrayList3);
            }

        }

        //对牌进行排序
        arrayList1.sort((s1, s2) -> s2.toCharArray()[1] - s1.toCharArray()[1]);
        arrayList2.sort((s1, s2) -> s2.toCharArray()[1] - s1.toCharArray()[1]);
        arrayList3.sort((s1, s2) -> s2.toCharArray()[1] - s1.toCharArray()[1]);
        return userHashMap;
    }

    public static void main(String[] args) {
        CardGame cardGame = new CardGame();
        List<String> poker = cardGame.getPoker();
        List<String> shuffle = cardGame.shuffle(poker);
        List<String> bottomCards = cardGame.getBottomCards(shuffle);
        System.out.println(bottomCards);
        HashMap<String, List<String>> deal = cardGame.deal("张三", "李四", "王五");
        System.out.println(deal);
    }
}
posted @ 2020-06-05 11:42  幻竹  阅读(173)  评论(0)    收藏  举报