Java--实现简单的斗地主的发牌(集合Map练习)优化手中牌的顺序

Java–实现简单的斗地主的发牌(集合Map练习)优化手中牌的顺序

博客说明

文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢!

介绍

实现简单的斗地主的准备牌,发牌,洗牌,看牌

优化看牌时,牌的排列顺序。

代码

package www;

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

/**
 * @author guizimo
 * @date 2020/4/5 6:35 下午
 */
public class doudizhuy {
    public static void main(String[] args) {
        //准备牌
        //存储牌的索引和牌
        HashMap<Integer,String> poker = new HashMap<>();
        //单独存储牌的索引
        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");
        //大小王
        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++;
            }
        }
//        System.out.println(poker);
//        System.out.println(pokerIndex);

        //洗牌
        Collections.shuffle(pokerIndex);

        //发牌
        //存储三个玩家的索引,和底牌的索引
        ArrayList<Integer> player1 = new ArrayList<>();
        ArrayList<Integer> player2 = new ArrayList<>();
        ArrayList<Integer> player3 = new ArrayList<>();
        ArrayList<Integer> dipai = new ArrayList<>();
        for (int i = 0; i < pokerIndex.size(); i++) {
            Integer in = pokerIndex.get(i);
            if(i>=51){
                dipai.add(in);
            }else if(i%3==0){
                player1.add(in);
            }else if(i%3==1){
                player2.add(in);
            }else if(i%3==2){
                player3.add(in);
            }
        }

        //排序
        Collections.sort(player1);
        Collections.sort(player2);
        Collections.sort(player3);
        Collections.sort(dipai);

        //看牌
        lookPoker("李逵",poker,player1);
        lookPoker("吴用",poker,player2);
        lookPoker("宋江",poker,player3);
        lookPoker("底牌",poker,dipai);

    }

    //看牌
    public static void lookPoker(String name,HashMap<Integer,String> poker,ArrayList<Integer> list){
        System.out.print(name+": ");
        for (Integer key : list) {
            String value = poker.get(key);
            System.out.print(value+" ");
        }

        System.out.println();
    }
}

结果

在这里插入图片描述

感谢

黑马程序员

以及勤劳的自己

发布了249 篇原创文章 · 获赞 624 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_45163122/article/details/105331597