发扑克牌
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;

public class Fu{
    private final int NUM=5;
    private String[] types = {"♠","♥","♣","♦"};
    private String[] values = {"8","9","10","J","Q","K","A"};
    private List<String> cards=new ArrayList<>();
    private Map<String, Set<String>> map=new HashMap<String, Set<String>>();
    
    private void init() {
        for (String value : values) {
             for (String type : types) {
                cards.add(value+type);
            }
        }
        Collections.shuffle(cards);
    }
    private Set<String> getSet() {
        Comparator<String> comp=new Comparator<String>() {
            public int compare(String o1, String o2) {
                int num1=getSize(o1.charAt(0));
                int num2=getSize(o2.charAt(0));
                if (num1>num2) {
                    return 1;
                } else if(num1<num2){
                    return -1;
                }else{
                    int index=1;
                    if (num1==10){
                        index=2;
                    }
                    int type1=getType(o1.charAt(index));
                    int type2=getType(o2.charAt(index));
                    return type1>type2?1:-1;
                }
            }
        };
        Set<String> set=new TreeSet<>(comp);
        return set;
    }
    private int getSize(char num) {
        switch (num) {
        case '8':
            return 8;
        case '9':
            return 9;
        case 'J':
            return 11;
        case 'Q':
            return 12;
        case 'K':
            return 13;
        case 'A':
            return 14;
        default:
            return 10;
        }
    }
    private int getType(char type) {
        switch (type) {
        case '♠':
            return 1;
        case '♥':
            return 2;
        case '♣':
            return 3;
        default:
            return 4;
        }
    }
    private void everyPlayer() {
        Set<String> set=map.keySet();
        for (String name : set) {
            Set<String> everyCards=map.get(name);
            for (int i = 0; i < 5; i++) {
                everyCards.add(cards.get(0));
                cards.remove(0);       
            }
        }
    }
    private void initPlayer(String... names) {     
        if (names.length>NUM||names.length<3) {
            System.exit(0);
        }
        for (String name : names) {
            map.put(name, getSet());
        }  
    }
    private void display() {
        for (Entry<String, Set<String>> string : map.entrySet()) {
            System.out.println(string.getKey()+"  "+string.getValue());
        }
    }
    public static void main(String[] args) {
        Fu show=new Fu();
        show.initPlayer("张三","李四","王五");
        show.init();
        show.everyPlayer();
        show.display();
    }
}

相关文章: