package Collo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
* 斗地主案例:
* 1.造牌
* 2.洗牌
* 3.发牌
* 4.看牌
* */
public class DoudizhuExercise {
public static void main(String[] args) {
//1.造牌
//这个集合用来存储排,通过索引对应牌
HashMap<Integer, String> poker = new HashMap<>();
//这个用来存储索引,通过这个索引来获得 poker.get(indexs) 的value值来获取牌
ArrayList<Integer> indexs = new ArrayList<>();
//1.of方法可以给集合一次性添加多个元素、of方法适用于List接口,Set接口,Map接口,不适用于接口的实现类
//2.of方法的返回值是一个不能改变的集合,集合不能再使用add,put方法添加元素,
// 会抛出不支持操作异常:UnsupportedOperationException
//3.Set接口和Map接口在调用of方法时,不能有重复的元素,否则会抛出异常。
// Map<String, String> of3 = Map.of("haha", "1", "houhou", "haha");
// System.out.println(of3);{haha=1, houhou=haha}
List<String> of = List.of("♠", "♥", "♣", "♦");
List<String> of2 = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
//因为数字中没有大小小王,所以用索引0和1分别装大小王 这里有顺序要求0装大王1则装小王 因为我们发牌后要根据索引来排序从而整理牌
int count = 0;
poker.put(count, "🐢大王");
indexs.add(count);
count++;
poker.put(count, "🐢小王");
indexs.add(count);
count++;
//给count集合中添加元素 因为前面已经加两次了,所以第三次循环从count开始 所以i=count
for (int i = count; i < 54; i++) {
indexs.add(i);
}
// System.out.println(indexs);
//装2-3的牌 总共13涨 所以从0-12,花色4种,从0-3开始
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
poker.put(count, of.get(j) + of2.get(i));
count++;
}
}
// for(String number : of2) {
// for(String color:of) {
// poker.put(count, color+number);
// count++;
// }
// }
// System.out.println(poker);
//通过Collections工具类种的shuffle方法来对索引数组打乱其顺序
Collections.shuffle(indexs);
//发牌阶段,创建三个玩家集合和底牌集合,用0-53 去 取模3 =0则给玩家1,=1则给玩家2,=2则给玩家3
List<Integer> palyer1 = new ArrayList<>();
List<Integer> palyer2 = new ArrayList<>();
List<Integer> palyer3 = new ArrayList<>();
List<Integer> dipai = new ArrayList<>();
//发牌
for (int i = 0; i < indexs.size(); i++) {
int j = indexs.get(i);
if (i >= 51) {
dipai.add(j);
} else if (i % 3 == 0) {
palyer1.add(j);
} else if (i % 3 == 1) {
palyer2.add(j);
} else if (i % 3 == 2) {
palyer3.add(j);
}
}
//对发到的牌 即使索引排序,循序默认升序
Collections.sort(palyer1);
Collections.sort(palyer2);
Collections.sort(palyer3);
Collections.sort(dipai);
//由于其算法都是一样的,所以用一个方法封装起来调用会更好
// System.out.print("周新驰:");
// for (Integer i : palyer1) {
// System.out.print(" " + poker.get(i));
// }
// System.out.println();
// System.out.print("周润发:");
// for (Integer i : palyer2) {
// System.out.print(" " + poker.get(i));
// }
// System.out.println();
// System.out.print("刘德华:");
// for (Integer i : palyer3) {
// System.out.print(" " + poker.get(i));
// }
// System.out.println();
// System.out.print("底牌:");
// for (Integer i : dipai) {
// System.out.print(" " + poker.get(i));
// }
//3.看牌:
show("周星驰:",poker,palyer1);
show("周润发:",poker,palyer2);
show("刘德华:",poker,palyer3);
show("底牌 :",poker,dipai);
}
//封装看牌方法
public static void show(String name, HashMap<Integer, String> poker, List<Integer> list) {
System.out.print(name);
for (Integer i : list) {
System.out.print(" " + poker.get(i));
}
System.out.println();
}
}