模拟斗地主(集合的综合练习)

package com.liushuaishuai;

import java.util.ArrayList;

import java.util.Collections;

public class PokerDemo {
    public static void main(String[] args) {
        //创建一个牌盒
        ArrayList<String> array = new ArrayList<String>();
        /*
        四种颜色从2.。。。。。A
        小王、大王
         */
        //定义花色数组
        String[] colors = {"♣", "♠", "♥", "♦"};
        //定义字母数组
        String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
        //将字母和数字拼接后再添加到array中去
        for (String color : colors) {
            for (String number : numbers) {
                array.add(color + number);
            }
        }
        //添加小王大王
        array.add("小王");
        array.add("大王");
        //System.out.println(array);


        //洗牌也就是把牌打散,用Collection中的shuffle()方法来进行洗牌
        Collections.shuffle(array);
        System.out.println(array);
        System.out.println("-----------------------");

        //发牌也就是遍历集合给三个玩家发牌
        //定义四个集合
        ArrayList<String> linqingxia = new ArrayList<String>();
        ArrayList<String> fengqingyang = new ArrayList<String>();
        ArrayList<String> liuyifei = new ArrayList<String>();
        ArrayList<String> dparray = new ArrayList<String>();

        //给集合添加牌
        for (int i = 0; i < array.size(); i++) {
            String poker = array.get(i);

            if (i >= array.size() - 3) {
                dparray.add(poker);
            } else if (i % 3 == 0) {
                linqingxia.add(poker);
            } else if (i % 3 == 1) {
                fengqingyang.add(poker);
            } else if (i % 3 == 2) {
                liuyifei.add(poker);
            }
        }
        //看牌,调用下面写的方法
        lookPoker("周润发",linqingxia);
        lookPoker("张家辉",fengqingyang);
        lookPoker("刘德华",liuyifei);
        lookPoker("底牌",dparray);






    }
    //看牌的方法
    public static void lookPoker(String name,ArrayList<String> array){
        System.out.print(name+"的牌是:");
        for(String poker: array) {
            System.out.print(poker+" ");

        }
        System.out.println();
    }
}

 运行结果:

C:jdk12.0.2injava.exe -Didea.launcher.port=61762 "-Didea.launcher.bin.path=C:Program FilesJetBrainsIntelliJ IDEA 2018.2.2in" -Dfile.encoding=UTF-8 -classpath "C:Users86132IdeaProjectsjavaSE_CodeoutproductionIDEA_Test;C:Program FilesJetBrainsIntelliJ IDEA 2018.2.2libidea_rt.jar" com.intellij.rt.execution.application.AppMainV2 com.liushuaishuai.PokerDemo
[♥6, ♠3, ♦2, ♦7, ♠9, ♥K, ♥3, ♥A, ♥10, ♥5, ♠J, ♦K, ♣8, ♥7, ♣6, ♥8, ♣5, ♣9, ♣2, ♥Q, ♠Q, ♣7, ♣J, ♦3, ♣10, ♦8, ♠K, ♠8, ♦9, ♣Q, ♣A, ♠6, ♦6, ♦5, ♦Q, ♠A, ♠10, ♠5, ♦A, ♠4, ♥2, ♥J, ♦4, 小王, ♣3, ♦J, ♠7, ♣K, ♦10, ♣4, ♥9, ♥4, 大王, ♠2]
-----------------------
周润发的牌是:♥6 ♦7 ♥3 ♥5 ♣8 ♥8 ♣2 ♣7 ♣10 ♠8 ♣A ♦5 ♠10 ♠4 ♦4 ♦J ♦10 
张家辉的牌是:♠3 ♠9 ♥A ♠J ♥7 ♣5 ♥Q ♣J ♦8 ♦9 ♠6 ♦Q ♠5 ♥2 小王 ♠7 ♣4 
刘德华的牌是:♦2 ♥K ♥10 ♦K ♣6 ♣9 ♠Q ♦3 ♠K ♣Q ♦6 ♠A ♦A ♥J ♣3 ♣K ♥9 
底牌的牌是:♥4 大王 ♠2 

Process finished with exit code 0

 升级版

package com.liushuaishuai;

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

public class pokerDemo01 {
    public static void main(String[] args) {
        //创建HashMap,键是编号,值是牌
        HashMap<Integer, String> hm = new HashMap<Integer, String>();
        //创建ArrayList,存储编号
        ArrayList<Integer> array = 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 color : colors) {
            for (String number : numbers) {
                String poker = color + number;
                hm.put(index, poker);
                array.add(index);
                index++;
            }
        }
        hm.put(index, "小王");
        array.add(index);
        index++;
        hm.put(index, "大王");
        array.add(index);

        //洗牌
        Collections.shuffle(array);

        //发牌
//        先定义treeSet集合接收
        TreeSet<Integer> fage = new TreeSet<Integer>();
        TreeSet<Integer> zhazhahui = new TreeSet<Integer>();
        TreeSet<Integer> huazai = new TreeSet<Integer>();
        TreeSet<Integer> dp = new TreeSet<Integer>();

        for (int i = 0; i < array.size(); i++) {
            Integer x = array.get(i);
            if (i >= array.size() - 3) {
                dp.add(x);
            } else if (i % 3 == 0) {
                fage.add(x);
            } else if (i % 3 == 1) {
                zhazhahui.add(x);
            } else if (i % 3 == 2) {
                huazai.add(x);
            }
        }

        //看牌方法调用
        lookPoker("发哥", fage, hm);
        lookPoker("渣渣辉", zhazhahui, hm);
        lookPoker("华仔", huazai, hm);
        lookPoker("底牌",dp,hm);


    }


    public static void lookPoker(String name, TreeSet<Integer> tr, HashMap<Integer, String> hm) {
        System.out.print(name + "的牌是");
        for (Integer key : tr) {
            String value = hm.get(key);
            System.out.print(value + " ");
        }
        System.out.println();
    }
}
原文地址:https://www.cnblogs.com/lsswudi/p/11417274.html