斗地主洗牌发牌——排序版

斗地主洗牌发牌——排序版,第1张

文章目录
  • 前言

    一、知识点

    二、使用步骤

    2.完整代码

            感谢您的阅读,不足之处欢迎指正


前言

        Java实现模拟斗地主洗牌发牌 *** 作,留下三张底牌,对发到手的纸牌排序。

一、知识点

        主要使用HashMap 集合和ArrayList集合及相关方法

二、使用步骤

        1)封装一副牌:使用Map集合,以数字(0~53)为键,纸牌牌面数字(大王、小王、2、A、K、Q、J、10、9...)为值,存储到Map集合中。存储完毕后,数字由小到大的顺序对应的也就是纸牌从大到小的排序。

Map poker = new HashMap<>();

        2)提取键作为编号,存储到ArrayList集合中。

ArrayList indexList = new ArrayList<>(poker.keySet());

        3)洗牌:打乱编号顺序。

Collections.shuffle(indexList);

        4)使用ArrayList集合定义三位玩家和底牌。

List p1 = new ArrayList<>();
List p2 = new ArrayList<>();
List p3 = new ArrayList<>();
List dipai = new ArrayList<>();

        5)发牌:发编号,将打乱顺序的编号发到三位玩家手中,留三张底牌。

for (int i = 0; i < indexList.size(); i++) {
    int n = indexList.get(i);
    //处理底牌
    if (i >= indexList.size() - 3) {
        dipai.add(n);
    } else {
        if (i % 3 == 0) {
            p1.add(n);
        } else if (i % 3 == 1) {
            p2.add(n);
        } else if (i % 3 == 2) {
            p3.add(n);
        }
    }
}

        6)对发到手的纸牌编号排序。

Collections.sort(p1);
Collections.sort(p2);
Collections.sort(p3);
Collections.sort(dipai);

        7)看牌:通过排序好的编号作为键,去查找对应的值,拼接到字符串中,这就是玩家手中最终排序好的牌。

public static String getPoker(List list, Map map) {
    StringBuilder bld = new StringBuilder("[");
    for (int i = 0; i < list.size(); i++) {
        //取出编号
        Integer n = list.get(i);
        //用编号做键,到map中取值(牌面)
        String poker = map.get(n);
        //将牌面封装到StringBuilder
        bld.append(poker);
        if (i < list.size() - 1) {
            bld.append(", ");
        } else {
            bld.append("]");
        }
    }
    return bld.toString();
}
2.完整代码

代码如下

public class Poker {
    public static void main(String[] args) {
        //1.封装一副牌,以Integer为键,String为值,并存储到Map中
        String[] colors = {"♥", "♠", "♦", "♣"};
        String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
        //定义map集合
        Map poker = new HashMap<>();
        int index = 0;
        //存储大小王到集合中
        poker.put(index++, "大王");
        poker.put(index++, "小王");
        //存储纸牌到集合中(除大小王)
        //使用增强for(嵌套),外层纸牌点数,内层纸牌花色
        for (String n : numbers) {//"2"
            for (String c : colors) {//"♥", "♠", "♦", "♣"
                poker.put(index++, c + n);
            }
        }
        //2.提取编号,并转为List集合
        ArrayList indexList = new ArrayList<>(poker.keySet());
        //3.洗牌(洗编号:key)
        Collections.shuffle(indexList);
        //定义三名玩家和底牌 集合
        List p1 = new ArrayList<>();
        List p2 = new ArrayList<>();
        List p3 = new ArrayList<>();
        List dipai = new ArrayList<>();
        //4.发牌(发编号:key)
        for (int i = 0; i < indexList.size(); i++) {
            int n = indexList.get(i);
            //处理底牌
            if (i >= indexList.size() - 3) {
                dipai.add(n);
            } else {
                if (i % 3 == 0) {
                    p1.add(n);
                } else if (i % 3 == 1) {
                    p2.add(n);
                } else if (i % 3 == 2) {
                    p3.add(n);
                }
            }
        }
        //对发到手的纸牌排序(根据key排序)
        Collections.sort(p1);
        Collections.sort(p2);
        Collections.sort(p3);
        Collections.sort(dipai);
        //5.看牌
        System.out.println("刘备:" + getPoker(p1, poker));
        System.out.println("关羽:" + getPoker(p2, poker));
        System.out.println("张飞:" + getPoker(p3, poker));
        System.out.println("底牌:" + getPoker(dipai, poker));

    }
    //定义一个看牌方法,通过排序好的编号作为键,去查找对应的值,拼接到字符串中并返回
    public static String getPoker(List list, Map map) {
        StringBuilder bld = new StringBuilder("[");
        for (int i = 0; i < list.size(); i++) {
            //取出编号
            Integer n = list.get(i);
            //用编号做键,到map中取值(牌面)
            String poker = map.get(n);
            //将牌面封装到StringBuilder
            bld.append(poker);
            if (i < list.size() - 1) {
                bld.append(", ");
            } else {
                bld.append("]");
            }
        }
        return bld.toString();
    }
}


        感谢您的阅读,不足之处欢迎指正

        

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/743175.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-29
下一篇 2022-04-29

发表评论

登录后才能评论

评论列表(0条)

保存