package cn.itcast.demo18; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; /* map<Integer,String> 大王 小王 2 A K Q J 10 9 8 7 6 5 4 3 0 1 2-5 6-9 10-13 14-17 18-21 22-25 26-29 30-33 34-37 38-41 42-45 46-49 50-53 */ public class Test { public static void main(String[] args) { Map<Integer,String> map = new HashMap<Integer,String>(); //用于存放牌 List<Integer> list = new ArrayList<Integer>(); //用于存放洗完牌后牌的顺序 List<Integer> user1 = new ArrayList<Integer>(); //用于存放每个用户的牌 List<Integer> user2 = new ArrayList<Integer>(); List<Integer> user3 = new ArrayList<Integer>(); List<Integer> dipai = new ArrayList<Integer>(); //洗牌 xiPai(map,list); System.out.println("洗完牌之后为:"); for(int k:list){ System.out.print(map.get(k)+" "); } System.out.println(); //发牌 faPai(map,list,user1,user2,user3,dipai); //看牌 showPai(map,user1,user2,user3,dipai); } public static void xiPai(Map<Integer,String> map,List<Integer> list){ //首先存放54张牌 String[] number = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"}; String[] huaSe = {"♥","♠","♦","♣"}; map.put(0, "大王"); map.put(1, "小王"); int i = 2; for(String num:number){ for(String hs:huaSe){ map.put(i++, hs+num); } } System.out.println(map.size()); //然后洗牌 for(int j = 0 ;j<54 ; j++){ list.add(j); } Collections.shuffle(list); } public static void faPai(Map<Integer,String> map,List<Integer> list ,List<Integer> user1,List<Integer> user2,List<Integer> user3,List<Integer> dipai){ for(int index = 0 ;index<list.size()-3;){ //17*3 = 51 0-50 user1.add(list.get(index)); index++; user2.add(list.get(index)); index++; user3.add(list.get(index)); index++; } for(Integer e :list.subList(list.size()-3, list.size())){ //51 52 53 dipai.add(e); } System.out.println("发牌的结果为:"); for(Integer i:user1){ System.out.print(map.get(i)+" "); } System.out.println(); for(Integer i:user2){ System.out.print(map.get(i)+" "); } System.out.println(); for(Integer i:user3){ System.out.print(map.get(i)+" "); } System.out.println(); for(Integer i:dipai){ System.out.print(map.get(i)+" "); } System.out.println(); } public static void showPai(Map<Integer,String> map,List<Integer> user1,List<Integer> user2,List<Integer> user3,List<Integer> dipai){ Collections.sort(user1); Collections.sort(user2); Collections.sort(user3); Collections.sort(dipai); System.out.println("每个人的牌为:"); for(Integer i:user1){ System.out.print(map.get(i)+" "); } System.out.println(); for(Integer i:user2){ System.out.print(map.get(i)+" "); } System.out.println(); for(Integer i:user3){ System.out.print(map.get(i)+" "); } System.out.println(); for(Integer i:dipai){ System.out.print(map.get(i)+" "); } System.out.println(); } } |