三人斗地主(for循环,Map集合,list集合)

2022-08-02,,,,

需求

/**

    1. 用map存储 牌的信息
  • key 存储 1-54张的编号 ; value 就是 花色+数字 组合牌的信息
    1. 用list 集合 存储 1-54张的编号
    1. 用2个数组 分别存储 4种花色 和 A ,2,3…J,Q ,K
    1. 将 牌的具体内容放到 map和list
    1. 发牌
    1. 看牌
      */

代码的实现

public class Game2{
    public static void main(String[] args) {
    //存储 牌的信息
    Map<Integer,String> map=new HashMap<>();
    //存储 1-54张的编号
    List<Integer> list=new ArrayList<>();
    //用2个数组存放 花色和 13张牌
    String [] colors={"♠","♡","♣","♦"};
    String [] numbers={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
    //记录编号
    int index= 0;
    //将牌放到上面的集合中  共循环 4*13=52次
    for(String num:numbers){
        for (String col:colors){
            map.put(index,col+num);
            list.add(index);
            index++;
        }
    }
        System.out.println("index="+index);
     //剩余2个编号就是大王小王
        list.add(52);
        list.add(53);
        map.put(52,"小王");
        map.put(53,"大王");
        //发牌之前 需要将牌打乱 ,也就是随机
        Collections.shuffle(list);//默认排序
        //发牌开始   ( 3人  , 还要 留下3张底牌) 需要定义3个人 和 1个底牌 集合接收牌
        List<Integer> p1 = new ArrayList<>();
        List<Integer> p2 = new ArrayList<>();
        List<Integer> p3 = new ArrayList<>();
        List<Integer> bottom = new ArrayList<>();
        //具体发送 牌
        for (int i = 0; i < list.size(); i++) {
            if(i<3){    // i = 0 , 1, 2 ,就是底牌了
                bottom.add(list.get(i));
            }else if(i%3==0){
                p1.add(list.get(i));
            }else if(i%3==1){
                p2.add(list.get(i));
            }else{
                p3.add(list.get(i));
            }
        }
        //看一下发到手里的牌信息
        Collections.sort(p1);
        Collections.sort(p2);
        Collections.sort(p3);
        Collections.sort(bottom);
        show("朱凯歌",p1,map);
        show("王昭雯",p2,map);
        show("朱凯歌",p3,map);
        show("底牌",bottom,map);
    }
    private static void show(String name, List<Integer> list, Map<Integer, String> map) {
        System.out.print(name+":  ");
        for(Integer in:list){
            System.out.print(map.get(in)+"  ");
        }
        System.out.println();
    }}

运行结果

index=52
朱凯歌: ♡A ♦A ♡2 ♦2 ♦4 ♡5 ♦5 ♠6 ♡6 ♣7 ♦8 ♦9 ♡10 ♦J ♡Q ♠K ♡K
王昭雯: ♣A ♠2 ♣2 ♠3 ♦3 ♡4 ♣4 ♠7 ♡7 ♠8 ♣8 ♣9 ♠10 ♠J ♡J ♣J ♠Q
朱凯歌: ♠A ♡3 ♣3 ♠4 ♠5 ♣5 ♦6 ♦7 ♠9 ♡9 ♦10 ♣Q ♦Q ♣K ♦K 小王 大王
底牌: ♣6 ♡8 ♣10

用list集合写

public static void main(String[] args) {
        //定义存储牌的集合
        List<String> pai = new ArrayList<>();
        //创建花色
        List<String> colors = new ArrayList<>();
        colors.add("♤");
        colors.add("♥");
        colors.add("♣");
        colors.add("♦");
       //创建数字,并存储数字
        List<String> nums = new ArrayList<>();
        for (int i = 2; i <=10; i++) {
            nums.add(i+" ");
        }
        nums.add("J");
        nums.add("Q");
        nums.add("K");
        nums.add("A");
//花色和数字进行组合,并存到牌里
        for (String col:colors) {
            for (String num:nums) {
                String s = col + num;
                pai.add(s);
            }
        }
    //添加大王小王
    pai.add("大王");
    pai.add("小王");
//洗牌
        Collections.shuffle(pai);
        // 创建三个玩家以及一个底牌集合进行存储
        List<String> play1 = new ArrayList<String>();
        List<String> play2 = new ArrayList<String>();
        List<String> play3 = new ArrayList<String>();
        List<String> dipai = new ArrayList<String>();

        for (int i = 0; i < pai.size(); i++) {
            // 根据索引获取牌
            String card = pai.get(i);
            // 准备底牌
            if(i >= 51) {
                dipai.add(card);
            } else {
                // 三个玩家
                if(i % 3 == 0) {
                    play1.add(card);
                } else if (i % 3 == 1) {
                    play2.add(card);
                } else {
                    play3.add(card);
                }
            }
        }
        // 4. 查看牌
        System.out.println("play1"+play1);
        System.out.println("play2"+play2);
        System.out.println("play3"+play3);
        System.out.println("dipai"+dipai);
    }

运行结果
play1[♦2 , ♥Q, 小王, ♣2 , ♤K, ♦J, ♣Q, ♤3 , ♦A, ♥J, ♣8 , ♤2 , ♦9 , ♥9 , ♣A, ♥A, ♦6 ]
play2[♤4 , ♤8 , ♦4 , ♤7 , ♣J, ♦K, ♦10 , ♤J, ♦Q, ♥K, 大王, ♥7 , ♣9 , ♥3 , ♥6 , ♣5 , ♦5 ]
play3[♤9 , ♥2 , ♦7 , ♣3 , ♣6 , ♦8 , ♤10 , ♤6 , ♣10 , ♤5 , ♥8 , ♤A, ♥10 , ♥4 , ♣7 , ♤Q, ♥5 ]
dipai[♣4 , ♦3 , ♣K]

本文地址:https://blog.csdn.net/zhu_kai_ge/article/details/107384205

《三人斗地主(for循环,Map集合,list集合).doc》

下载本文的Word格式文档,以方便收藏与打印。