package com.zhongruan;
import java.util.*;
public class DouDiZzhu2 {
public static void main(String[] args){
// 使用map创建一副扑克
String [] numbers={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
String [] colors={"♥","♠","♣","♦"};
Map<Integer,String> poker=new HashMap<>();
int index=1;
poker.put(index++,"大王");
poker.put(index++,"小王");
for (String number:numbers){
for (String color:colors){
String pai=color+number;
poker.put(index++,pai);
}
}
// System.out.println(poker);
// 将牌的索引创建为一个新的集合
List<Integer> pokerIndexs=new ArrayList<>();
Set<Integer>integers=poker.keySet();
System.out.println(integers);
for (Integer i:poker.keySet()){
pokerIndexs.add(i);
}
// 将牌的索引打乱
Collections.shuffle(pokerIndexs);
System.out.println(pokerIndexs);
// 创建四个Set接受三个玩家和底牌的索引值
Set<Integer> player1=new TreeSet<>();
Set<Integer> player2=new TreeSet<>();
Set<Integer> player3=new TreeSet<>();
Set<Integer> dipai=new TreeSet<>();
dipai.add(pokerIndexs.remove(0));
dipai.add(pokerIndexs.remove(0));
dipai.add(pokerIndexs.remove(0));
for (int i=0;i<pokerIndexs.size();i++){
int pi=pokerIndexs.get(i);
int mod=i%3;
if(mod==0){
player1.add(pi);
}else if (mod==1){
player2.add(pi);
}else {
player3.add(pi);
}
}
// 调用look方法将索引值转换为扑克牌
look(poker,player1);
look(poker,player2);
look(poker,player3);
look(poker,dipai);
}
// 创建look方法,通过索引从扑克牌中取牌
public static void look(Map<Integer,String> poker,Set<Integer> indexs){
List<String> p=new ArrayList<>();
for(Integer i:indexs){
String pai=poker.get(i);
p.add(pai);
}
System.out.println(p);
}
}
斗地主案例
最新推荐文章于 2021-08-07 19:48:55 发布