import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; public class ArrayList3 { public static void main(String[] args) { ArrayList<String> color = new ArrayList<String>(); color.add("♠"); color.add("♥"); color.add("♦"); color.add("♣"); String[] number = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; HashMap<Integer, String>map = new HashMap<Integer, String>(); int index=0; for (String thisNumber : number) { for (String thisColor : color) { map.put(index++, thisColor+thisNumber); } } map.put(index++, "小王"); map.put(index++, "大王"); ArrayList<Integer>cards = new ArrayList<Integer>(); for (int i = 0; i<= 53; i++) { cards.add(i); } Collections.shuffle(cards); ArrayList<Integer>player1Pokers1 = new ArrayList<Integer>(); ArrayList<Integer>player1Pokers2 = new ArrayList<Integer>(); ArrayList<Integer>player1Pokers3 = new ArrayList<Integer>(); ArrayList<Integer>pokers = new ArrayList<Integer>(); //发牌 for(int i=0;i<cards.size();i++){ if(i>=51){ pokers.add(cards.get(i)); }else{ if(i%3==0){ player1Pokers1.add(cards.get(i)); } else if(i%3==1){ player1Pokers2.add(cards.get(i)); } else if(i%3==2){ player1Pokers3.add(cards.get(i)); } } } Collections.sort(player1Pokers1); Collections.sort(player1Pokers2); Collections.sort(player1Pokers3); Collections.sort(pokers); ArrayList<String>Player1 = new ArrayList<String>(); ArrayList<String>Player2 = new ArrayList<String>(); ArrayList<String>Player3 = new ArrayList<String>(); ArrayList<String>Cards = new ArrayList<String>(); for(Integer key : player1Pokers1){ Player1.add(map.get(key)); } for(Integer key : player1Pokers2){ Player2.add(map.get(key)); } for(Integer key : player1Pokers3){ Player3.add(map.get(key)); } for(Integer key : pokers){ Cards.add(map.get(key)); } System.out.println("甲:"+Player1); System.out.println("乙:"+Player2); System.out.println("丙:"+Player3); System.out.println("底牌:"+Cards); } }
Java编程实现模拟斗地主洗牌发牌
最新推荐文章于 2022-04-19 19:32:27 发布