本文主要是介绍斗地主的发牌实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. 将牌按序号存入HanshMap对象中
2.用Collections中的方法shuffle来将牌的序号打乱
3.利用for循环进行发牌
4.展示各成员发的牌
import java.util.*;public class Main {public static void main(String[] args) {Map<Integer,String> map = new HashMap();//将牌存入mapString[] str1 ={"♦","❤","♠","♣"};String[] str2 = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};int index = 1;for(int i=0;i< str1.length;i++){for(int j=0;j<str2.length;j++){map.put(index++,str1[i]+str2[j]);}}map.put(53,"大王");map.put(54,"小王");//打乱牌的顺序,只需打乱编号顺序即可,编号为1-54List<Integer> list = new ArrayList<>();for(int i=1;i<55;i++){list.add(i);}Collections.shuffle(list); //利用Collections工具类进行顺序打乱//发牌ArrayList<String> member1 = new ArrayList<>();ArrayList<String> member2 = new ArrayList<>();ArrayList<String> member3 = new ArrayList<>();ArrayList<String> tail = new ArrayList<>();for(int i=0;i<54;i++){if(i>50){tail.add(map.get(list.get(i)));}else{if(i%3==1){member1.add(map.get(list.get(i)));}else if(i%3==2){member2.add(map.get(list.get(i)));}else{member3.add(map.get(list.get(i)));}}}//看牌showTablet(member1,"农民");showTablet(member2,"农民");showTablet(member3,"地主");showTablet(tail,"底牌");}public static void showTablet(ArrayList<String> list,String name){System.out.print(name+":");for(String l:list ){System.out.print(l+" ");}System.out.println();}
}
这篇关于斗地主的发牌实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!