* 业务需求
* 斗地主的做牌,发牌,排序,看牌
* 业务,54张牌
* 点数:"3","4","5","6","7","8","9","10","J","Q","K","A","2"
* 花色:"♠","♥","♣","♦"
* 大小王:"👲"(大王),"👨🌾"(小王)
* 点数风暴要组合4种花色,大小王各一张
* 斗地主:发51张牌,剩下3张做底牌
* 功能:
* 1,做牌
* 2,洗牌
* 3,定义3个玩家
* 4,发牌
* 5,排序
* 6,看牌
* 面向对象设计案例
* a,定义一个牌类,代表牌对象
* b,定义一个集合存储54张牌,集合只需要1个
public class DemoGame {/*a,顶一个静态集合,代表牌对象,存储54张牌,集合只需要一个*/public static final List<Card> ALL_CARDS = new ArrayList<>();/*b,做牌*/static{//1,定义一个数组存储牌的点数,类型确定,个数确定请用数组存储String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};//2,定义一个数组存储牌的花色,类型确定,个数确定存入到集合中去String[] colors = {"♠","♥","♣","♦"};//3,先遍历点数与4种花色组中成牌对象存入到集合中去int index = 0;for (String number : numbers) {//遍历花色for (String color : colors) {Card card = new Card(number,color,index++);ALL_CARDS.add(card);}}//4,单独加入大小王Collections.addAll(ALL_CARDS, new Card("","👲",index++),new Card("","👨🌾",index++));System.out.println("输出新牌 :"+ALL_CARDS);}public static void main(String[] args) {/*c,洗牌*/Collections.shuffle(ALL_CARDS);System.out.println("洗牌后 :"+ALL_CARDS);/*d,定义3个玩家*/ArrayList<Card> ZhangSan = new ArrayList<>();ArrayList<Card> LiSi = new ArrayList<>();ArrayList<Card> WangWu = new ArrayList<>();/*e,发牌*/for (int i = 0; i < ALL_CARDS.size() - 3; i++) {//得到当前这张牌对象Card c = ALL_CARDS.get(i);//判断这个牌发给谁if (i % 3 == 0){ZhangSan.add(c);}else if (i % 3 == 1){LiSi.add(c);}else if (i % 3 == 2){WangWu.add(c);}}/*f,对牌进行排序*/sortCard(ZhangSan);sortCard(LiSi);sortCard(WangWu);/*g,看牌*/System.out.println("张三 :"+ZhangSan);System.out.println("李四 :"+LiSi);System.out.println("王五 :"+WangWu);//截取最后三张牌做底牌List<Card> lastThreeCards = ALL_CARDS.subList(ALL_CARDS.size() - 3, ALL_CARDS.size());System.out.println("底牌 :"+lastThreeCards);}/*对牌的List集合进行排序*/private static void sortCard(List<Card> cards) {Collections.sort(cards, new Comparator<Card>() {@Overridepublic int compare(Card o1, Card o2) {return o2.getIndex()- o1.getIndex();}});}}
public class Card{private String number;private String color;private int index;//存储牌在新牌中的索引public Card() {}public Card(String number, String color, int index) {this.number = number;this.color = color;this.index = index;}public Card(String number, String color) {this.number = number;this.color = color;}public int getIndex() {return index;}public void setIndex(int index) {this.index = index;}public String getNumber() {return number;}public void setNumber(String number) {this.number = number;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}@Overridepublic String toString() {return number+color;}
}