package com.js.Pai;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* 功能:
* 1.做牌。
* 2.洗牌。
* 3.定义3个玩家
* 4.发牌。
* 5.排序
* 6.看牌
* @author JiaShuai
* @date 2022/4/12 21:08
*/
public class CardDemo {
public static List<Card> allCards=new ArrayList<>();
static {
//做牌。定义一个集合存储54张牌
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);
allCards.add(c);
}
}
//将大小王存入到集合中
Card c1=new Card("","👲",++index);
Card c2=new Card("","🃏",++index);
Collections.addAll(allCards,c1,c2);
System.out.println("新牌:"+allCards);
}
public static void main(String[] args) {
//调用Collections的shuffle对数组进行乱序排列(洗牌)
Collections.shuffle(allCards);
System.out.println("洗牌后:"+allCards);
//定义三个集合(玩家)
List<Card> zhangsan=new ArrayList<>();
List<Card> lisi=new ArrayList<>();
List<Card> wangwu=new ArrayList<>();
//使用对3取余的方法进行发牌
for (int i = 0; i < allCards.size()-3; i++) {
Card c= allCards.get(i);
if (i%3==0){
//张三的牌
zhangsan.add(c);
}else if(i%3==1){
//李四的牌
lisi.add(c);
}else {
//王五的牌
wangwu.add(c);
}
}
//剩余的三张底牌
List<Card> lastThreeCards=allCards.subList(allCards.size()-3,allCards.size());
//调用方法进行排序
sortCards(zhangsan);
sortCards(lisi);
sortCards(wangwu);
System.out.println("张三的牌:"+zhangsan);
System.out.println("李四的牌:"+lisi);
System.out.println("王五的牌:"+wangwu);
System.out.println("三张底牌:"+lastThreeCards);
}
//将每个人手中的牌进行排序(从大到小)
private static void sortCards(List<Card> cards) {
Collections.sort(cards, new Comparator<Card>() {
@Override
public int compare(Card o1, Card o2) {
return o2.getIndex()- o1.getIndex();
}
});
}
}
截图
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)