package com.test;
import org.junit.Test;
import java.util.*;
/**
* @program: study
* @description: 模拟斗地主
* @author: Elias.Guo
* @create: 2020-06-11 16:41
**/
public class MoNiDouDiZhu {
@Test
public void test(){
//创建HashMap key是编号,值是牌
HashMap map =new HashMap<>();
//创建List,存储编号
List list =new ArrayList<>();
//创建花色数组
String[] colors ={"♦", "♣", "♠", "♥"};
//创建牌数组
String[] numbers={"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
//遍历,存储牌和编号
int index =0;
for (String color : colors) {
for (String number : numbers) {
String poker =color+number;
map.put(index,poker);
list.add(index);
index++;
}
}
//添加大小王
map.put(index,"大王");
list.add(index);
index++;
map.put(index,"小王");
list.add(index);
//洗牌
Collections.shuffle(list);
//定义玩家
TreeSet faGe=new TreeSet<>();
TreeSet reBa=new TreeSet<>();
TreeSet me=new TreeSet<>();
TreeSet dePai=new TreeSet<>();
//发牌
for(int i=0;i=list.size()-3){
//底牌
dePai.add(list.get(i));
}else if(i%3==0){
//发哥
faGe.add(list.get(i));
}else if(i%3==1){
//热巴
reBa.add(list.get(i));
}else if(i%3==2){
//华仔
me.add(list.get(i));
}
}
//看牌方法调用
showPoker("发哥", faGe, map);
showPoker("迪丽热巴", reBa, map);
showPoker("迪丽热巴的老公(我)", me, map);
showPoker("底牌",dePai,map);
}
public static void showPoker(String name, TreeSet tr, HashMap map) {
System.out.print(name + "的牌是:");
for (Integer key : tr) {
String value = map.get(key);
System.out.print(value + " ");
}
System.out.println();
System.out.println("-------------------");
}
}
输出结果:
发哥的牌是:♦5 ♦J ♦A ♣7 ♣10 ♣J ♣K ♣2 ♠6 ♠10 ♠A ♠2 ♥6 ♥7 ♥8 ♥10 ♥K
-------------------
迪丽热巴的牌是:♦4 ♦9 ♣3 ♣6 ♣8 ♣Q ♣A ♠4 ♠5 ♠7 ♠J ♠K ♥3 ♥9 ♥Q ♥A 小王
-------------------
迪丽热巴的老公(我)的牌是:♦3 ♦6 ♦7 ♦8 ♦K ♦2 ♣4 ♣5 ♣9 ♠3 ♠8 ♠Q ♥4 ♥5 ♥J ♥2 大王
-------------------
底牌的牌是:♦10 ♦Q ♠9
-------------------
Process finished with exit code 0