package com.heima.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class T3 {
// 模拟斗地主洗牌和发牌并对牌进行排序的代码实现
public static void main(String[] args) {
String[] s = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
String[] color = {"黑桃","红桃","梅花","方片"};
//买牌
HashMap<Integer, String> hm = new HashMap<Integer, String>();
ArrayList<Integer> list = new ArrayList<>();
int index = 0;
//整理牌
for (String a1 : color) {
for (String a2 : s) {
String s1 = a1.concat(a2);
System.out.print(s1 + " ");
hm.put(index,s1);
list.add(index);
index++;
}
System.out.println();
}
hm.put(index, "小王");
list.add(index);
index++;
hm.put(index, "大王");
list.add(index);
//洗牌
Collections.shuffle(list);
//发牌
TreeSet<Integer> ts1 = new TreeSet<>();
TreeSet<Integer> ts2 = new TreeSet<>();
TreeSet<Integer> ts3 = new TreeSet<>();
TreeSet<Integer> dipai = new TreeSet<>();
for(int i = 0;i < list.size();i ++){
if(i >= list.size()-3 && i < list.size())
dipai.add(list.get(i));
else if(i % 3 == 0)
ts1.add(list.get(i));
else if(i % 3 == 1)
ts2.add(i);
else ts3.add(list.get(i));
}
//看牌
look(hm,ts1,"阿辉");
look(hm,ts2,"明辉");
look(hm,ts3,"我");
look(hm,dipai,"底牌");
}
private static void look(HashMap<Integer, String> hm, TreeSet<Integer> ts1, String name) {
System.out.print(name + "的牌" + "\t:");
for (Integer i : ts1) {
System.out.print(hm.get(i) + "\t");
}
System.out.println();
}
}
|
|