import java.util.*; public class game { private String size; private String
color; private int idenx; public game() { } public game(String size, String
color,int idenx) { this.size = size; this.color = color;
this.idenx=idenx;//牌的真正大小 } public int getIdenx() { return idenx; } public void
setIdenx(int idenx) { this.idenx = idenx; } public String getSize() { return
size; } public void setSize(String size) { this.size = size; } public String
getColor() { return color; } public void setColor(String color) { this.color =
color; } @Override public String toString() { return size+color; } } class
gametext{ //定义一个静态集合存储54张牌 public static List<game> allcards=new ArrayList<>();
//定义静态代码块初始化牌数据 static{//定义点数和花色 String
sizes[]={"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; String
colors[]={"♥","♠","♦","♣"}; //组合点数和花色 int idenx=0;//记录牌的大小 for(String
size:sizes){ idenx++; for(String color:colors){ //封装成一个牌对象 game g=new
game(size,color,idenx); //存到集合容器中去 allcards.add(g); } } //将大小王存入 ♚大王 ♔小王 game
g1=new game("","♚",++idenx); game g2=new game("","♔",++idenx);
Collections.addAll(allcards,g1,g2); System.out.println("新牌:"+allcards); }
public static void main(String[] args) { //洗牌
Collections.shuffle(allcards);//将集合元素打乱 System.out.println("洗牌后:"+allcards);
//发牌,定义三个玩家,将51张牌发给三个玩家,最后三张牌当做地主牌 List<game> zhangsan=new ArrayList<>();
List<game> Lisi=new ArrayList<>(); List<game> wangwu=new ArrayList<>(); for
(int i = 0; i < allcards.size(); i++) { //拿到当前牌对象 game g=allcards.get(i);
if(i%3==0){ //zhangsan接牌 zhangsan.add(g); }else if(i%3==1){ //Lisi接牌
Lisi.add(g); }else if(i%3==2){ //wangwu接牌 wangwu.add(g); } }
//拿到最后三张底牌,把最后三张牌截取成一个字集合 List<game>
eventual=allcards.subList(allcards.size()-3,allcards.size()); //给玩家的牌排序
sortcard(zhangsan); sortcard(Lisi); sortcard(wangwu); //输出玩家的牌
System.out.println("张三:"+zhangsan); System.out.println("李四:"+Lisi);
System.out.println("王五:"+wangwu); System.out.println("三张地主牌:"+eventual); }
private static void sortcard(List<game> games) { Collections.sort(games, new
Comparator<game>() { @Override public int compare(game o1, game o2) { return
o1.getIdenx()-o2.getIdenx(); } }); } }

技术
今日推荐
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:766591547
关注微信