斗地主案例的需求分析与实践

斗地主案例有序版本:

1、准备牌

  特殊牌:大王小王

  52张牌:循环嵌套遍历两个集合/数组,组装52张牌

  将组装好的牌放到Map集合中

2、洗牌

  获取Map集合的键,放到List集合中

  使用Collections中的shuffle(list)方法,将集合打乱

3、发牌

  要求:1人17张牌,最后剩余3张作为底牌,一人一张轮流发牌,让集合的索引%3;

    定义4个集合,用来存储3个玩家的牌和底牌

    索引%3为0时添加到第一个集合

    索引%3为1时添加到第二个集合

    索引%3为2时添加到第三个集合

    当索引大于等于51时,添加到底牌集合

4、排序

  将分发好的牌进行排序,使用Collections中的sort(list)方法

5、看牌:可以使用查表方法

  遍历一个集合,获取到另外一个集合的key,通过key找到value

  遍历玩家和底牌的List集合,获取到Map集合的key,通过key找到value值

复制代码
public class DouDiZhu {
    public static void main(String[] args) {
        HashMap<Integer, String> poker = new HashMap<>();
        List<Integer> pokerIndex = new ArrayList<>();

        List<String> colors = new ArrayList<>();
        List<String> numbers = new ArrayList<>();
        Collections.addAll(colors,"♠","♥","♣","♦");
        Collections.addAll(numbers,"A","2","3","4","5","6","7","8","9","10","J","Q","K");
        int index = 0;
        poker.put(index,"大王");
        pokerIndex.add(index);
        index++;
        poker.put(index,"小王");
        pokerIndex.add(index);
        index++;

        for (String number : numbers) {
            for (String color : colors) {
                poker.put(index,color+number);
                pokerIndex.add(index);
                index++;
            }
        }

        Collections.shuffle(pokerIndex);

        List<Integer> one = new ArrayList<>();
        List<Integer> two = new ArrayList<>();
        List<Integer> three = new ArrayList<>();
        List<Integer> dipai = new ArrayList<>();
        for (int i = 0; i < pokerIndex.size(); i++) {
            if (i>=51){
                dipai.add(pokerIndex.get(i));
            }else {
                if (i%3==0){
                    one.add(pokerIndex.get(i));
                }else if (i%3==1){
                    two.add(pokerIndex.get(i));
                }else if (i%3==2){
                    three.add(pokerIndex.get(i));
                }
            }

        }
        Collections.sort(one);
        Collections.sort(two);
        Collections.sort(three);
        Collections.sort(dipai);

        lookPoker("张三",poker,one);
        lookPoker("李四",poker,two);
        lookPoker("王五",poker,three);
        lookPoker("底牌",poker,dipai);
    }
    public static void lookPoker(String name,HashMap<Integer, String> poker,List<Integer> list){
        System.out.print(name+":");
        for (Integer key : list) {
            String value = poker.get(key);
            System.out.print(value+" ");
        }
        System.out.println();
    }
}
复制代码

 

posted @   xjw12345  阅读(106)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示