A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 地狱里的帅灵魂 于 2015-9-3 22:49 编辑

[fly][sup]搞定这个集合就及格了[/sup][/fly]


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;

/**
* 步骤:
*         a: 创建一个牌盒使用Map集合存储
*         b: 定义一个ArrayList存储每一张牌对应的索引
*         c: 定义一个int类型的变量int index = 0 ;
*         d: 定义花色数组和数字数组
*         e: 遍历数组生成指定的牌,把牌添加到牌盒中,并同时存储索引
*        f: 洗牌 , 洗的是索引对应的集合
*        g: 发牌 , 发的也是索引,只不过我们需要使用TreeSet集合存储索引(因为可以排序)
*        h: 看牌
*/
public class PokerDemo2 {
   
    public static void main(String[] args) {
        
        // 创建一个牌盒使用Map集合存储
        HashMap<Integer , String> pokerBox = new HashMap<Integer , String>() ;
        
        // 定义一个ArrayList存储每一张牌对应的索引
        ArrayList<Integer> indexs = new ArrayList<Integer>() ;
        
        // 定义一个int类型的值
        int index = 0 ;
        
        // 定义花色数组
        String[] colors = {"♥" , "♠" , "♣" , "♦"} ;
        
        // 定义数字数组
        String[] nums = {"3" , "4" , "5" , "6" , "7" , "8" , "9" , "10"  ,"J" , "Q" , "K" , "A" , "2"} ;
        
        // 生成牌
        for(String num : nums) {
            for(String color : colors) {
               
                // 牌
                String poker = color.concat(num) ;
               
                // 把这个牌添加到牌盒中
                pokerBox.put(index, poker) ;
               
                // 把索引添加到indexs中
                indexs.add(index) ;
               
                // 索引+1
                index++ ;
               
            }
        }
        
        // 添加大小王
        pokerBox.put(index, "小王") ;
        indexs.add(index) ;
        index++ ;
        
        pokerBox.put(index, "大王") ;
        indexs.add(index) ;
        
        // 洗牌
        Collections.shuffle(indexs) ;
        
        // 发牌
        // 定义四个集合
        TreeSet<Integer> zhourunfaSet = new TreeSet<Integer>() ;
        TreeSet<Integer> zhouxingxingSet = new TreeSet<Integer>() ;
        TreeSet<Integer> huileiyuSet = new TreeSet<Integer>() ;
        TreeSet<Integer> dipaiSet = new TreeSet<Integer>() ;
        
        for(int x = 0 ; x < indexs.size() ; x++) {
            
            if(x >= indexs.size() - 3){
                dipaiSet.add(indexs.get(x)) ;
            }else if(x % 3 == 0) {
                zhourunfaSet.add(indexs.get(x)) ;
            }else if(x % 3 == 1) {
                zhouxingxingSet.add(indexs.get(x)) ;
            }else if(x % 3 == 2) {
                huileiyuSet.add(indexs.get(x)) ;
            }
            
        }
        
        // 看牌
        lookPoker("周润发" , zhourunfaSet , pokerBox) ;
        lookPoker("周星星" , zhouxingxingSet , pokerBox) ;
        lookPoker("刘德华" , huileiyuSet , pokerBox) ;
        lookPoker("底牌" , dipaiSet , pokerBox) ;
    }
   
    public static void lookPoker(String name , TreeSet<Integer> set , HashMap<Integer , String> pokerBox) {
        
        System.out.print(name + ":\t");
        for(Integer i : set) {
            System.out.print(pokerBox.get(i) + "\t");
        }
        System.out.println();
        
    }

}


18 个回复

倒序浏览
活学活用
回复 使用道具 举报

是的,挺有意思的
回复 使用道具 举报
代码生成的牌第一个基本全是3   出现的范围太小了重复率太高
回复 使用道具 举报
楼主要不要继续攻关看牌之后的算法呢?
回复 使用道具 举报
boboyuwu 发表于 2015-9-4 10:08
代码生成的牌第一个基本全是3   出现的范围太小了重复率太高

没有啊,前面是按顺序生成牌,后边就全打乱了。不会有这种情况的
回复 使用道具 举报
王盟 发表于 2015-9-4 20:41
楼主要不要继续攻关看牌之后的算法呢?

必须的啊,快快拿来
回复 使用道具 举报
王盟 发表于 2015-9-4 20:41
楼主要不要继续攻关看牌之后的算法呢?

必须的啊,快快拿来
回复 使用道具 举报

请看附件

FansUnion-DouDiZhu-20131012.zip

1.9 MB, 下载次数: 69

斗地主

回复 使用道具 举报
gpw 中级黑马 2015-9-4 21:49:27
10#
刘意视频讲了
回复 使用道具 举报
字符打的厉害
回复 使用道具 举报
还没学集合呢
回复 使用道具 举报
表示要拿部扑克实践一下才能有感觉敲代码
回复 使用道具 举报
Wqi 高级黑马 2015-9-4 22:21:25
14#
学习了~~~等学完集合也实践一下.....现在只能看看...
回复 使用道具 举报
貌似不太复杂呀
回复 使用道具 举报
gpw 发表于 2015-9-4 21:49
刘意视频讲了

嗯,回顾下
回复 使用道具 举报
Wqi 发表于 2015-9-4 22:21
学习了~~~等学完集合也实践一下.....现在只能看看...

慢慢来,别着急
回复 使用道具 举报
索引是啥意思
回复 使用道具 举报

Set集合和数组每一个元素都对应一个标记,第一个元素对应的是0,第二个是1,第三个是2.。。。以此类推。这个标记就是索引,也就是角标
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马