运用java简单的集合操作模拟斗地主。

 

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;

public class Demo02 {

public static void main(String[] args) {
// TODO Auto-generated method stub
// 这里模斗地主的部分
//1.准备一副牌Poker(花色数字)
Map<Integer,String> poker = new HashMap<Integer,String>();

//牌的花色
String[] colors = {"方块","梅花","黑桃","红桃"};

//牌的数字
String[] nums = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

int index = 0;
for(String num : nums){
for(String c : colors){
poker.put(index, c + num);
index ++;
}
}

//加上小王
poker.put(index, "小王");

//加上大王
index ++;
poker.put(index, "大王");

for(Entry<Integer, String> entry : poker.entrySet()){
System.out.println(entry.getKey() + ":" + entry.getValue());
}

//2.洗牌
//获取Map索引,key
Set<Integer> pokerKeys = poker.keySet();
List<Integer> pokerIndexSet = new ArrayList<Integer>();
pokerIndexSet.addAll(pokerKeys);
Collections.shuffle(pokerIndexSet);
System.out.println("打乱牌的顺序:" + pokerIndexSet);

//3.发牌[发牌发索引]
Set<Integer> dipaiIndex = new TreeSet<Integer>();
Set<Integer> meIndex = new TreeSet<Integer>();
Set<Integer> gaojinIndex = new TreeSet<Integer>();
Set<Integer> longWuIndex = new TreeSet<Integer>();
for(int i : pokerIndexSet){
if(i > 50){
dipaiIndex.add(pokerIndexSet.get(i));
}else if(i%3 == 0){
meIndex.add(pokerIndexSet.get(i));
}else if(i%3 == 1){
gaojinIndex.add(pokerIndexSet.get(i));
}else{
longWuIndex.add(pokerIndexSet.get(i));
}
}

System.out.println("底牌" + dipaiIndex);
lookPoker(poker, dipaiIndex, "底牌");

System.out.println("Me" + meIndex);
lookPoker(poker, meIndex, "Me");

System.out.println("GaoJin" + gaojinIndex);
lookPoker(poker, gaojinIndex, "GaoJin");


System.out.println("LongWu" + longWuIndex);
lookPoker(poker, longWuIndex, "LongWu");


}

//看牌
public static void lookPoker(
Map<Integer,String> poker,//牌的数据
Set<Integer> pokerIndex,//牌索引
String role){//角色

System.out.println(role);
for(Integer key : pokerIndex){
System.out.print(poker.get(key) + " ");
}
System.out.println();
}

}

posted @ 2018-04-10 12:46  sun丶凹凸曼  阅读(890)  评论(0编辑  收藏  举报