需求: 通过程序实现斗地主过程中的洗牌,发牌和看牌。要求:对牌进行排序
思路:
1:创建HashMap,键是编号,值是牌
2:创建ArrayList,存储编号
3:创建花色数组和点数数组
4:从0开始往HashMap里面存储编号,并存储对应的牌。
同时往ArrayList里面存储编号
5:洗牌(洗的是编号),用Collections的shuffle()方法实现
6:发牌(发的也是编号,为了保证编号是排序的,创建TreeSet集合接收)
7:定义方法看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
8:调用看牌方法
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class PokerTest01 {
public static void main(String[] args) {
HashMap<Integer, String> hashMap = new HashMap<>();
ArrayList<Integer> arrayList = new ArrayList<>();
String[] color = {"黑桃", "红桃", "花色", "方块"};
String[] number = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
int index = 0;
for (String numbers : number) {
for (String colors : color) {
hashMap.put(index, colors + numbers);
arrayList.add(index);
index++;
}
}
hashMap.put(index, "小王");
arrayList.add(index);
index++;
hashMap.put(index, "大王");
arrayList.add(index);
Collections.shuffle(arrayList);
TreeSet<Integer> aaa = new TreeSet<>();
TreeSet<Integer> bbb = new TreeSet<>();
TreeSet<Integer> ccc = new TreeSet<>();
TreeSet<Integer> dpSet = new TreeSet<>();
for (int i = 0; i < arrayList.size(); i++) {
int x = arrayList.get(i);
if (i >= arrayList.size() - 3) {
dpSet.add(x);
} else if (i % 3 == 0) {
aaa.add(x);
} else if (i % 3 == 1) {
bbb.add(x);
} else if (i % 3 == 2) {
ccc.add(x);
}
}
lookPoker("方亚", aaa, hashMap);
lookPoker("方正", bbb, hashMap);
lookPoker("方圆", ccc, hashMap);
lookPoker("底牌", dpSet, hashMap);
}
public static void lookPoker(String name, TreeSet<Integer> ts, HashMap<Integer, String> hashMap) {
System.out.print(name + "的牌是:");
for (Integer key : ts) {
String poker = hashMap.get(key);
System.out.print(poker + " ");
}
System.out.println();
}
}