6.斗地主(单列集合练习)

6.斗地主(单列集合练习)


使用集合工具类Collections的方法static void shuffle(List<?>list):会随机的打乱集合中元素的顺序。

 斗地主案例
 * .分析:
 *     1.准备牌:54张牌,存储到一个集合中。
 *       特殊牌:大王,小王
 *       其它52张牌:
 *              定义一个数组或集合,存储4种花色:♠,♥,♣,◆
 *              定义一个数组或集合,存储13个序号:2,A,K,Q...3
 *              循环嵌套遍历两个数组或集合,组装4*13=52张牌。如♠2,♠A,♠K...♥2,♥A...
 *     2.洗牌:
 *             使用集合工具类Collections的方法
 *             static void shuffle(List<?>list):使用指定的随机源对指定列表进行置换
 *             会随机的打乱集合中元素的顺序。
 *     3.发牌
 *             要求:1人17张牌,剩余3张作为底牌,一人一张轮流发牌:集合的索引(0-53)%3
 *             定义4个集合,存储3个玩家的牌和底牌。
 *             索引%3有三个值(0,1,2) 0%3=0 1%3=1 2%3=2 3%3=0
 *             索引>=51改底牌发牌。
 *     4.看牌
 *             直接打印集合,遍历存储玩家和底牌的集合。


public class Doudizhu_case {
    public static void main(String[] args) {
        /**
         * 1.准备牌
         */
        //定义一个存储54张牌的ArrayList集合(list集合有索引),泛型使用String
        ArrayList<String> poker = new ArrayList<>();
        //定义两个数组,一个数组存储牌的花色,一个数组存储牌的序号
        String[] colors={"♠","♥","♣","◆"};
        String[] numbers={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
        //先把大王和小王存储到poker集合中
        poker.add("大王");
        poker.add("小王");
        //循环嵌套遍历两个数组,组装52张牌
        for (String number : numbers) {
            for (String color : colors) {
               //把组装好的牌存储到poker集合
                poker.add(color+number);//字符串的拼接为一个字符串
            }
        }
        /*poker集合中的牌已准备好如下:System.out.println(poker);
        * [大王, 小王, ♠2, ♥2, ♣2, ◆2, ♠A, ♥A, ♣A, ◆A, ♠K, ♥K, ♣K, ◆K,
         * ♠Q, ♥Q, ♣Q, ◆Q, ♠J, ♥J, ♣J, ◆J, ♠10, ♥10, ♣10, ◆10, ♠9, ♥9,
           ♣9, ◆9, ♠8, ♥8, ♣8, ◆8, ♠7, ♥7, ♣7, ◆7, ♠6, ♥6, ♣6, ◆6, ♠5,
           ♥5, ♣5, ◆5, ♠4, ♥4, ♣4, ◆4, ♠3, ♥3, ♣3, ◆3]
        * */

        /**
         * 2.洗牌
         *   使用集合的工具类Collections中的方法打乱集合中顺序
         *   static void shuffle(List<?>list) 使用默认随机源对指定列表进行置换
         */
        Collections.shuffle(poker);

        /**
         * 3.发牌
         */
        //定义4个集合,存储3个玩家的牌和底牌(因为要用集合索引,所以用list集合)
        ArrayList<String>play01=new ArrayList<>();
        ArrayList<String>play02=new ArrayList<>();
        ArrayList<String>play03=new ArrayList<>();
        ArrayList<String>dipai=new ArrayList<>();
        /**
         *  遍历poker集合,获取每一张牌
         *  使用poker集合的索引%3给3个玩家轮流发牌
         *  剩余3张牌给底牌。
         *  注意:先判断底牌(i>=51),否则牌就发没了。
         *  不能使用增强for遍历,因为它没有索引
         */
        for (int i=0;i<poker.size();i++){
            //获取每一张牌
            String p = poker.get(i);
            //轮流发牌
            if(i>=51){
                //给底牌发牌
                dipai.add(p);
            }else if(i%3==0){
                //给玩家1发牌
                play01.add(p);
            }else if (i%3==1){
                //给玩家2发牌
                play02.add(p);
            }else if(i%3==2){
                //给玩家3发牌
                play03.add(p);
            }
        }

        /**
         * 4.看牌
         */
        System.out.println("张三:"+play01);
        System.out.println("李四:"+play02);
        System.out.println("王五:"+play03);
        System.out.println("底牌:"+dipai);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值