1、创建一个HashMap 集合 在Map中存储键值对,
键是索引值,值是花色和点数组成的字符串,索引用来指定派,利用索引进行洗牌操作
2、创建一个ArrayList,用来存放牌的编号
3、创建花色数组和点数数组
4、从 1 编号 开始向 HashMap 中存放相应的编号和对应的牌,同时往 ArrayList 中存储编号
5、洗牌(洗的 是编号)
6、发牌(发的是编号,为了保证编号时有序的,应该创建一个TreeSet集合)
7、看牌(遍历 TreeSet,获取编号,然后从HashMap中找到对应的牌)
package poke;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Demo01 {
public static void main(String[] args) {
//创建一个 ArrayList 用于存放 扑克牌的序号
ArrayList<Integer> pokerNum = new ArrayList<>();
//创建一个HashMap 用于存放扑克牌
HashMap<Integer, String> poker = new HashMap<>();
//创建一个 ArrayList 用于存放底牌
ArrayList<Integer> dipai = new ArrayList<>();
Person p1 = new Person("小红");
Person p2 = new Person("小明");
Person p3 = new Person("小白");
String[] colors = {"♠","♥","♣","♦"};
String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
int count = 1;
for(String number : numbers) {
for(String color : colors) {
poker.put(count, color.concat(number));
pokerNum.add(count);
count++;
}
}
poker.put(count, "小王");
pokerNum.add(count++);
poker.put(count, "大王");
pokerNum.add(count++);
//System.out.println(poker);
//洗牌
Collections.shuffle(pokerNum);
System.out.println(pokerNum);
//发牌
for(int i = 0 ;i<pokerNum.size();i++) {
if(i >=pokerNum.size() -3) {
dipai.add(pokerNum.get(i));
}else {
if(i % 3 == 0) {
p1.setPokers(pokerNum.get(i));
}
if(i % 3 == 1) {
p2.setPokers(pokerNum.get(i));
}
if (i % 3 == 2 ) {
p3.setPokers(pokerNum.get(i));
}
}
}
//System.out.println(p1);
lookPokers(p1, poker);
lookPokers(p2, poker);
lookPokers(p3, poker);
lookDiPai(dipai, poker);
}
//看牌
public static void lookPokers(Person person,Map<Integer, String> map) {
int count = person.getPokers().size();
System.out.println(person.getName() + "的牌是:" + person.getName() + "共有" + count + "张牌" );
for(int i = 0;i<count;i++) {
System.out.print(map.get(person.getPokers().get(i)) + " ");
}
System.out.println();
}
public static void lookDiPai(List<Integer> list,Map<Integer, String> map) {
System.out.println("底牌是:");
for(int i = 0;i<list.size();i++) {
System.out.print(map.get(list.get(i)) + " ");
}
System.out.println();
}
}