# JAVASE-22：Collections工具类，斗地主洗牌发牌、整理牌

#### Collections工具类

Collections成员方法
public static < T > void sort (List < T > list):排序, 默认按照自然顺序
public static < T > int binarySearch (List < ? > list, T key):二分查找
public static < T > T max(Collection < ? > coll):获取最大值
public static void reverse (List < ? > list):反转
public static void shuffle (List < ? > list):随机置换

public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
// 排序
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer a, Integer b) {
return a-b;
}
});
System.out.println(list);//[10, 20, 24, 202, 205, 209]

int i = Collections.binarySearch(list, 20);
System.out.println(i);//1
Integer max = Collections.max(list);//获取最大值209
Integer min = Collections.min(list);//获取最小值10
System.out.println(max);
System.out.println(min);

Collections.reverse(list);//倒序
System.out.println(list);//[209, 205, 202, 24, 20, 10]
Collections.shuffle(list);//随机置换
System.out.println(list);//[24, 20, 209, 10, 202, 205]
}


public class MyTest {
public static void main(String[] args) {
// 案例演示:模拟斗地主洗牌和发牌，牌没有排序
// 1.创建一个集合，来充当这个牌盒子
ArrayList<String> pokerBox = new ArrayList<>();
// 2.生成 牌、花色 和 数字
String[] nums = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
String[] colors = {"♣", "♥", "♦", "♠"};
for (String color : colors) {
for (String num : nums) {
String pai = color.concat(num);//拼接字符串
}
}
// 手动添加大小王
// 洗牌:
Collections.shuffle(pokerBox);
Collections.shuffle(pokerBox);
Collections.shuffle(pokerBox);

// 发牌：
/*
List<String> aList = pokerBox.subList(0, 17);
List<String> bList = pokerBox.subList(17, 34);
List<String> cList = pokerBox.subList(34, 51);*/

// 一人一张来发
// 定义三个人的集合，在有一个底牌集合
ArrayList<String> 星仔 = new ArrayList<>();
ArrayList<String> 刀仔= new ArrayList<>();
ArrayList<String> 高进 = new ArrayList<>();
ArrayList<String> 底牌 = new ArrayList<>();
// 星仔 0 3 6  9
// 刀仔 1 4 7  10
// 高进 2 5 8  11
// 对3取余
for (int i = 0; i < pokerBox.size(); i++) {
if(i>=pokerBox.size()-3){
}else if(i%3==0){
}else if(i % 3 == 1){
}else{
}
}
// 看牌
lookPoker("星仔", 星仔);
lookPoker("刀仔", 刀仔);
lookPoker("高进", 高进);
lookPoker("底牌", 底牌);
}

private static void lookPoker(String name, ArrayList<String> list) {
System.out.println(name);
for (String s : list) {
System.out.print(s+"\t");
}
System.out.println();
}
}


public class HomeWork {
public static void main(String[] args) {

HashMap<Integer, String> Pai = new HashMap<>();
String[] nums = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
String[] colors = {"♣", "♥", "♦", "♠"};
int i=0;
for (String color : colors) {
for (String num : nums) {
String pai = num.concat(color);//拼接字符串
Pai.put(i,pai);
i++;
}
}
// 手动添加大小王
Pai.put(52,"☀");
Pai.put(53,"⭐");
System.out.println(Pai);

// 洗牌
ArrayList<Integer> integers = new ArrayList<>();
for (int j = 0; j < 54; j++) {
}

Collections.shuffle(integers);
Collections.shuffle(integers);
Collections.shuffle(integers);
System.out.println(integers);

// 发牌：给三个人，再留三张底牌
TreeSet<String> PlayerOne = new TreeSet<>();
TreeSet<String> PlayerTwo = new TreeSet<>();
TreeSet<String> PlayerThree = new TreeSet<>();
TreeSet<String> DiPai = new TreeSet<>();
for (int k = 0; k < Pai.size(); k++) {
if(k>=Pai.size()-3){
}else if(k%3==0){
}else if(k%3==1){
}else {
}
}

// 看牌
LookPai("PlayerOne",PlayerOne);
LookPai("PlayerTwo",PlayerTwo);
LookPai("PlayerThree",PlayerThree);
LookPai("底牌",DiPai);
}

private static void LookPai(String name,TreeSet<String> set) {
System.out.println(name);
for (String s : set) {
System.out.print(s + "\t");
}
System.out.println();
}
}

04-19 98

06-02 23
05-13 756
08-30 1265
05-09 1776
06-22 619
04-08 115万+