package com.heima.collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class Demo3_Collections
{
public static void main(String[] args)
{
// 模拟玩扑克,斗地主
// 先造一副牌
// 数字
//斗地主中3最小,从小到大排序
String[] num =
{ "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
// 颜色
String[] color =
{ "红桃", "黑桃", "方片", "梅花" };
// 定义扑克的索引
ArrayList<Integer> list = new ArrayList<Integer>();
//存储扑克牌
HashMap<Integer, String> puke = new HashMap<>();
int index = 0;
for (String s1 : num)
{
for (String s2 : color)
{
puke.put(index, s2.concat(s1));
list.add(index);
index++;
}
}
// 大王和小王
puke.put(index, "大王");
index++;
puke.put(index, "小王");
// System.out.println(puke.size());
// 洗牌
Collections.shuffle(list);
System.out.println(list);
// 发牌
TreeSet<Integer> a = new TreeSet<>();
TreeSet<Integer> b = new TreeSet<>();
TreeSet<Integer> me = new TreeSet<>();
TreeSet<Integer> dipai = new TreeSet<>();
for (int i = 0; i < list.size(); i++)
{
if (i >= list.size() - 3)
{
dipai.add(list.get(i));
} else if (i % 3 == 0)
{
a.add(list.get(i));
} else if (i % 3 == 1)
{
b.add(list.get(i));
} else
{
me.add(list.get(i));
}
}
System.out.println(a);
System.out.println(b);
System.out.println(me);
System.out.println(dipai);
// 发牌完毕看牌
lookPuke(puke, a, "a");
lookPuke(puke, b, "b");
lookPuke(puke, me, "me");
lookPuke(puke, dipai, "dipai");
}
public static void lookPuke(HashMap<Integer, String> hm,
TreeSet<Integer> ts, String name)
{
System.out.print(name + ":");
for (Integer i : ts)
{
System.out.print(hm.get(i) + " ");
}
System.out.println();
}
}
|
|