斗地主集合案例
1 public class Card { 2 private String size; 3 private String color; 4 /** 5 * 用index来表示大小方便排序 6 * 3♣, 3♠, 3❤, 3◇, 4♣, 4♠, 4❤, 4◇, 5♣, 5♠, 5❤, 5◇, 6♣, 6♠, 6❤, 6◇, 7 * 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 8 */ 9 private int index; 10 11 public Card() { 12 } 13 14 public Card(String size, String color, int index) { 15 this.size = size; 16 this.color = color; 17 this.index = index; 18 } 19 20 public String getSize() { 21 return size; 22 } 23 24 public void setSize(String size) { 25 this.size = size; 26 } 27 28 public String getColor() { 29 return color; 30 } 31 32 public void setColor(String color) { 33 this.color = color; 34 } 35 36 public int getIndex() { 37 return index; 38 } 39 40 public void setIndex(int index) { 41 this.index = index; 42 } 43 44 @Override 45 public String toString() { 46 return size + color; 47 } 48 }
1 import java.util.ArrayList; 2 import java.util.Collections; 3 import java.util.Comparator; 4 import java.util.List; 5 6 public class GameDemo1 { 7 /** 8 * 1. 定义一个静态的集合存储54张牌对象 9 * @param args 10 */ 11 public static List<Card> allCards = new ArrayList<>(); 12 13 /** 14 * 2. 做牌:定义静态代码块初始化牌数据 15 * @param args 16 */ 17 static { 18 // 3. 定义点数,使用数组 19 String[] sizes = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" , "A", "2"}; 20 // 4. 定义花色 21 String[] colors = {"♣", "♠", "❤", "◇"}; 22 // 5. 组合点数和花色 23 //记录牌的大小 24 int index = 0; 25 for (String size : sizes) { 26 index++; 27 for (String color : colors) { 28 // 6. 封装 29 Card c = new Card(size, color, index); 30 // 7. 存入到集合容器中去 31 allCards.add(c); 32 } 33 } 34 35 // 8. 大小王存入到集合对象中去 36 Card da = new Card("", "d", ++index); 37 Card xiao = new Card("", "x", ++index); 38 Collections.addAll(allCards, da, xiao); 39 System.out.println("新牌: " + allCards); 40 41 } 42 43 public static void main(String[] args) { 44 45 // 9. 洗牌 46 Collections.shuffle(allCards); 47 System.out.println("洗牌后: " + allCards); 48 49 // 10. 发牌(定义三个玩家, 每个玩家的牌也是一个集合容器) 50 List<Card> wl = new ArrayList<>(); 51 List<Card> phx = new ArrayList<>(); 52 List<Card> ai = new ArrayList<>(); 53 54 // 11. 开始发牌,从牌集合中发出51张牌给三个玩家,剩余三张作为底牌 55 // 2◇, Q◇, K◇, 2♠, 5◇, K♠, 4❤, 2❤, 9♣, 3◇, A❤, 56 // i 0 1 2 3 4 5 6 7 8 9 10 57 for (int i = 0; i < allCards.size() - 3; i++) { 58 59 // 先拿到当前对象 60 Card c = allCards.get(i); 61 62 if (i % 3 == 0) { 63 // 发牌个wl 64 wl.add(c); 65 } else if (i % 3 == 1) { 66 // 发牌给phx 67 phx.add(c); 68 } else if (i % 3 == 2) { 69 //发牌个ai 70 ai.add(c); 71 } 72 } 73 74 // 12. 拿到最后三张牌(把最后三张牌截取成一个子集合), 用一个List的API , subList(51索引, 53索引) 75 List<Card> lastThreeCards = allCards.subList(allCards.size() - 3, allCards.size()); 76 77 // 13. 给玩家的牌排序(从大到小) 78 sortCards(wl); 79 sortCards(phx); 80 sortCards(ai); 81 82 // 14. 输出玩家的牌 83 System.out.println("wl: " + wl); 84 System.out.println("phx: " + phx); 85 System.out.println("ai: " + ai); 86 System.out.println("三张底牌: " + lastThreeCards); 87 } 88 89 /** 90 * 给牌排序 91 */ 92 private static void sortCards(List<Card> cards) { 93 Collections.sort(cards, new Comparator<Card>() { 94 @Override 95 public int compare(Card o1, Card o2) { 96 //降序 97 return o2.getIndex() - o1.getIndex(); 98 } 99 }); 100 } 101 }