Java基础:斗地主发牌案例Map版(有序版)

    技术2025-01-28  51

    public class DouDiZhu2 { public static void main(String[] args) { // Map储存牌的索引和组装好的牌 HashMap<Integer, String> poker = new HashMap<>(); // List储存牌的索引 ArrayList<Integer> pokerIndex = new ArrayList<>(); // 定义两个集合储存花色、牌号 List<String> colors = List.of("♠", "♥", "♣", "♦"); List<String> nums = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"); // 把大王小王先放进去 int index = 0; poker.put(index, "大王"); pokerIndex.add(index); index++; poker.put(index, "小王"); pokerIndex.add(index); index++; // 把剩下的牌组装并存储 for (String num : nums) { for (String color : colors) { poker.put(index, color + num); pokerIndex.add(index); index++; } } //System.out.println(poker); //System.out.println(pokerIndex); // 洗牌 Collections.shuffle(pokerIndex); // 发牌 ArrayList<Integer> player1 = new ArrayList<>(); ArrayList<Integer> player2 = new ArrayList<>(); ArrayList<Integer> player3 = new ArrayList<>(); ArrayList<Integer> diPai = new ArrayList<>(); for (Integer i : pokerIndex) { Integer integer = pokerIndex.get(i); if (i >= 51) { // 给底牌 diPai.add(integer); } else if (i % 3 == 0) { player1.add(integer); } else if (i % 3 == 1) { player2.add(integer); } else if (i % 3 == 2) { player3.add(integer); } } // 排序 Collections.sort(player1); Collections.sort(player2); Collections.sort(player3); Collections.sort(diPai); // 看牌 look("卢本伟", poker, player1); look("阿姨", poker, player2); look("我", poker, player3); look("底牌", poker, diPai); } /* * 定义一个看牌的方法 * */ public static void look(String name, HashMap<Integer, String> poker, ArrayList<Integer> list) { // 输出 System.out.print(name + ": "); for (Integer key : list) { String value = poker.get(key); System.out.print(value + " "); } System.out.println();//打印一位玩家后换行 } } /* 卢本伟: 大王 ♣2 ♠A ♥A ♣A ♦K ♥10 ♣10 ♠8 ♣8 ♥7 ♦7 ♠6 ♠5 ♠4 ♣4 ♥3 阿姨: ♠2 ♦A ♠Q ♣Q ♠J ♥J ♣J ♦J ♠9 ♣9 ♥8 ♦8 ♥6 ♥5 ♣5 ♦5 ♦4 我: 小王 ♥2 ♦2 ♥K ♣K ♥Q ♦Q ♠10 ♦10 ♠7 ♣7 ♣6 ♦6 ♥4 ♠3 ♣3 ♦3 底牌: ♠K ♥9 ♦9 */
    Processed: 0.018, SQL: 9