案例:模拟斗地主及升级版

public class PokerDemo {
    public static void main(String[] args) {
        //创建一个牌盒,也就是定义一个集合对象,用ArrayList集合实现
        ArrayList<String> array = new ArrayList<String>();

        //往牌盒里面装牌
        //定义花色数组
        String[] colors = {"♦", "♣", "♥", "♠"};

        //定义点数数组
        String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
        for (String color : colors) {
            for (String number : numbers) {
                array.add(color + number);
            }
        }
        array.add("小王");
        array.add("大王");

        //洗牌,把牌打散,用Collections的shuffle()方法实现
        Collections.shuffle(array);

        //发牌,也就是遍历集合,给三个玩家玩牌
        //首先定义三个玩家的牌集合
        ArrayList<String> player1 = new ArrayList<String>();
        ArrayList<String> player2 = new ArrayList<String>();
        ArrayList<String> player3 = new ArrayList<String>();
        //定义三张底牌集合
        ArrayList<String> dipai = new ArrayList<String>();

        for (int i = 0; i < array.size(); i++) {
            String poker = array.get(i);
            //保留三张底牌,放入底牌集合
            if (i >= array.size() - 3) {
                dipai.add(poker);
            } else if (i % 3 == 0) {  //对3取余 可以得到余数 0,1,2 对应三位玩家
                player1.add(poker);
            } else if (i % 3 == 1) {
                player2.add(poker);
            } else if (i % 3 == 2) {
                player3.add(poker);
            }
        }
        //看牌,也就是三个玩家分别遍历自己的牌
        LookPoker("玩家1",player1);
        LookPoker("玩家2",player2);
        LookPoker("玩家3",player3);
        LookPoker("底牌",dipai);
    }
    //定义看牌方法
    public static void LookPoker(String name, ArrayList<String> player){
        System.out.print(name+"的牌是:");
        for (String poker : player){
            System.out.print(poker+" ");
        }
        System.out.println();
    }
}

运行结果:

 

public class PokerDemo {
    public static void main(String[] args) {
        //创建HashMap,键是编号,值是牌
        HashMap<Integer, String> hm = new HashMap<Integer, String>();

        //创建ArrayList,存储编号
        ArrayList<Integer> arrayList = new ArrayList<Integer>();

        //创建花色数组和点数数组
        String[] colors = {"♦", "♣", "♥", "♠"};
        String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};

        //从0开始往HashMap里面存储编号,并存储对应的牌。同时往ArrayList里面存储编号
        int index = 0;
        //先数字在花色,不同花同点数按索引排序
        for (String number : numbers) {
            for (String color : colors) {
                hm.put(index, color + number);
                arrayList.add(index);
                index++;
            }
        }
        hm.put(index, "小王");
        arrayList.add(index);
        index++;
        hm.put(index, "大王");
        arrayList.add(index);

        //洗牌(洗的是编号),用Collections的shuffle()方法实现
        Collections.shuffle(arrayList);

        //发牌(发的也是编号,为了保证编号是排序的,创建TreeSet集合接收)
        TreeSet<Integer> player1 = new TreeSet<Integer>();
        TreeSet<Integer> player2 = new TreeSet<Integer>();
        TreeSet<Integer> player3 = new TreeSet<Integer>();
        TreeSet<Integer> dpSet = new TreeSet<Integer>();

        for (int i = 0; i < arrayList.size(); i++) {
            Integer integer = arrayList.get(i);
            if (i >= arrayList.size() - 3) {
                dpSet.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);
            }
        }
        //调用看牌方法
        lookPoker("玩家1",player1,hm);
        lookPoker("玩家2",player2,hm);
        lookPoker("玩家3",player3,hm);
        lookPoker("底牌",dpSet,hm);
    }
    //定义方法看牌
    public static void lookPoker(String name,TreeSet<Integer> ts,HashMap<Integer,String> hm){
        System.out.print(name + "的牌是:");
        for (Integer key : ts){
            String poker = hm.get(key); //根据编号(键),获取牌(值)
            System.out.print(poker+" ");
        }
        System.out.println();
    }
}

运行结果:

 

猜你喜欢

转载自www.cnblogs.com/pxy-1999/p/12691726.html
今日推荐