package zbr;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Card {
//定义变量
private String color;
private String number;
private int index;//记录牌的大小,便于发好牌后排序
public Card() {
}
public Card(String color, String number, int index) {
this.color = color;
this.number = number;
this.index = index;
}
public int getIndex() {
return index;
}
//显示牌的点数+花色
@Override
public String toString() {
return this.color+this.number;
}
}
public class CardDemo {
//定义1个静态集合,用来存储54张牌
public static final List<Card> CARD_LIST = new ArrayList<>();
//制作扑克牌
static{
String[] colors = {"♠","♥","♦","♣"};
String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
//添加2-A的不同花色的牌,并用index对每张牌进行编号记录,每张牌编号唯一,便于后面排序
int index = 1;
for (String number : numbers) {
for (String color : colors) {
CARD_LIST.add(new Card(color,number,index));
index++;
}
}
//添加大小王
Collections.addAll(CARD_LIST,new Card("","🤡",53),new Card("","🃏",54));
}
public static void main(String[] args) {
//定义3名玩家 和 1个存放三张地主牌的容器
List<Card> player_1 = new ArrayList<>();
List<Card> player_2 = new ArrayList<>();
List<Card> player_3 = new ArrayList<>();
List<Card> landLordCard = new ArrayList<>();
//洗牌
Collections.shuffle(CARD_LIST);
//发牌,最后3张牌当作地主牌
for (int i = 0; i < CARD_LIST.size(); ) {
if (i>=0&&i<=50) {
player_1.add(CARD_LIST.get(i++));
player_2.add(CARD_LIST.get(i++));
player_3.add(CARD_LIST.get(i++));
} else{
landLordCard.add(CARD_LIST.get(i++));
}
}
//对发好的牌进行排序
mySort(player_1);
mySort(player_2);
mySort(player_3);
mySort(landLordCard);
//输出排序后的牌
System.out.println("player_1:"+player_1);
System.out.println("player_2:"+player_2);
System.out.println("player_3:"+player_3);
System.out.println("landLordCard:"+landLordCard);
}
//定义牌的排序方法
public static void mySort(List <Card> list) {
Collections.sort(list, new Comparator<Card>() {
@Override
public int compare(Card o1, Card o2) {
return o1.getIndex()-o2.getIndex();
}
});
}
}
用集合模拟斗地主洗牌发牌的过程,并将发的牌排序
最新推荐文章于 2022-08-10 18:32:22 发布