模拟斗地主洗牌发牌

package com.imooc;


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

/**
 * @auther cen
 * @date 2019/5/20 17:15
 */
public class Poker {
    //斗地主洗牌发牌
    public static void main (String[] args){
        //准备花色
        ArrayList<String> color = new ArrayList<>();
        color.add("♠");
        color.add("♥");
        color.add("♦");
        color.add("♣");
        //准备数字
        ArrayList<String> number = new ArrayList<String>();
        Collections.addAll(number,"3","4","5","6","7","8","9","10","J","Q","K","A","2");
        //定义一个map集合,用来将数字与每一张牌来结合
        Map<Integer,String> map = new HashMap<Integer, String>();
        int index = 0;
        for(int i=0;i<number.size();i++){
            String thisNumber = number.get(i);
            for(int j=0;j<color.size();j++){
                String thisColor = color.get(j);
                map.put(index++,thisColor+thisNumber);
            }
        }

        //加入大小王
        map.put(index++,"大王");
        map.put(index++,"小王");
        System.out.println(map);

        //一副54张的牌  索引0-53
        ArrayList<Integer> cards = new ArrayList<Integer>();
        for(int i=0;i<54;i++){
            cards.add(i);

        }
        //洗牌
        Collections.shuffle(cards);
        System.out.println(cards);

        //创建三个玩家和底牌
        ArrayList<Integer> iplayer = new ArrayList<Integer>();
        ArrayList<Integer> iplayer2 = new ArrayList<Integer>();
        ArrayList<Integer> iplayer3 = new ArrayList<Integer>();
        ArrayList<Integer> itCards = new ArrayList<Integer>();
        //遍历已经洗好的牌,发到三个玩家和底牌中
        for(int i = 0;i<cards.size();i++){
            if(i>=51){
                itCards.add(cards.get(i));
            }else{
                if(i%3==0){
                    iplayer.add(cards.get(i));
                }else if(i%3==1){
                    iplayer2.add(cards.get(i));
                }else{
                    iplayer3.add(cards.get(i));
                }
            }
        }

        //对每个人手中的牌进行排序
        Collections.sort(iplayer);
        Collections.sort(iplayer2);
        Collections.sort(iplayer3);

        //对应数字形式的牌 ,定义字符串形式的牌

        ArrayList<String> splayer = new ArrayList<String>();
        ArrayList<String> splayer2 = new ArrayList<String>();
        ArrayList<String> splayer3 = new ArrayList<String>();
        ArrayList<String> scords = new ArrayList<String>();

        for(int i=0;i<iplayer.size();i++){
            Integer key = iplayer.get(i);
            splayer.add(map.get(key));
        }
        for(int i=0;i<iplayer2.size();i++){
            Integer key = iplayer2.get(i);
            splayer2.add(map.get(key));
        }
        for(int i=0;i<iplayer3.size();i++){
            Integer key = iplayer3.get(i);
            splayer3.add(map.get(key));
        }
        for(int i=0;i<itCards.size();i++){
            Integer key = itCards.get(i);
            scords.add(map.get(key));
        }

        //看牌
        System.out.println(splayer);
        System.out.println(splayer2);
        System.out.println(splayer3);
        System.out.println(scords);



    }
}

你可能感兴趣的:(面试,map集合)