package test01;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class pokerList {
/*
看牌有序斗地主
1、准备牌
2、发牌
3、洗牌
4、排序
5、看牌
*/
public static void main(String[] args) {
//1、准备牌
//创建Map集合,索引和组好的牌
HashMap<Integer,String> poker = new HashMap<>();
//创建List集合,索引
ArrayList<Integer> pokerIndex = new ArrayList<>();
//创建花色和数字集合
List<String> colors = List.of("♠","♥" ,"♦","♣");
List<String> numbers = List.of("2","A","K" ,"Q","J","10","9","8","7","6","5","4","3");
//放大小王
int index = 0;
poker.put(index,"大王");
pokerIndex.add(index);
index++;
poker.put(index,"小王");
pokerIndex.add(index);
index++;
//组合52张牌
for (String number : numbers) {
for (String color : colors) {
poker.put(index,color+number);
pokerIndex.add(index);
index++;
}
}
//2、洗牌
Collections.shuffle(pokerIndex);
//3、发牌
//创建4个集合放3个玩家牌盒底牌索引
ArrayList<Integer> p1 = new ArrayList<>();
ArrayList<Integer> p2 = new ArrayList<>();
ArrayList<Integer> p3 = new ArrayList<>();
ArrayList<Integer> diPai = new ArrayList<>();
//遍历索引发牌
for (int i = 0; i < pokerIndex.size(); i++) {
Integer ind = pokerIndex.get(i);
//判断底牌
if(i>=51){
diPai.add(ind);
}else if(i%3==0){
p1.add(ind);
}else if(i%3==1){
p2.add(ind);
}else if(i%3==2){
p3.add(ind);
}
}
//4、排序
Collections.sort(p1);
Collections.sort(p2);
Collections.sort(p3);
Collections.sort(diPai);
//5、看牌 调用方法
lookPoker("张三",poker,p1);
lookPoker("李四",poker,p2);
lookPoker("王五",poker,p3);
lookPoker("底牌",poker,diPai);
}
public static void lookPoker(String name,HashMap<Integer,String> poker,ArrayList<Integer> list){
System.out.print(name + ": ");
//使用索引遍历牌
for (Integer key : list) {
String value = poker.get(key);
System.out.print(value + " ");
}
System.out.println();
}
}
Map:看牌有序版斗地主案例
最新推荐文章于 2022-01-02 22:11:37 发布