Simple realization of the case of Doudizhu

introduction

Recently I learned java-related knowledge, and I learned the case of the landlord by Chuanzhi teacher. It feels a bit interesting. Here Xiao Yuan decided to study the case and make some related notes. Not much nonsense, we started to get into the topic.

Case requirements

The existing player1, player1, and player1 need to finish poker. Our operation is to issue 54 cards to these three players. In this process, we need to go through:
prepare cards -> shuffle cards -> deal cards -> watch four cards A process, we need to use collective knowledge to achieve this process.

Option One

Use a single column set to implement this case

Knowledge framework

The integration system inherited by arrayList is as follows:
Insert picture description here
The list interface and set interface are both derived from the collection
Insert picture description here
case. The basic idea is to use an ArrayList collection, a large collection to store all the pokers, and then traverse the pokers to add them to the collections collected by different players at once. , When there are only three cards left in the poker, do the hole cards, don't talk nonsense, and offer the code.

Code

public class DouDiZhuDemo {
    
    
    public static void main(String[] args) {
    
    
        Collection collection = readyPoker();
        collection=shufflePoker(collection);
        ArrayList<String> dipai=new ArrayList<>();
        ArrayList<String> [] player=new ArrayList[4];
        for (int i = 0; i < player.length; i++) {
    
    
            player[i]=new ArrayList<>();
        }
        distributePoker(collection,dipai,player);
        lookPoker(dipai,player);
    }
    //准备牌
    public static Collection readyPoker(){
    
    
        ArrayList<String> poker = new ArrayList<>();
        // 静态定义数组
        String [] number= {
    
    "2","A","K","Q","J","10","9","8","7","6","5","4","3"};
        //动态定义数组,复习一下动态定义数组的方法,可以参考一下和C/C++定义数组有什么区别
        String [] color = new String[4];
        color[0]="♠";
        color[1]="♥";
        color[2]="♣";
        color[3]="♦";
        for (int i = 0; i < number.length; i++) {
    
    
            for (int i1 = 0; i1 < color.length; i1++) {
    
    
                poker.add(number[i]+color[i1]);
            }
        }
        poker.add("大王");
        poker.add("小王");
        System.out.println("开始准备poker");
        System.out.println(poker);
        return poker;
    }

    //洗牌
    public static Collection shufflePoker(Collection poker){
    
    
        Collections.shuffle((List<?>) poker);
        return poker;
    }

    //发牌
    public static void distributePoker(Collection poker,ArrayList dipai, ArrayList... player){
    
    
        ArrayList<?> poker1 = (ArrayList<?>) poker;
        String p=null;

        for (int i = 0; i < poker1.size(); i++) {
    
    
            p= (String) poker1.get(i);
            if(i>50){
    
    
                dipai.add(p);
            }else {
    
    
                for (int i1 = 0; i1 < player.length; i1++) {
    
    
                    if(i%player.length==i1){
    
    
                        player[i1].add(p);
                        break;
                    }
                }
            }
        }
    }
	//看牌
    public static void lookPoker(ArrayList dipai,ArrayList... player){
    
    
        System.out.println("-------------------------");
        System.out.println("dipai"+dipai);
        System.out.println("-----------------------");
        for (int i = 0; i < player.length; i++) {
    
    
            System.out.println("player"+i+"pokeris:"+player[i]);
        }
    }
}

Assume that there are four people in the figure, and the little ape uses variable parameter parameter transmission. The advantage of this is that the number of people can be variable and it has higher flexibility.
Let's take a look at the result first:
Insert picture description here
the disadvantage of the above case is that the pokers are not arranged in order when looking at the cards, so you need to consider using other two-column sets to implement this case.

Option II

Using double-column set to realize the case of landlord

Knowledge framework

Map is a top-level interface in Java, and the common inheritance system for changing interfaces is shown in the figure below. Insert picture description here
What Xiao Ape should focus on is the application of hashmap under Map. Use hashMap to implement the above cases. Not much nonsense, please provide code

Code

