斗地主2.0

package day13;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public class DouDIZhu {
    public static void main(String[] args) {

        //准备牌
        // 创建Map集合,存储牌的索引和组装好的牌
        HashMap<Integer, String> poker = new HashMap<>();
        // 创建一个list 集合,存储牌的索引
        ArrayList<Integer> pokerIndex = new ArrayList<>();
        // 定义2个集合,存储牌花色,和序号
        List<String> colors = List.of("♠", "♥", "♣", "♦");
        List<String> numbers = List.of("2", "A", "k", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
        // 把大小王存储到Map集合中
        int index = 0;
        poker.put(index, "大王");
        pokerIndex.add(index);
        index++;
        poker.put(index, "小王");
        pokerIndex.add(index);
        index++;
        //组装牌
        for (String color : colors) {
            for (String number : numbers) {
                poker.put(index, (color + number));
                pokerIndex.add(index);
                index++;
            }
        }
        // 洗牌
        Collections.shuffle(pokerIndex);
        //发牌,定义4个集合分别存储玩家1,2,3和底牌的牌
        ArrayList<Integer> poker1 = new ArrayList<>();
        ArrayList<Integer> poker2 = new ArrayList<>();
        ArrayList<Integer> poker3 = new ArrayList<>();
        ArrayList<Integer> dipa = new ArrayList<>();
        //遍历集合发牌
        for (int i = 0; i < pokerIndex.size(); i++) {
            if (i >= 51) {
                // 发给底牌
                dipa.add(pokerIndex.get(i));
                //根据条件不同发牌
            } else if (i % 3 == 0) {
                poker1.add(pokerIndex.get(i));
            } else if (i % 3 == 1) {
                poker2.add(pokerIndex.get(i));

            } else if (i % 3 == 2) {
                poker3.add(pokerIndex.get(i));
            }

        }
        //排序
        Collections.sort(poker1);
        Collections.sort(poker2);
        Collections.sort(poker3);
        Collections.sort(dipa);
        // 调用看牌方法
        look(poker, poker1);
        look(poker, poker2);
        look(poker, poker3);
        look(poker, dipa);
    }

    // 看牌
    public static void look(HashMap<Integer, String> porker, ArrayList<Integer> list) {
        for (Integer key : list) {
            System.out.print(porker.get(key));
        }
        System.out.println("");

    }
}

猜你喜欢

转载自www.cnblogs.com/wurengen/p/10658378.html
今日推荐