Java Se Simulation Fighting Landlords【Practice using collections】

Simulated Landlord

Purpose: Practice using collections

Ideas:

  • A total of 54 cards are divided into four suits, so you can get 52 cards in a loop
  • HashMap: used to store all the cards, the key is Integer type, and the value is String type
  • ArrayList: used to save the index
  • TreeSet: used to save players and hole cards (it can be sorted naturally and looks neat)
  • Shuffle: through the shuffle method in Collections (shuffle the collection)
  • Finally find the player's card in the map by index
  1. Define two arrays to store suit and card size respectively
  2. Create a HashMap collection to store all the cards
  3. Create an ArrayList to store the index of the card
  4. Use nested loops to generate 52 cards, and add the cards and index to the Map collection, and the index to the ArrayList collection
  5. Adding the king and the king to this card is ready to be completed
  6. Shuffle the array index through the shuffle method of Collections
  7. Create four TreeSet collections to represent the three players and the hole cards
  8. Start to deal, traverse the arraylist collection through the ordinary for loop, and deal with the cards by judgment
  9. Write the card reading method by traversing the index in the treeset collection, and looking up the card in the map through the index
    10. Call the card reading method

Show running results:
operation result

package cn.dreamyi.demo1.doudizhu;

import java.util.*;

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

        String[] colors = {
    
    "♦", "♥", "♣", "♠"};
        String[] numbers = {
    
    "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};

        //创建map集合保存所有牌
        HashMap<Integer, String> hm = new HashMap<>();
        ArrayList<Integer> al = new ArrayList<>();//保存牌的索引

        int index = 0;
        for (String color : colors) {
    
    
            for (String number : numbers) {
    
    
                al.add(index);
                hm.put(index, color + number);
                index++;
            }
        }
        hm.put(index, "小王");
        al.add(index);
        index++;
        hm.put(index, "大王");
        al.add(index);

        //洗牌 打乱顺序
        Collections.shuffle(al);

        //创建三个玩家和底牌集合
        TreeSet<Integer> play1 = new TreeSet<>();
        TreeSet<Integer> play2 = new TreeSet<>();
        TreeSet<Integer> play3 = new TreeSet<>();
        TreeSet<Integer> dp = new TreeSet<>();

        //开始发牌
        for (int i = 0; i < al.size(); i++) {
    
    
            if (i >= al.size() - 3) {
    
    
                dp.add(al.get(i));
            } else {
    
    
                if (i % 3 == 0) {
    
    
                    play1.add(al.get(i));
                } else if (i % 3 == 1) {
    
    
                    play2.add(al.get(i));
                } else if (i % 3 == 2) {
    
    
                    play3.add(al.get(i));
                }
            }
        }
        //看牌
        lookPoker("张三",play1,hm);
        lookPoker("李四",play2,hm);
        lookPoker("王五",play3,hm);
        lookPoker("底牌",dp,hm);
    }

    /**
     * 定义发看牌方法
     * @param play 玩家名字
     * @param list 玩家的索引
     * @param map 洗过后的牌
     */
    public static void lookPoker(String play,TreeSet<Integer> list,Map<Integer,String> map){
    
    
        System.out.println(play+"的牌:");
        Set<Map.Entry<Integer, String>> entries = map.entrySet();
        for(Integer i:list){
    
    
            System.out.print(map.get(i)+" ");
        }
        System.out.println();
    }
}

Public Account : Start from Jane [mmk_xgg]
study together
Follow and learn together

Guess you like

Origin blog.csdn.net/weixin_42143994/article/details/114028169