本文主要是介绍模拟斗地主洗牌和发牌并且排序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
思路:
public class test {//写一个方法看牌public static void lookup(String name,TreeSet<Integer> ts,HashMap<Integer,String> hm){System.out.print(name+"的牌是:");for(Integer key:ts){String value=hm.get(key);System.out.print(value+" ");}System.out.println();}public static void lookup(String name,ArrayList<String> s){System.out.print(name+"的牌是:");for(String ss:s){System.out.print(ss+" ");}System.out.println();}public static void main(String[] args) {//创建一个HashMap集合HashMap<Integer,String> hm=new HashMap<Integer,String>();//创建一个ArrayList集合ArrayList<Integer> array=new ArrayList<Integer>();//创建花色数组String[] colors={"♠","♥","♣","◆"};//定义一个点数组String[] numbers={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};//从0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号即可。int index=0;for(String s:numbers){for(String c:colors){String poker=c.concat(s);hm.put(index, poker);array.add(index);index++;}}hm.put(index, "小王");array.add(index);index++;hm.put(index, "大王");array.add(index);//洗牌(洗的是编号)Collections.shuffle(array);//发牌,发的是编号,为了保证编号的唯一性,就创建TreeSet集合接收TreeSet<Integer> player1=new TreeSet<Integer>();TreeSet<Integer> player2=new TreeSet<Integer>();TreeSet<Integer> player3=new TreeSet<Integer>();TreeSet<Integer> dipai=new TreeSet<Integer>();for(int x=0;x<array.size();x++){if(x>=array.size()-3){dipai.add(array.get(x));}else if(x%3==0){player1.add(array.get(x));}else if(x%3==1){player2.add(array.get(x));}else if(x%3==2){player3.add(array.get(x));}}//看牌(遍历TreeSet集合,获取编号,到HashMap集合找到对应的牌)lookup("张三",player1,hm);lookup("李四",player2,hm);lookup("王五",player3,hm);lookup("底牌",dipai,hm);}}
这篇关于模拟斗地主洗牌和发牌并且排序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!