package com.itheima.map集合练习.斗地主;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
public class Test {
public static void main(String[] args) {
//一、创建相关集合:
//1.存放索引和牌的集合
HashMap<Integer,String> map = new HashMap<>();
//2.单独存放索引的集合
ArrayList<Integer> indexList = new ArrayList<>();//indexList 是map的key的值
//3.创建三个玩家 存放是 索引集合%3 得出来的 ( %3就是[0,1,2] )
ArrayList<Integer> player1 = new ArrayList<>();
ArrayList<Integer> player2 = new ArrayList<>();
ArrayList<Integer> player3 = new ArrayList<>();
//4.底牌集合 最后三张牌的索引 【51 52 53】(索引从0开始 少一位)
ArrayList<Integer> dipai = new ArrayList<>();
//二、组合牌
// 先把大王小王放到集合中 并指定好索引
int index = 0;
map.put(index,"大王");
//放入索引集合中 //每次++的时候 把索引的集合放进去
indexList.add(index);
index++;
map.put(index,"小王");
//放入索引集合中
indexList.add(index);
index++;
//2. 13个数字及其对应的花色拼接在一起,放入集合中
//数字集合 [A 2 3 。。。。J Q K]
ArrayList<String> numbers = new ArrayList<>();
//颜色集合 [♠ ♥ ♣ ♦]
ArrayList<String> colors = new ArrayList<>();
//分别存值
Collections.addAll(numbers,"A","2","3","4","5","6","7","8","9","10","J","Q","K");
Collections.addAll(colors,"♠","♥","♣","♦");
//拼接52张牌 每一个数字都对应4个不同花色 13*4 = 52
for (String number : numbers) {
for (String color : colors) {
String pai = color+number;//拼接
map.put(index,pai);
//放入索引集合中
indexList.add(index);
index++;//为下一张的扑克牌做好铺垫
}
}
/*//遍历map集合
Set<Integer> keys = map.keySet();
for (Integer key : keys) {
System.out.println(key+":"+map.get(key));
}
System.out.println(indexList);//索引的集合*/
//三、洗牌
//打乱索引,索引一乱,对应的牌也是乱
Collections.shuffle(indexList);//shuffle洗(牌)
//[18, 19, 49, 3, 51, 38, 17, 37, 53, 50, 24, 6, 22, 23, 52, 14, 47, 9, 2, 42, 7, 45, 28, 5, 36, 29, 16, 40, 12, 0, 4, 34, 46, 8, 27, 26, 10, 30, 35, 33, 25, 15, 39, 20, 48, 43, 13, 31, 41, 32, 44, 21, 1, 11]
//System.out.println(indexList);
//四、发牌
//给三个玩家轮流发牌的索引(54个索引:4*13+2=54)
// 0取模于3等于0,1取模于3等于1,2取模于3等于2,正好轮流发(如果是四个玩家,那么取模于4)
//用到索引的时候,用普通for
for (int i = 0; i < indexList.size(); i++) {
//留三张底牌
if(i >= 51){
dipai.add(indexList.get(i));
}else{
//轮流发
if(i % 3 == 0){
//发给player1
player1.add(indexList.get(i));
}
if(i % 3 == 1){
//发给player2
player2.add(indexList.get(i));
}
if(i % 3 == 2){
//发给player3
player3.add(indexList.get(i));
}
}
}
//第一个玩家[44, 15, 32, 3, 53, 20, 14, 10, 17, 1, 11, 36, 38, 23, 0, 46, 27]
System.out.println("第一个玩家"+player1);
//第二个玩家[41, 25, 29, 49, 39, 35, 7, 42, 40, 12, 34, 31, 19, 13, 43, 18, 50]
System.out.println("第二个玩家"+player2);
//第三个玩家[45, 9, 8, 33, 5, 48, 28, 26, 16, 37, 22, 4, 51, 6, 2, 30, 21]
System.out.println("第三个玩家"+player3);
System.out.println("底牌"+dipai);//底牌[24, 47, 52]
}
}
10-09
3859

07-17
149

02-08
144
