package com.mlt.d4_collection_test; import java.util.*; /** * 斗地主游戏 */ public class Test1 { //定义一个静态集合来存放54张牌 public static List<Card> list =new ArrayList<>(); static { String[] sizes={"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; String[] colors={"♥","♠","♦","♣"}; int index=0; for (String size : sizes) { index++; for (String color : colors) { Card c =new Card(size,color,index); list.add(c); } } //大小王 Card c1 =new Card("","?",++index); Card c2 =new Card("","?",++index); Collections.addAll(list,c1,c2); System.out.println(list); } public static void main(String[] args) { //1.洗牌 Collections.shuffle(list); System.out.println(list); //2.定义三个玩家 List<Card> xm =new ArrayList<>(); List<Card> xh =new ArrayList<>(); List<Card> xz =new ArrayList<>(); //3.发牌 for (int i = 0; i < list.size(); i++) { Card c =list.get(i); if (i % 3==0){ xm.add(c); } else if (i % 3==1) { xh.add(c); } else if (i % 3 ==2) { xz.add(c); } } List<Card> lastThreecadrs=list.subList(list.size()-3,list.size()); //4.排序 paixu(xm); paixu(xh); paixu(xz); //大音 System.out.println("小明"+xm); System.out.println("小红"+xh); System.out.println("小张"+xz); System.out.println(lastThreecadrs); } private static void paixu(List<Card> size) { Collections.sort(size,( o1, o2)-> o2.getIndex()-o1.getIndex()); } }
//Card类
package com.mlt.d4_collection_test; public class Card { private String size; private String color; public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } private int index; public Card(){ } public Card(String size, String color,int index) { this.size = size; this.color = color; this.index = index; } public String getSize() { return size; } public void setSize(String size) { this.size = size; } @Override public String toString() { return size+color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } }