//模拟斗地主
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class PokerDemo {
public static void main(String[] args) {
//创建牌盒
HashMap<Integer, String> pkbox=new HashMap<Integer, String>();
//定义两个数组
String[] colors={"♥" , "♠" , "♣" , "♦"};
String[] nums={"3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" , "A" , "2"};
//定义一个变量,用来记录集合角标
int index=0;
//创建角标集合
ArrayList<Integer> indexs=new ArrayList<Integer>();
//生成牌
for(String num:nums){
for(String color:colors){
pkbox.put(index,color.concat(num));
indexs.add(index);
index++;
}
}
//添加大小王
pkbox.put(index, "小王");
indexs.add(index);
index++;
pkbox.put(index, "大王");
indexs.add(index);
//洗牌
Collections.shuffle(indexs);
//发牌
TreeSet<Integer> ts1=new TreeSet<Integer>();
TreeSet<Integer> ts2=new TreeSet<Integer>();
TreeSet<Integer> ts3=new TreeSet<Integer>();
TreeSet<Integer> ts4=new TreeSet<Integer>();
for(int x=0;x<indexs.size();x++){
if(x>=indexs.size()-3){
ts4.add(indexs.get(x));
}else if(x%3==0){
ts1.add(indexs.get(x));
}else if(x%3==1){
ts2.add(indexs.get(x));
}else{
ts3.add(indexs.get(x));
}
}
//看牌
look("小白",ts1,pkbox);
look("小红",ts2,pkbox);
look("小黑",ts3,pkbox);
look("底牌",ts4,pkbox);
}
public static void look(String name,TreeSet<Integer> ts,HashMap<Integer,String> pkbox){
System.out.print(name+"\t");
for(Integer i:ts){
System.out.print(pkbox.get(i)+"\t");
}
System.out.println();
}
} |
|