斗地主案例

模拟斗地主洗牌发牌

在这里插入图片描述

import java.util.ArrayList;
import java.util.Collections;
/**
 * 斗地主综合案例
 * 1.准备牌
 * 2.洗牌
 * 3.发牌
 * 4.看牌
 */
public class DouDiZhu {
    public static void main(String[] args) {
        //1.准备牌
        //定义一个存储54张牌的ArrayList集合,泛型使用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);
            }
        }
        /** 2.洗牌
         *  使用集合的工具类Collections中的办法
         *  static void shuffler(List<?>List)使用默认随机源对指定列表进行置换。
         */
        Collections.shuffle(poker);
        /**
         * 3.发牌
         * 定义4个集合,存储到玩家的牌和底牌
         */
        ArrayList<String> Player01 = new ArrayList<>();
        ArrayList<String> Player02 = new ArrayList<>();
        ArrayList<String> Player03 = new ArrayList<>();
        ArrayList<String> dipai = new ArrayList<>();
        /**
         * 遍历poker集合,获取每一张牌
         * 使用poker集合的索引%3给三个玩家轮流发牌
         * 剩余3张牌给底牌
         * 注意:
         *      先判断底牌(i>=51),否则底牌就发没了
         */
        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发牌
                Player01.add(p);
            } else if (i % 3 == 1) {
                //给玩家2发牌
                Player02.add(p);
            } else if (i % 3 == 2) {
                //给玩家3发牌
                Player03.add(p);
            }

        }
        //4.看牌
        System.out.println("刘德华" + Player01);
        System.out.println("周润发" + Player02);
        System.out.println("周星驰" + Player03);
        System.out.println("底牌" + dipai);
    }
}

在这里插入图片描述

模拟斗地主洗牌发牌(有序版本)

在这里插入图片描述

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

/**
 * 斗地主综合案例(有序版本)
 * 1.准备牌
 * 2.洗牌
 * 3.发牌
 * 4.排序
 * 5.看牌
 */
public class DouDiZhu2 {
    public static void main(String[] args) {
        //1.准备牌
        //创建一个Map集合,存储牌的索引和组装好的牌
        HashMap<Integer, String> poker = new HashMap<>();
        //创建一个List集合,存储牌的索引
        ArrayList<Integer> pokerIndex = new ArrayList<>();
        //定义两个集合,存储花色和牌的序号
        List<String> colors = List.of("♠", "♥", "♣", "♦");
        List<String> numbers = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
        //先把大王和小王存储到poker集合中
        //定义一个牌的索引
        int index = 0;
        poker.put(index, "大王");
        pokerIndex.add(index);
        index++;
        poker.put(index, "小王");
        pokerIndex.add(index);
        index++;
        //循环嵌套遍历两个数组,组装52张牌
        for (String number : numbers) {
            for (String color : colors) {
                poker.put(index, color + number);
                pokerIndex.add(index);
                index++;
            }
        }
        /** 2.洗牌
         *  使用集合的工具类Collections中的办法
         *  static void shuffler(List<?>List)使用默认随机源对指定列表进行置换。
         */
        Collections.shuffle(pokerIndex);
        /**
         * 3.发牌
         * 定义4个集合,存储到玩家的牌和底牌
         */
        ArrayList<Integer> Player01 = new ArrayList<>();
        ArrayList<Integer> Player02 = new ArrayList<>();
        ArrayList<Integer> Player03 = new ArrayList<>();
        ArrayList<Integer> dipai = new ArrayList<>();
        //遍历存储牌索引的List集合,获取每一个牌的索引
        for (int i = 0; i < pokerIndex.size(); i++) {
            Integer in = pokerIndex.get(i);
            //先判断底牌(i>=51),否则底牌就发没了
            if (i >= 51) {
                //改发底牌
                dipai.add(in);
            } else if (i % 3 == 0) {
                //给玩家1发牌
                Player01.add(in);
            } else if (i % 3 == 1) {
                //给玩家2发牌
                Player02.add(in);
            } else if (i % 3 == 2) {
                //给玩家3发牌
                Player03.add(in);
            }
        }
            //4.排序
            //使用Collections中的方法sort(List)
            //默认是升序排序
            Collections.sort(Player01);
            Collections.sort(Player02);
            Collections.sort(Player03);
            Collections.sort(dipai);
            //5.看牌
            lookPoker("刘德华",poker,Player01);
            lookPoker("周瑞发",poker,Player02);
            lookPoker("周星驰",poker,Player03);
            lookPoker("底牌",poker,dipai);

    }
    //定义一个看牌的方法,提高代码的复用性
    public static void lookPoker(String name,HashMap<Integer,String> poker,ArrayList<Integer> list){
        //输出玩家名称,不换行
        System.out.print(name+":");
        //遍历玩家或者底牌集合,获取牌的索引
        for (Integer key : list) {
            //使用牌的索引,去Map集合中,找到对应的牌
            String value =poker.get(key);
            System.out.print(value+" ");
        }
        System.out.println();//打印每一玩家后换行
    }
}

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值