本帖最后由 马瑞_u9n12 于 2015-12-27 18:41 编辑
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.TreeSet;
- //* 模拟斗地主洗牌和发牌并对牌进行排序的代码实现
- public class Demo5 {
- public static void main(String[] args) {
- //先定义一幅牌 用 hashmap集合 定义
- String[] num = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
- String[] color = {"方片","梅花","红桃","黑桃"};
- HashMap<Integer, String> hm = new HashMap<>(); //存储索引和扑克牌
-
- ArrayList<Integer> list = new ArrayList<>(); //存储索引 洗牌 洗到低还是洗的索引值
- int index = 0 ; //定义索引值为0;
- for(String s1: num){
- for(String s2: color){
- hm.put(index, s2.concat(s1) );
- //将索引和扑克牌添加到hashmap 中
- list.add(index);
- index++;
- }
- }
- hm.put(index, "小王");
- list.add(index);
- index++;
- hm.put(index, "大王");
- list.add(index);
- //洗牌
- Collections.shuffle(list);
- //发牌 因为 treeSet 集合的 排序 和 无重复特点 所以用他
- TreeSet<Integer> gaojin = new TreeSet<>();
- TreeSet<Integer> longwu = new TreeSet<>();
- TreeSet<Integer> me = new TreeSet<>();
- TreeSet<Integer> dipai = new TreeSet<>();
- for(Integer i: list){
- if( i >=list.size()-3){
- dipai.add(list.get(i));
- }
- else if(i%3==0){
- gaojin.add(list.get(i));
- }else if(i%3==1){
- longwu.add(list.get(i));
- }else {
- me.add(list.get(i));
- }
- }
- //看牌 定义方法 看牌的过程就是通过键找值的过程
- //lookpai(String ,高进,hm);
- /*for (Integer i : dipai) {
- System.out.println("dipaishi"+hm.get(i));
- }*/
- lookpai("高进",gaojin,hm);
- lookpai("龙五",longwu,hm);
- lookpai("我的",me,hm);
- lookpai("底牌",dipai,hm);
-
- }
- public static void lookpai (String names, TreeSet<Integer> ts, HashMap<Integer, String>hm ){
- System.out.print(names+"的牌是");
- for (Integer key : ts) {
- System.out.print(hm.get(key)+" ");
-
- }
- System.out.println();
- }
- }
复制代码
|