package cn.demo.one;
import java.sql.Connection;
import java.util.*;
/**
* @author wangze 模拟斗地主发牌
*/
public class SendPokerTest {
public static void main(String[] args) {
// 1.买牌
// 1.1定义一个双列集合,键:牌的编号 值:具体的牌,规则:编号越小,牌越小
Map<Integer, String>pokers=new HashMap<>();
// 1.2定义一个单列集合,用来存储所有牌的编号
List<Integer>list=new ArrayList<>();
// 1.3具体的买牌动作
// 普通牌
String[] colors={"♠","♦","♥","♣"};
String[] numbers={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
int num=0;//牌的编号
for(String number:numbers){
for(String color:colors){
String poker=color+number;
// 将牌的编号,值放入双列结合pokers中
pokers.put(num,poker);
// 将牌的编号放到单列集合中
list.add(num);
// 每添加一次牌编号自增1
num++;
}
}
// 大小王
pokers.put(num,"小王");
list.add(num++);
pokers.put(num,"大王");
list.add(num++);
// 打印牌
System.out.println("所有的牌"+pokers);
System.out.println("牌的编号"+list);
// 2.洗牌
Collections.shuffle(list);
System.out.println("洗好牌后编号为"+list);
// 3. 发牌
// 四个集合,三个玩家,一个底牌
List<Integer> lufei=new ArrayList<>();
List<Integer> suolong=new ArrayList<>();
List<Integer> shanzhi=new ArrayList<>();
List<Integer> dipai=new ArrayList<>();
// 具体的发牌,索引和3取模,决定发给谁
for (int i = 0; i < list.size(); i++) {
Integer pokerNum= list.get(i);
if (i>= list.size()-3){
dipai.add(pokerNum);
}else if(i%3==0){
lufei.add(pokerNum);
}else if(i%3==1){
suolong.add(pokerNum);
}else if(i%3==2){
shanzhi.add(pokerNum);
}
}
// 查看玩家,底牌的编号
// System.out.println("路飞的牌是"+lufei);
// System.out.println("索隆的牌是"+suolong);
// System.out.println("山治的牌是"+shanzhi);
// System.out.println("底牌是"+dipai);
// 看牌
// 先把牌排序
// 打印牌
System.out.println("路飞的牌是"+printPoker(lufei,pokers));
System.out.println("索隆的牌是"+printPoker(suolong,pokers));
System.out.println("山治的牌是"+printPoker(shanzhi,pokers));
System.out.println("底牌是"+printPoker(dipai,pokers));
}
// 看牌方法
public static String printPoker(List<Integer> nums,Map<Integer, String>pokers){
Collections.sort(nums);
StringBuilder sb=new StringBuilder();
for(Integer num:nums){
String poker = pokers.get(num);
sb.append(poker+" ");
}
String str=sb.toString();
return str.trim();
}
}
运行结果: