java~斗地主发牌、看牌案例

**第一种=======List实现**

public class Main {

    public static void main(String[] args) {

        ArrayList<String> poker=new ArrayList<>();

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

        //组装扑克牌
        for (String i:card) {
            for (String j:color ) {
                poker.add(j+i);
            }
        }
        poker.add("大王");
        poker.add("小王");

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

        ArrayList<String> per1=new ArrayList<>();//玩家1手牌
        ArrayList<String> per2=new ArrayList<>();//玩家2手牌
        ArrayList<String> per3=new ArrayList<>();//玩家3手牌
        ArrayList<String> per4=new ArrayList<>();//底牌

        //发牌
        for(int i=0;i<poker.size();i++){
            if(i>50){
                per4.add(poker.get(i));
            }else if(i<17){
                per1.add(poker.get(i));
            }else if(i>=17 && i<34){
                per2.add(poker.get(i));
            }else if(i>=34 && i<51){
                per3.add(poker.get(i));
            }
        }

        System.out.println(per1+"==玩家1");
        System.out.println(per2+"==玩家2");
        System.out.println(per3+"==玩家3");
        System.out.println(per4+"==底牌");

    }
}

**第二种=======List+Map实现(手牌从小到大排序)**

public class Main {

    public static void main(String[] args) {


        String color[]={"♥","♠","♣","♦"};

        String card[]={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};

        Map<Integer,String> poker=new HashMap<>();//存储扑克牌

        ArrayList<Integer> pokerindex=new ArrayList<>();//扑克牌索引

        int index=0;

        poker.put(index,"大王");
        pokerindex.add(index);
        index++;
        poker.put(index,"小王");
        pokerindex.add(index);

        //组装扑克牌
        for (String cardI: card) {
            for (String colorI:color ) {
                index++;
                poker.put(index,colorI+cardI);
                pokerindex.add(index);
            }
        }

        //打乱扑克牌索引
        Collections.shuffle(pokerindex);


        ArrayList<Integer> play1=new ArrayList<>();
        ArrayList<Integer> play2=new ArrayList<>();
        ArrayList<Integer> play3=new ArrayList<>();
        ArrayList<Integer> deep=new ArrayList<>();

        //为玩家发放扑克牌以及生成底牌
        for(int i=0;i<pokerindex.size();i++){
            if(i>50){
                deep.add(pokerindex.get(i));
            }else if(i%3==0){
                play1.add(pokerindex.get(i));
            }else if(i%3==1){
                play2.add(pokerindex.get(i));
            }else if(i%3==2){
                play3.add(pokerindex.get(i));
            }
        }

        System.out.println("玩家1→"+play1);
        System.out.println("玩家2→"+play2);
        System.out.println("玩家3→"+play3);
        System.out.println("底牌→"+deep);

        //将玩家手牌进行排序
        Collections.sort(play1);
        Collections.sort(play2);
        Collections.sort(play3);
        Collections.sort(deep);

        //查看排序后的手牌
        viewCard(play1,poker);
        System.out.println("==============");
        viewCard(play2,poker);
        System.out.println("==============");
        viewCard(play3,poker);
        System.out.println("==============");
        viewCard(deep,poker);

    }

    public static void viewCard(ArrayList<Integer> list,Map<Integer,String> poker){
        for (Integer key:list) {
            System.out.println(poker.get(key));
        }
    }

}

你可能感兴趣的:(java)