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("我:" + meIndex);
lookPoker(poker, meIndex, "我:");

System.out.println("高进:" + gaojinIndex);
lookPoker(poker, gaojinIndex, "高进:");


System.out.println("龙五:" + longWuIndex);
lookPoker(poker, longWuIndex, "龙五:");


}

//看牌
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();
}

}

付明旭

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值