案例分析:

牌从大到小的顺序进行排列

图解:

 代码:

public static void main(String[] args) {
//创建一个Map集合存储牌的索引和组装好的牌
HashMap<Integer, String> map = new HashMap<>();
//创建一个集合用于存储扑克牌
ArrayList<Integer> mapIndex = new ArrayList<>();
ArrayList<String> list = new ArrayList<>();
//定义两个数组存放花色和牌的点数
String[] colors={"♠","♣","♥","♦"};
String[] numbers={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
//定义一个牌的索引
int index=0;
map.put(index,"大王");
mapIndex.add(index);
index++;
map.put(index,"小王");
mapIndex.add(index);
index++;
//使用嵌套增强for循环将花色和点数存入map集合
for (String color : colors) {
for (String number : numbers) {
map.put(index,color+number);
mapIndex.add(index);
index++;
}
}

//使用Collections集合工具类里的shuffle进行洗牌
Collections.shuffle(mapIndex);

//创建四个集合 分别是第一个人、第二个人、第三个人、底牌
ArrayList<Integer> person1 = new ArrayList<>();
ArrayList<Integer> person2 = new ArrayList<>();
ArrayList<Integer> person3 = new ArrayList<>();
ArrayList<Integer> cards = new ArrayList<>();


//使用for进行遍历添加集合
for (int i = 0; i < mapIndex.size(); i++) {
Integer in = mapIndex.get(i);
if (i>=51){
//给底牌进行发牌
cards.add(in);
}else if (i%3==0){
//给底牌进行发牌
person1.add(in);
}else if (i%3==1){
//给底牌进行发牌
person2.add(in);
}else if (i%3==2){
//给底牌进行发牌
person3.add(in);
}
}

Collections.sort(person1);
Collections.sort(person2);
Collections.sort(person3);
Collections.sort(cards);

method("张三",map,person1);
method("李四",map,person2);
method("王五",map,person3);
method("底牌",map,cards);
}

/**
*定义一个方法提高代码的复用性
* @param name:玩家名称
* @param map:存储牌的poker集合
* @param list:存储玩家和底牌的List集合
*/
public static void method(String name,HashMap<Integer,String> map,ArrayList<Integer> list){
//输入玩家名称 不换行
System.out.print(name+":");
//遍历玩家或者底牌 获取牌的索引
for (Integer key : list) {
//使用牌的索引 去Map集合中 找到对应的牌
String s = map.get(key);
System.out.print(s+" ");
}
System.out.println();//打印完每一个玩家
}

运行结果:

原文地址:http://www.cnblogs.com/shenziyi/p/16798146.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性