public class DouDiZhuDemo {
    
    
    public static void main(String[] args) {
    
    
        Collection<Integer> pokerIndex=new ArrayList<>();
        Map<Integer, String> poker= new HashMap<>();
        readyPoker(pokerIndex,poker);
       // System.out.println(pokerIndex);
      //  System.out.println(poker);
        shufflePoker(pokerIndex);
        System.out.println(pokerIndex);
      //  System.out.println(poker.size());

        Map<String, List> disPoker = new HashMap<>();
        disPoker.put("赵丽颖",new ArrayList());
        disPoker.put("杨幂",new ArrayList());
        disPoker.put("陈乔恩",new ArrayList());
        disPoker.put("杨紫",new ArrayList());
        disPoker.put("底牌",new ArrayList());
        disPoker.put("张天爱",new ArrayList());
        disPoker.put("甘露露",new ArrayList());
        try {
    
    
            disPoker=distributePoker(pokerIndex,disPoker,poker);
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
        lookPoker(disPoker);

    }

    //准备牌
    public static void readyPoker(Collection<Integer> pokerIndex, Map<Integer, String> poker){
    
    
        //定义洗好的poker索引

        //定义poker
        System.out.println("------------------准备poker-------------------------");

        // 定义牌的数字
        List<String> numbers = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
       // String [] number= {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};

        //动态定义数组,复习一下动态定义数组的方法,可以参考一下和C/C++定义数组有什么区别
        /*String [] color = new String[4];
        color[0]="♠";
        color[1]="♥";
        color[2]="♣";
        color[3]="♦";*/

        //定义牌的花色
        List<String> colors = List.of("♠", "♥", "♣", "♦");

        int index=0;
        pokerIndex.add(index);
        poker.put(index,"大王");
        index++;
        pokerIndex.add(index);
        poker.put(index,"小王");
        index++;

        for (String number : numbers) {
    
    
            for (String color : colors) {
    
    
                pokerIndex.add(index);
                poker.put(index,number+color);
                index++;
            }
        }
       // System.out.println(pokerIndex);
       System.out.println(poker);
        //return poker;
    }

    //洗牌
    public static void shufflePoker(Collection poker){
    
    
        System.out.println("----------开始洗牌-----------");
        Collections.shuffle((List<?>) poker);
        //return poker;
    }

    //发牌
    /*public static void distributePoker(Collection poker,ArrayList dipai, ArrayList... player){
        ArrayList<?> poker1 = (ArrayList<?>) poker;
        String p=null;

        for (int i = 0; i < poker1.size(); i++) {
            p= (String) poker1.get(i);
            if(i>50){
                dipai.add(p);
            }else {
                for (int i1 = 0; i1 < player.length; i1++) {
                    if(i%player.length==i1){
                        player[i1].add(p);
                        break;
                    }
                }
            }
        }
    }*/

    public static Map distributePoker(Collection pokerIndex,Map<String,List> disPoker,Map<Integer,String> poker ) throws Exception {
    
    
        System.out.println("-------------------------开始发牌----------------------------");
        Map<String,List> newDisPoker = new HashMap<>();

        for (int i = 0; i < pokerIndex.size(); i++) {
    
    
            if(i>50){
    
    
                if(disPoker.containsKey("底牌")){
    
    
                    List dipai = disPoker.get("底牌");
                    dipai.add(((List)pokerIndex).get(i));
                }else {
    
    
                    throw new Exception("没有底牌");
                }
            }else {
    
    
                int size = disPoker.size()-1;
                int in=0;
                for (String key :
                        disPoker.keySet()) {
    
    
                    if(key.equals("底牌")){
    
    
                        continue;
                    }
                    if(i%size==in){
    
    
                        disPoker.get(key).add(((List)pokerIndex).get(i));
                        break;
                    }
                    in++;
                }
            }
        }

        for (String name:disPoker.keySet()
        ) {
    
    
            Collections.sort(disPoker.get(name), new Comparator<Integer>() {
    
    
                @Override
                public int compare(Integer o1, Integer o2) {
    
    
                    return o2-o1;
                }
            });
        }

        //重新组装牌
        for (String name:disPoker.keySet()
             ) {
    
    
            List<String> tempList= new ArrayList<>();
            for (Object o : disPoker.get(name)) {
    
    
                tempList.add(o+":"+poker.get(o));
            }
            newDisPoker.put(name,tempList);
            //tempList.clear();
        }

        //disPoker.clear();
        return  newDisPoker;
    }

    //看牌
    /*public static void lookPoker(ArrayList dipai,ArrayList... player){
        System.out.println("-------------------------");
        System.out.println("dipai"+dipai);
        System.out.println("-----------------------");
        for (int i = 0; i < player.length; i++) {
            System.out.println("player"+i+"pokeris:"+player[i]);
        }
    }*/
    public static void lookPoker(Map<String,List> disPoker){
    
    
        System.out.println("-------------------------------准备看牌----------------------------");

        for (Map.Entry<String,List> entry :
                disPoker.entrySet()) {
    
    
            System.out.println(entry.getKey()+"::"+entry.getValue()+"共"+entry.getValue().size()+"::张牌");

        }
        System.out.println("---------------------------看牌结束-------------------------------");
    }

}

It is worth mentioning here that the of method of List, which can add fixed-length elements in batches, and the length of the List does not change after adding, that is, List.of(element...) is suitable for adding fixed-length elements, and adopts this method The function has requirements for the jdk version, that is, jdk9.0.4 is recommended. The execution result of the above code is:
Insert picture description here

to sum up

Through rehearsing the case of fighting the landlord, the little ape has reviewed the relevant knowledge about the collection and the picture. The ancients said: My day is three times, the little ape needs to reflect on himself and work harder.

Guess you like

Origin blog.csdn.net/xueshanfeitian/article/details/106441395