实现斗地主洗牌、发牌、看牌
import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; public class FightAgainstLandlord { public static void main(String[] args) { //准备牌 HashMap<Integer, String> poker = new HashMap<>(); ArrayList<Integer> pokerIndex = new ArrayList<>(); List<String> colors = List.of("♠", "♥", "♣", "♦"); List<String> numbers = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"); int index=0; pokerIndex.add(index); poker.put(index,"大王"); index++; pokerIndex.add(index); poker.put(index,"小王"); index++; for(String n:numbers){ for(String c:colors){ pokerIndex.add(index); poker.put(index,c+n); index++; } } System.out.print(poker); System.out.println(); //洗牌 Collections.shuffle(pokerIndex); System.out.println(pokerIndex); //发牌 ArrayList<Integer> player01=new ArrayList<>(); ArrayList<Integer> player02=new ArrayList<>(); ArrayList<Integer> player03=new ArrayList<>(); ArrayList<Integer> diPai=new ArrayList<>(); for(int i=0;i<pokerIndex.size();i++){ if(i>=51){ diPai.add(pokerIndex.get(i)); }else if(i%3==0){ player01.add(pokerIndex.get(i)); }else if(i%3==1){ player02.add(pokerIndex.get(i)); }else if(i%3==2){ player03.add(pokerIndex.get(i)); } } Collections.sort(player01); Collections.sort(player02); Collections.sort(player03); Collections.sort(diPai); /* System.out.println(diPai); System.out.println(player01); System.out.println(player02); System.out.println(player03); */ //看牌 lookPoker("player01",poker,player01); lookPoker("player02",poker,player02); lookPoker("player03",poker,player03); lookPoker("diPai",poker,diPai); } public static void lookPoker(String name,HashMap<Integer,String> poker, ArrayList<Integer> list){ System.out.println(name+":"); for(Integer k:list){ String v=poker.get(k); System.out.print(v+" "); } System.out.println(); } }