【Java】集合 --- 简单斗地主发牌、看牌、排序操作

步骤:1.准备牌:创建ArrayList集合对象将54张扑克放入集合中;

           2.发牌:用集合工具类的shuffle方法,将ArrayList打乱,直接调用就行;

           3.看牌:将List集合的元素放入四份,三个玩家,一份底牌

代码实现:

import java.util.ArrayList;
import java.util.Collections;

public class poker01 {
    public static void main(String[] args) {
        //1.准备牌
        ArrayList<String> poker = new ArrayList<> ();
        poker.add("大王");
        poker.add("小王");
        String[] colors = {"♠","♣","♥","♦"};
        String[] numbers = {"A","K","Q","J","10","9","8","7","6","5","4","3","2"};
        for(String color:colors){
            for(String number:numbers){
                poker.add(color+number);
            }
        }
        // 2.洗牌
        //使用Collections中的方法shuffle(List)
        Collections.shuffle(poker);
        //3.发牌
        //定义4个集合,存储玩家牌的索引,和底牌的索引
        ArrayList<String> play1 = new ArrayList ();
        ArrayList<String> play2 = new ArrayList ();
        ArrayList<String> play3 = new ArrayList ();
        ArrayList<String> zhuozi =new ArrayList ();
        for (int i = 0; i < poker.size (); i++) {
            if(i >= 51){
                zhuozi.add (poker.get (i));
            }else if(i % 3 == 0){
                play1.add (poker.get (i));
            }else if(i % 3 == 1){
                play2.add (poker.get (i));
            }else if(i % 3 == 2){
                play3.add (poker.get (i));
            }
        }
        System.out.println ("金克丝:" + play1);
        System.out.println ("拉克丝:" + play2);
        System.out.println ("金克喵:" + play3);
        System.out.println ("底牌:" + zhuozi);
    }
}

运行结果:

金克丝:[♣9, ♣3, ♣7, ♦A, ♦7, 小王, ♦J, ♣2, ♦4, ♠3, ♦10, ♠2, ♥8, ♥2, ♥Q, ♣8, ♥5]
拉克丝:[♥K, ♦9, ♦8, ♥3, ♥10, ♣J, ♣4, ♠7, ♥6, ♠A, ♠5, ♠10, ♠Q, ♦Q, ♥J, ♥9, ♠9]
金克喵:[♥4, ♠6, ♣Q, ♠K, ♦K, ♥7, ♣A, ♣6, ♠J, ♥A, ♣5, ♦5, ♣K, ♣10, ♠4, ♠8, 大王]
底牌:[♦6, ♦2, ♦3]

优化:加入排序,使得牌顺序输出

上面的代码中没有对看牌的打印结果进行排序,这样看上去很乱,用户体验不好,下面加入排序功能。
分析:要想实现排序,需要找到排序的规则,扑克牌中的点数对于计算机来说不方便进行排序,因此我们自己定义一个int类型的变量作为排序索引,然后将该索引的值与扑克的点数做映射,这样只要根据该索引进行排序就可以了。
正好在java中的map有这种key-value对应的功能,所以这里使用map来解决这个问题,将扑克牌的点数放到Map的value中,将索引放到key中。
需要使用list中的shuffle方法来进行洗牌操作,上面已经将索引和扑克牌的点数做了映射了,因此只需要将索引即map的key放到List中存放一份。使用shuffle方法对List进行清洗,之后将清洗后的list中的数据分别均等的存放到三个TreeSet中,这样TreeSet中的数据是排好序的,将这些数据作为key去map查找相应的value,打印即可。

这里我们实际上是按照,先插入同数字的牌对应不同的四个花色的四张牌,在依次往后插入集合,并且每插入一张牌,牌的索引就+1,最后实现,牌索引的升序排序,就对应的牌的大小排序(即,Map集合中,知道key值之后,就对应知道了value的值),斗地主的发牌是很经典的集合案例题,对于Map以及List的使用需要了解。

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

代码实现:

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

public class DouDiZhu {
    /*
        斗地主综合案例:有序版本
        加入排序功能,需要用到索引 Map中的key-value 存储牌所对应的序号,从而实现排序功能
     */
    public static void main(String[] args) {
        //1.准备牌
        //创建一个Map集合,存储牌的索引和组装好的牌
        HashMap<Integer, String> poker = new HashMap<> ();
        //创建一个List集合,存储牌的索引
        ArrayList<Integer> pokerIndex = new ArrayList<> ();
        //定义两个集合,存储花色和牌的序号
        String[] Colors = {"♠","♣","♥","♦"};
        String[] 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++;
            }
        }

        //2.洗牌
        Collections.shuffle (pokerIndex);

        //3.发牌
        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);
            if (i >= 51) {
                diPai.add (in);
            } else if (i % 3 == 0) {
                player01.add (in);
            } else if (i % 3 == 1) {
                player02.add (in);
            } else if (i % 3 == 2) {
                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);
    }

    /*
        定义一个看牌的方法,提高代码的复用性
        参数:
            String name:玩家名称
            HashMap<Integer,String> poker:存储牌的poker集合
            ArrayList<Integer> list:存储玩家和底牌的List集合
        查表法:
             遍历玩家或者底牌集合,获取牌的索引
             使用牌的索引,去Map集合中,找到对应的牌
     */
    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 ();//打印完每一个玩家的牌,换行
    }
}

运行结果:

金克丝::大王 ♣A ♥A ♠2 ♣2 ♦2 ♠3 ♣3 ♥5 ♠6 ♣6 ♠7 ♥7 ♣8 ♠10 ♠J ♣K 
拉克丝::小王 ♠A ♥3 ♠4 ♥4 ♦4 ♠5 ♥6 ♣7 ♠8 ♥8 ♦8 ♠9 ♣10 ♥10 ♦10 ♦J 
金克喵::♦A ♦3 ♣4 ♣5 ♦5 ♦6 ♦7 ♣9 ♥9 ♦9 ♠Q ♣Q ♥Q ♦Q ♠K ♥K ♦K 
底牌::♥2 ♣J ♥J 

我们可以看到,排序是按从小到大依次排序

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值