java实现斗地主发牌

/**
 * @author shuai1.liu
 * @create 2018-09-21 13:36
 * @desc
 **/
package cn.com.callback;


import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

public class Main {

    public static void main (String [] args){
        System.out.println(System.currentTimeMillis());
        List> meiPaiPeopleList = initPeople(); //未发完牌的人
        List> youPaiPeopleList = new ArrayList<>(); //有牌的人的集合
        //初始化总牌数
        List> pukeList = initPuke();

        Random random = new Random();
        for (int i = 0; i < 51; i++) {
            int who = random.nextInt(meiPaiPeopleList.size());//发给谁
            int hang = random.nextInt(pukeList.size());  //发哪种花色的牌
            int lie = random.nextInt(pukeList.get(hang).size());  // 发花色的几号牌

            List people = meiPaiPeopleList.get(who);
            List huase = pukeList.get(hang);
            people.add(huase.get(lie));
            huase.remove(lie);

            if (people.size() == 17){
                youPaiPeopleList.add(people);
                meiPaiPeopleList.remove(people);
            }
            if (huase.size() == 0){
                pukeList.remove(huase);
            }
        }
        System.out.println(System.currentTimeMillis());
        for (int i = 0; i < youPaiPeopleList.size(); i++) {
            List list =  youPaiPeopleList.get(i);
            list.sort(new Comparator() {
                @Override
                public int compare(String o1, String o2) {
                    int i1 = Integer.valueOf(o1);
                    int i2 = Integer.valueOf(o2);
                    if (i1 > i2){
                        return 0;
                    }else {
                        return -1;
                    }

                }
            });
            for (int j = 0; j < list.size(); j++) {
                System.out.print(list.get(j)+",");
            }
            System.out.println();

        }

        for (int i = 0; i < pukeList.size(); i++) {
            List list =  pukeList.get(i);
            for (int j = 0; j < list.size(); j++) {
                String s =  list.get(j);
                System.out.print(s+",");
            }
            System.out.println();
        }

        System.out.println(System.currentTimeMillis());
    }

    /**
     * 初始化所有人
     * @return
     */
    public static List> initPeople(){
        List> allPeopleList = new ArrayList<>();
        List zhangsan = new ArrayList<>();
        List lisi = new ArrayList<>();
        List wangwu = new ArrayList<>();
        allPeopleList.add(zhangsan);
        allPeopleList.add(lisi);
        allPeopleList.add(wangwu);
        return allPeopleList;
    }

    /**
     * 初始化总牌数
     * @return
     */
    public static List> initPuke(){
        List> pukeList = new ArrayList<>();
        List heitao = new ArrayList<>();
        List hongtao = new ArrayList<>();
        List meihua = new ArrayList<>();
        List fangpian = new ArrayList<>();
        for (int i = 0; i < 14; i++) {
            String aaa = i+"";
            heitao.add(aaa);
            hongtao.add(aaa);
            meihua.add(aaa);
            fangpian.add(aaa);
        }
        meihua.remove(13);
        fangpian.remove(13);
        pukeList.add(heitao);
        pukeList.add(hongtao);
        pukeList.add(meihua);
        pukeList.add(fangpian);
        return pukeList;
    }
}

你可能感兴趣的:(Java面试)